我使用Spring Boot / OAuth创建了一个API . 它当前设置为access_tokens有效期为30天,refresh_tokens有效期为5年 . 已经要求OAuth以这种方式工作,以便可以反复使用单个refresh_token . 我们还需要做的是在用户更改密码时实现一些过期刷新令牌的方式,这是我正在努力解决的问题因为我们没有使用令牌存储,因为我们正在使用JWT,所以没有必要存储令牌,即使我们将其存储在数据库中,我们也经常收到“无效刷新令牌”错误,因此删除了令牌存储 .
我的问题是,如何处理过期的刷新令牌,比如,当用户更改其密码时(如OAuth所示) .
我的客户特别要求返回的refresh_token是长寿命的,但是我担心长寿命的刷新令牌不是很安全,好像有人拿到那个令牌他们可以访问用户帐户,直到该令牌自然到期 . 就个人而言,我宁愿在45天内在refresh_tokens上设置一个较短的到期时间,迫使客户至少每45天存储一次新的refresh_token .
这是我的一些安全配置类,以显示我目前如何设置东西;
@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);
}
}
}
1 回答
如果使用JWT,则不支持撤销令牌 . 如果您希望实现此功能,则应考虑使用JdbcTokenStore .
当用户更改密码时,您应该调用revokeToken API
JDBCTokenStore还公开了一种方法,使用该方法可以使刷新令牌无效