fix #I1OEEN

This commit is contained in:
zlt2000
2020-07-21 09:45:59 +08:00
parent 812b335ad7
commit 37890b2d6d
3 changed files with 18 additions and 26 deletions
@@ -5,6 +5,7 @@ import com.central.oauth2.common.converter.CustomUserAuthenticationConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.bootstrap.encrypt.KeyProperties; import org.springframework.cloud.bootstrap.encrypt.KeyProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter; import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.TokenEnhancer; import org.springframework.security.oauth2.provider.token.TokenEnhancer;
@@ -23,6 +24,9 @@ import java.util.Map;
* *
* @author zlt * @author zlt
* @date 2018/7/24 16:21 * @date 2018/7/24 16:21
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/ */
@ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "authJwt") @ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "authJwt")
public class AuthJwtTokenStore { public class AuthJwtTokenStore {
@@ -41,6 +45,7 @@ public class AuthJwtTokenStore {
} }
@Bean @Bean
@Order(2)
public JwtAccessTokenConverter jwtAccessTokenConverter() { public JwtAccessTokenConverter jwtAccessTokenConverter() {
final JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); final JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
KeyPair keyPair = new KeyStoreKeyFactory KeyPair keyPair = new KeyStoreKeyFactory
@@ -59,6 +64,7 @@ public class AuthJwtTokenStore {
* @return TokenEnhancer * @return TokenEnhancer
*/ */
@Bean @Bean
@Order(1)
public TokenEnhancer tokenEnhancer() { public TokenEnhancer tokenEnhancer() {
return (accessToken, authentication) -> { return (accessToken, authentication) -> {
final Map<String, Object> additionalInfo = new HashMap<>(1); final Map<String, Object> additionalInfo = new HashMap<>(1);
@@ -14,13 +14,9 @@ import org.springframework.security.oauth2.config.annotation.web.configurers.Aut
import org.springframework.security.oauth2.provider.TokenGranter; import org.springframework.security.oauth2.provider.TokenGranter;
import org.springframework.security.oauth2.provider.code.RandomValueAuthorizationCodeServices; import org.springframework.security.oauth2.provider.code.RandomValueAuthorizationCodeServices;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator; import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore; import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Arrays;
/** /**
* OAuth2 授权服务器配置 * OAuth2 授权服务器配置
@@ -47,12 +43,6 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
@Autowired @Autowired
private TokenStore tokenStore; private TokenStore tokenStore;
@Autowired(required = false)
private JwtAccessTokenConverter jwtAccessTokenConverter;
@Autowired(required = false)
private TokenEnhancer tokenEnhancer;
@Autowired @Autowired
private WebResponseExceptionTranslator webResponseExceptionTranslator; private WebResponseExceptionTranslator webResponseExceptionTranslator;
@@ -71,16 +61,6 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
*/ */
@Override @Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) { public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
if (jwtAccessTokenConverter != null) {
if (tokenEnhancer != null) {
TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
tokenEnhancerChain.setTokenEnhancers(
Arrays.asList(tokenEnhancer, jwtAccessTokenConverter));
endpoints.tokenEnhancer(tokenEnhancerChain);
} else {
endpoints.accessTokenConverter(jwtAccessTokenConverter);
}
}
endpoints.tokenStore(tokenStore) endpoints.tokenStore(tokenStore)
.authenticationManager(authenticationManager) .authenticationManager(authenticationManager)
.userDetailsService(userDetailsService) .userDetailsService(userDetailsService)
@@ -22,10 +22,7 @@ import org.springframework.security.oauth2.provider.implicit.ImplicitTokenGrante
import org.springframework.security.oauth2.provider.password.ResourceOwnerPasswordTokenGranter; import org.springframework.security.oauth2.provider.password.ResourceOwnerPasswordTokenGranter;
import org.springframework.security.oauth2.provider.refresh.RefreshTokenGranter; import org.springframework.security.oauth2.provider.refresh.RefreshTokenGranter;
import org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory; import org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices; import org.springframework.security.oauth2.provider.token.*;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider; import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider;
import java.util.ArrayList; import java.util.ArrayList;
@@ -56,7 +53,7 @@ public class TokenGranterConfig {
private TokenStore tokenStore; private TokenStore tokenStore;
@Autowired(required = false) @Autowired(required = false)
private TokenEnhancer tokenEnhancer; private List<TokenEnhancer> tokenEnhancer;
@Autowired @Autowired
private IValidateCodeService validateCodeService; private IValidateCodeService validateCodeService;
@@ -157,11 +154,20 @@ public class TokenGranterConfig {
tokenServices.setSupportRefreshToken(true); tokenServices.setSupportRefreshToken(true);
tokenServices.setReuseRefreshToken(reuseRefreshToken); tokenServices.setReuseRefreshToken(reuseRefreshToken);
tokenServices.setClientDetailsService(clientDetailsService); tokenServices.setClientDetailsService(clientDetailsService);
tokenServices.setTokenEnhancer(tokenEnhancer); tokenServices.setTokenEnhancer(tokenEnhancer());
addUserDetailsService(tokenServices, this.userDetailsService); addUserDetailsService(tokenServices, this.userDetailsService);
return tokenServices; return tokenServices;
} }
private TokenEnhancer tokenEnhancer() {
if (tokenEnhancer != null) {
TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
tokenEnhancerChain.setTokenEnhancers(tokenEnhancer);
return tokenEnhancerChain;
}
return null;
}
private void addUserDetailsService(DefaultTokenServices tokenServices, UserDetailsService userDetailsService) { private void addUserDetailsService(DefaultTokenServices tokenServices, UserDetailsService userDetailsService) {
if (userDetailsService != null) { if (userDetailsService != null) {
PreAuthenticatedAuthenticationProvider provider = new PreAuthenticatedAuthenticationProvider(); PreAuthenticatedAuthenticationProvider provider = new PreAuthenticatedAuthenticationProvider();