com.flyfish.oauth.client.OAuth2SsoClientInitializer Maven / Gradle / Ivy
package com.flyfish.oauth.client;
import com.flyfish.oauth.common.AuditingContext;
import com.flyfish.oauth.common.OAuth2SsoInitializeAware;
import com.flyfish.oauth.configuration.OAuth2SsoProperties;
import com.flyfish.oauth.configuration.sync.SyncTransformer;
import com.flyfish.oauth.domain.OAuthSSOToken;
import com.flyfish.oauth.domain.auditing.AuditingStrategy;
import com.flyfish.oauth.domain.raw.Domain;
import com.flyfish.oauth.entry.AuditingEntryPoint;
import com.flyfish.oauth.entry.AuthenticationEntryPoint;
import com.flyfish.oauth.entry.ScribeAuthenticationEntryPoint;
import com.flyfish.oauth.entry.auditing.RestClientAuditingEntryPoint;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 认证客户端初始化载体
*
* @author wangyu
*/
@Slf4j
public abstract class OAuth2SsoClientInitializer {
/**
* 获取初始化单例
*
* @return 结果
*/
private static Initializer getInitializer() {
return Initializer.instance;
}
/**
* 初始化功能,仅调用一次
*
* @param savedClient 保存的client
*/
public synchronized static void initialize(OAuth2SsoClient savedClient) {
if (savedClient == null) {
return;
}
Initializer initializer = getInitializer();
initializer.setClient(savedClient);
// 初始化客户端
assignDefault(savedClient);
// 如果
initializer.setInitialized(true);
// 初始化认证接入点
AuthenticationEntryPoint authenticationEntryPoint = new ScribeAuthenticationEntryPoint();
authenticationEntryPoint.configure(savedClient.getProperties());
initializer.setAuthenticationEntryPoint(authenticationEntryPoint);
// 初始化审计接入点,如果开启的话
if (savedClient.getProperties().getAuditing()) {
initializer.setAuditingEntryPoint(new RestClientAuditingEntryPoint(savedClient.getProperties()));
}
for (OAuth2SsoInitializeAware aware : initializer.registries) {
doInitialize(aware);
}
// 使用完就销毁
initializer.clearRegistry();
// 认证
log.info("[==sso==]: start authenticating... =============");
OAuthSSOToken token = authenticate(authenticationEntryPoint.getClientToken());
if (null == token) {
log.error("[==sso==]: fatal error occurred! Can't pull access token!");
return;
// throw new NullPointerException("no access token");
}
log.info("[==sso==]: client authentication finished successfully! =============");
log.info(String.format("[==sso==]: token will be expired in %s", token.getExpiresIn()));
// 设置审计上下文
if (savedClient.getProperties().getAuditing()) {
log.info("[==sso==]: auditing is on, fetching auditing configurations... =============");
List strategies = initializer.auditingEntryPoint.getStrategies();
AuditingContext.sharedContext().set(strategies);
log.info("[==sso==]: fetched " + strategies.size() + " strategy to auditing! ==========");
}
// 打印
log.info("[==sso==]: sso filter initialized!=============");
}
public static void register(OAuth2SsoInitializeAware aware) {
Initializer initializer = getInitializer();
if (initializer.isInitialized()) {
doInitialize(aware);
} else {
initializer.addRegistry(aware);
}
}
private static void assignDefault(OAuth2SsoClient client) {
if (null == client.getProperties()) {
client.setProperties(new OAuth2SsoProperties());
}
}
private static void doInitialize(OAuth2SsoInitializeAware aware) {
Initializer initializer = getInitializer();
// 这里分流,可以取出同步用工具
if (aware instanceof SyncTransformer) {
initializer.addSyncTransformer((SyncTransformer extends Domain, ?>) aware);
}
aware.setClient(initializer.client);
aware.setAuthenticationEntryPoint(initializer.authenticationEntryPoint);
aware.setAuditingEntryPoint(initializer.auditingEntryPoint);
}
public static SyncTransformer extends Domain, ?> acceptTransform(String uri) {
return getInitializer().acceptTransform(uri);
}
public static boolean isInitialized() {
return getInitializer().isInitialized();
}
public static OAuth2SsoClient getClient() {
return getInitializer().getClient();
}
public static boolean authenticated() {
return getInitializer().authenticated();
}
public static OAuthSSOToken authenticate(OAuthSSOToken token) {
getInitializer().setClientToken(token);
return token;
}
public static OAuthSSOToken clientToken() {
return getInitializer().getClientToken();
}
public static AuthenticationEntryPoint entryPoint() {
return getInitializer().getAuthenticationEntryPoint();
}
/**
* 内置懒加载单例全局上下文
*/
private static class Initializer {
private static Initializer instance = new Initializer();
@Getter
private List registries = new ArrayList<>();
@Getter
@Setter
private OAuth2SsoClient client;
@Getter
@Setter
private AuthenticationEntryPoint authenticationEntryPoint;
@Getter
@Setter
private AuditingEntryPoint auditingEntryPoint;
@Setter
private OAuthSSOToken clientToken;
@Getter
@Setter
private boolean initialized = false;
@Getter
private Map> transformers = new HashMap<>();
private void addRegistry(OAuth2SsoInitializeAware registry) {
this.registries.add(registry);
}
private void addSyncTransformer(SyncTransformer extends Domain, ?> transformer) {
transformers.put(transformer.uri(), transformer);
}
private boolean authenticated() {
return clientToken != null;
}
private OAuthSSOToken getClientToken() {
if (clientToken == null) {
clientToken = authenticationEntryPoint.getClientToken();
}
return clientToken;
}
private void clearRegistry() {
this.registries = null;
}
private SyncTransformer extends Domain, ?> acceptTransform(String uri) {
return transformers.get(uri);
}
}
}