|
@@ -22,8 +22,7 @@ import com.chelvc.framework.common.util.ObjectUtils;
|
|
import com.chelvc.framework.common.util.StringUtils;
|
|
import com.chelvc.framework.common.util.StringUtils;
|
|
import com.chelvc.framework.oauth.annotation.Authorize;
|
|
import com.chelvc.framework.oauth.annotation.Authorize;
|
|
import com.chelvc.framework.oauth.context.OAuthContextHolder;
|
|
import com.chelvc.framework.oauth.context.OAuthContextHolder;
|
|
-import com.chelvc.framework.oauth.token.TokenActiveValidator;
|
|
|
|
-import com.google.common.collect.Lists;
|
|
|
|
|
|
+import com.chelvc.framework.oauth.token.RedisTokenValidator;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.aop.framework.AopProxyUtils;
|
|
import org.springframework.aop.framework.AopProxyUtils;
|
|
@@ -40,14 +39,9 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
|
-import org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator;
|
|
|
|
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
|
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
|
import org.springframework.security.oauth2.core.OAuth2Error;
|
|
import org.springframework.security.oauth2.core.OAuth2Error;
|
|
-import org.springframework.security.oauth2.core.OAuth2TokenValidator;
|
|
|
|
-import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
|
|
|
|
-import org.springframework.security.oauth2.jwt.Jwt;
|
|
|
|
import org.springframework.security.oauth2.jwt.JwtDecoder;
|
|
import org.springframework.security.oauth2.jwt.JwtDecoder;
|
|
-import org.springframework.security.oauth2.jwt.JwtTimestampValidator;
|
|
|
|
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
|
|
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
|
|
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
|
|
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
|
|
import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter;
|
|
import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter;
|
|
@@ -78,36 +72,8 @@ public class OAuthConfigurer extends WebSecurityConfigurerAdapter {
|
|
SecretKey key = new SecretKeySpec(secret.getBytes(), AESUtils.ALGORITHM);
|
|
SecretKey key = new SecretKeySpec(secret.getBytes(), AESUtils.ALGORITHM);
|
|
NimbusJwtDecoder decoder = NimbusJwtDecoder.withSecretKey(key).build();
|
|
NimbusJwtDecoder decoder = NimbusJwtDecoder.withSecretKey(key).build();
|
|
|
|
|
|
- // 添加JWT过期校验器
|
|
|
|
- List<OAuth2TokenValidator<Jwt>> validators = Lists.newLinkedList();
|
|
|
|
- validators.add(new OAuth2TokenValidator<Jwt>() {
|
|
|
|
- private final JwtTimestampValidator delegate = new JwtTimestampValidator();
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public OAuth2TokenValidatorResult validate(Jwt jwt) {
|
|
|
|
- OAuth2TokenValidatorResult result = this.delegate.validate(jwt);
|
|
|
|
- if (result != null && result.hasErrors()) {
|
|
|
|
- throw new OAuth2AuthenticationException(new OAuth2Error(
|
|
|
|
- "TOKEN_EXPIRED", "Token has expired", null
|
|
|
|
- ));
|
|
|
|
- }
|
|
|
|
- return result;
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
-
|
|
|
|
- // 加载自定义JWT验证器
|
|
|
|
- validators.addAll(this.applicationContext.getBeansOfType(TokenActiveValidator.class).values());
|
|
|
|
-
|
|
|
|
- // 添加令牌会话初始化拦截器
|
|
|
|
- validators.add(jwt -> {
|
|
|
|
- SessionContextHolder.initializeSession(
|
|
|
|
- OAuthContextHolder.getId(jwt),
|
|
|
|
- OAuthContextHolder.getScope(jwt),
|
|
|
|
- OAuthContextHolder.isAnonymous(jwt)
|
|
|
|
- );
|
|
|
|
- return OAuth2TokenValidatorResult.success();
|
|
|
|
- });
|
|
|
|
- decoder.setJwtValidator(new DelegatingOAuth2TokenValidator<>(validators));
|
|
|
|
|
|
+ // 设置令牌验证器
|
|
|
|
+ decoder.setJwtValidator(new RedisTokenValidator());
|
|
return decoder;
|
|
return decoder;
|
|
}
|
|
}
|
|
|
|
|