Spring boot Spring Boot OAuth 2-密码更改时刷新令牌过期
我已经使用SpringBoot/OAuth创建了一个API。当前设置为access\u令牌有效期为30天,refresh\u令牌有效期为5年。有人要求OAuth以这种方式工作,以便可以反复使用单个刷新令牌。我们还需要做的是,当用户更改密码时,实现某种使刷新令牌过期的方法,这是我正在努力解决的问题,因为我们没有像使用JWTs那样使用令牌存储,因此不需要存储令牌,即使我们将其存储在数据库中,我们也经常会遇到“无效刷新令牌”错误,因此,删除了令牌存储 我的问题是,如何处理过期的刷新令牌,例如,当用户更改其密码时(如OAuth所建议的) 我的客户特别要求返回的refresh_令牌是长寿命的,但我担心长寿命的refresh令牌不是很安全,因为如果任何人拥有该令牌,他们可以访问用户帐户,直到该令牌自然过期。就个人而言,我更喜欢将刷新令牌的过期时间设置为45天,强制客户端至少每45天存储一个新的刷新令牌 下面是我的一些安全配置类,展示了我目前是如何进行设置的Spring boot Spring Boot OAuth 2-密码更改时刷新令牌过期,spring-boot,spring-security,jwt,spring-security-oauth2,Spring Boot,Spring Security,Jwt,Spring Security Oauth2,我已经使用SpringBoot/OAuth创建了一个API。当前设置为access\u令牌有效期为30天,refresh\u令牌有效期为5年。有人要求OAuth以这种方式工作,以便可以反复使用单个刷新令牌。我们还需要做的是,当用户更改密码时,实现某种使刷新令牌过期的方法,这是我正在努力解决的问题,因为我们没有像使用JWTs那样使用令牌存储,因此不需要存储令牌,即使我们将其存储在数据库中,我们也经常会遇到“无效刷新令牌”错误,因此,删除了令牌存储 我的问题是,如何处理过期的刷新令牌,例如,当用户更
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private Environment env;
@Autowired
private CustomUserDetailsService userDetailsService;
@Autowired
private AccountAuthenticationProvider accountAuthenticationProvider;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
auth.authenticationProvider(accountAuthenticationProvider);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
final JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
jwtAccessTokenConverter.setSigningKey(env.getProperty("jwt.secret"));
return jwtAccessTokenConverter;
}
}
@Configuration
public class OAuth2ServerConfiguration {
private static final String RESOURCE_ID = "myapi";
@Autowired
DataSource dataSource;
@Bean
public TokenStore tokenStore() {
return new JdbcTokenStore(dataSource);
}
@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Autowired
TokenStore tokenStore;
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
resources
.resourceId(RESOURCE_ID)
.tokenStore(tokenStore);
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/oauth/**", "/view/**").permitAll()
.anyRequest().authenticated();
}
}
@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
@Autowired
private JwtAccessTokenConverter jwtAccessTokenConverter;
@Autowired
private DataSource dataSource;
@Autowired
private TokenStore tokenStore;
@Autowired
private CustomUserDetailsService userDetailsService;
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints
//.tokenStore(tokenStore)
.authenticationManager(authenticationManager)
.userDetailsService(userDetailsService)
.accessTokenConverter(jwtAccessTokenConverter);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients
.jdbc(dataSource);
}
}
}
如果使用JWT,则不支持撤销令牌。如果您希望实现此功能,则应考虑使用JDCBtoKoSturn。
@Bean
public TokenStore tokenStore() {
return new JdbcTokenStore(dataSource());
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource jdbcdataSource = new DriverManagerDataSource();
jdbcdataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
jdbcdataSource.setUrl(env.getProperty("jdbc.url"));//connection String
jdbcdataSource.setUsername(env.getProperty("jdbc.user"));
jdbcdataSource.setPassword(env.getProperty("jdbc.pass"));
return dataSource;
}
当用户更改密码时,您应该调用revokeToken API
@Resource(name="tokenServices")
ConsumerTokenServices tokenServices;
@RequestMapping(method = RequestMethod.POST, value = "/tokens/revoke/{tokenId:.*}")
@ResponseBody
public String revokeToken(@PathVariable String tokenId) {
tokenServices.revokeToken(tokenId);
return tokenId;
}
JDBCTokenStore还公开了一种方法,您可以使用该方法使刷新令牌无效
@RequestMapping(method = RequestMethod.POST, value = "/tokens/revokeRefreshToken/{tokenId:.*}")
@ResponseBody
public String revokeRefreshToken(@PathVariable String tokenId) {
if (tokenStore instanceof JdbcTokenStore){
((JdbcTokenStore) tokenStore).removeRefreshToken(tokenId);
}
return tokenId;
}