|
@@ -6,8 +6,11 @@ import com.chelvc.framework.base.context.ApplicationContextHolder;
|
|
|
import com.chelvc.framework.base.context.SessionContextHolder;
|
|
|
import com.chelvc.framework.base.context.Using;
|
|
|
import com.chelvc.framework.common.model.Platform;
|
|
|
+import com.chelvc.framework.common.model.Version;
|
|
|
+import com.chelvc.framework.common.util.StringUtils;
|
|
|
import com.chelvc.framework.security.context.SecurityContextHolder;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
|
|
import org.springframework.security.oauth2.core.OAuth2Error;
|
|
|
import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
|
|
@@ -49,41 +52,46 @@ public class DefaultSessionValidator implements SessionValidator {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取版本号
|
|
|
- *
|
|
|
- * @param jwt Jwt对象
|
|
|
- * @return 版本号
|
|
|
- */
|
|
|
- protected String getVersion(Jwt jwt) {
|
|
|
- return SecurityContextHolder.getVersion(jwt);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取平台信息
|
|
|
+ * 获取用户授权信息
|
|
|
*
|
|
|
- * @param jwt Jwt对象
|
|
|
- * @return 平台信息
|
|
|
+ * @param jwt JWT对象
|
|
|
+ * @return 授权信息集合
|
|
|
*/
|
|
|
- protected Platform getPlatform(Jwt jwt) {
|
|
|
- return SecurityContextHolder.getPlatform(jwt);
|
|
|
+ protected Set<String> getAuthorities(Jwt jwt) {
|
|
|
+ return SecurityContextHolder.getAuthorities(jwt);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取用户授权信息
|
|
|
+ * 初始化会话主体信息
|
|
|
*
|
|
|
* @param jwt JWT对象
|
|
|
- * @return 授权信息集合
|
|
|
*/
|
|
|
- protected Set<String> getAuthorities(Jwt jwt) {
|
|
|
- return SecurityContextHolder.getAuthorities(jwt);
|
|
|
+ protected void initializeSessionPrincipal(Jwt jwt) {
|
|
|
+ Long id = this.getId(jwt);
|
|
|
+ String scope = this.getScope(jwt);
|
|
|
+ Set<String> authorities = this.getAuthorities(jwt);
|
|
|
+ SessionContextHolder.updateSession(id, Using.NORMAL, scope, null, null, authorities);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public OAuth2TokenValidatorResult validate(Jwt jwt) {
|
|
|
- if (!SecurityContextHolder.isClient(jwt)) {
|
|
|
- SessionContextHolder.updateSession(this.getId(jwt), Using.NORMAL, this.getScope(jwt), null,
|
|
|
- this.getVersion(jwt), this.getPlatform(jwt), null, this.getAuthorities(jwt));
|
|
|
+ // 客户端模式
|
|
|
+ if (SecurityContextHolder.isClient(jwt)) {
|
|
|
+ return OAuth2TokenValidatorResult.success();
|
|
|
}
|
|
|
+
|
|
|
+ // 校验版本号及平台信息
|
|
|
+ String version = SecurityContextHolder.getVersion(jwt);
|
|
|
+ Platform platform = SecurityContextHolder.getPlatform(jwt);
|
|
|
+ if ((StringUtils.notEmpty(version) && !Version.isAfter(SessionContextHolder.getVersion(), version, true))
|
|
|
+ || (platform != null && platform != SessionContextHolder.getPlatform())) {
|
|
|
+ throw new OAuth2AuthenticationException(new OAuth2Error(
|
|
|
+ HttpStatus.UNAUTHORIZED.name(), ApplicationContextHolder.getMessage("Unauthorized"), null
|
|
|
+ ));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 初始化主体信息
|
|
|
+ this.initializeSessionPrincipal(jwt);
|
|
|
return OAuth2TokenValidatorResult.success();
|
|
|
}
|
|
|
}
|