gpt4 book ai didi

java - Spring Security OAuth2 撤销 token 不起作用

转载 作者:行者123 更新时间:2023-11-30 06:31:56 25 4
gpt4 key购买 nike

我的 Spring Boot 应用程序中有以下配置:

@Configuration
public class SecurityConfig {

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public static class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private MyUserDetailsService userDetailsService;

@Autowired
private BCryptPasswordEncoder passwordEncoder;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
}

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}

@Configuration
@EnableAuthorizationServer
public static class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

@Autowired
private MyUserDetailsService userDetailsService;

@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;

@Autowired
@Qualifier("myOauth2ClientDetailsService")
private ClientDetailsService clientDetailsService;

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.withClientDetails(clientDetailsService);
}

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
// set custom exception translator
endpoints.exceptionTranslator(e -> {
if (e instanceof OAuth2Exception) {
OAuth2Exception exception = (OAuth2Exception) e;
return ResponseEntity
.status(exception.getHttpErrorCode())
.body(new MyWLoginException(exception.getMessage()));
} else {
throw e;
}
});
// other settings
TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
tokenEnhancerChain.setTokenEnhancers(Arrays.asList(tokenEnhancer(), accessTokenConverter()));
endpoints.authenticationManager(authenticationManager).userDetailsService(userDetailsService).tokenStore(tokenStore())
.tokenEnhancer(tokenEnhancerChain);
}

@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
}

@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(accessTokenConverter());
}

@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setSigningKey("123");
return converter;
}

@Bean
public TokenEnhancer tokenEnhancer() {
return new MyTokenEnhancer();
}

@Bean
@Primary
public DefaultTokenServices tokenServices() {
DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
defaultTokenServices.setTokenStore(tokenStore());
defaultTokenServices.setSupportRefreshToken(true);
return defaultTokenServices;
}
}

@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(-10)
public static class ResourceServerConfig extends ResourceServerConfigurerAdapter {

@Override
public void configure(HttpSecurity http) throws Exception {
http.addFilterAfter(new AuditorFilter(), BasicAuthenticationFilter.class)
.headers().frameOptions().disable()
.and().csrf().disable()
.authorizeRequests()
.antMatchers("/img/**").permitAll()
.anyRequest().authenticated();

}

@Override
public void configure(ResourceServerSecurityConfigurer config) {
config.tokenServices(tokenServices());
}

@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(accessTokenConverter());
}

@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
DefaultAccessTokenConverter defaultAccessTokenConverter = new DefaultAccessTokenConverter();
defaultAccessTokenConverter.setUserTokenConverter(userAuthenticationConverter());
converter.setAccessTokenConverter(defaultAccessTokenConverter);
converter.setSigningKey("123");
return converter;
}

@Bean
public UserAuthenticationConverter userAuthenticationConverter() {
return new MyUserAuthenticationConverter();
}

@Bean
@Primary
public DefaultTokenServices tokenServices() {
DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
defaultTokenServices.setTokenStore(tokenStore());
return defaultTokenServices;
}
}

}

此外,我有特殊的端点来撤销 token ,它通过以下方法处理请求:

@Autowired
private TokenStore tokenStore;
@Autowired
private AuthorizationServerTokenServices authorizationServerTokenServices;
@Autowired
private ResourceServerTokenServices resourceServerTokenServices;
...
final String tokenValue = ((OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails()).getTokenValue();
final OAuth2AccessToken token = tokenStore.readAccessToken(tokenValue);
tokenStore.removeAccessToken(token);
boolean authRemoved = ((DefaultTokenServices) authorizationServerTokenServices).revokeToken(tokenValue); // <- true
boolean resourceRemoved = ((DefaultTokenServices) resourceServerTokenServices).revokeToken(tokenValue); // <- true
SecurityContextHolder.getContext().setAuthentication(null);

没有任何错误。我看到 token 服务返回 true (已删除)。但是,当我使用旧访问 token 调用任何端点时,它的工作方式就像该 token 仍然存在。但我从身份验证服务器和资源服务器中删除了 token 。如何解决这个问题?

最佳答案

使用 JWT 无法进行 token 撤销,因为其中嵌入的 token 已过期。颁发 token 后,您的授权服务器将不会捕获有关 token 的任何信息。所以也许你应该尝试使用 JdbcTokenStore在您的授权服务器中将您的 token 保存在数据库中,然后根据需要撤销它们(或者也可以在内存中)。如果您的应用程序是分开的,您可以使用 RemoteTokenServices验证您的 token 。

Here这是一个向您展示如何完成此操作的教程。

关于java - Spring Security OAuth2 撤销 token 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45917934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com