spring-boot - 带有 jwt 的 Spring Security oauth2,撤销刷新 token

标签 spring-boot spring-security jwt spring-security-oauth2

我目前有一个 spring security 的实现,oauth2 在 spring boot 上运行。它按预期工作,我已将访问 token 的有效期设置为 10 分钟,并将刷新 token 的有效期设置为 30 天。

但是,如果用户丢失了设备并希望该客户端被注销,我希望能够使刷新 token 无效。

我的 token 商店如下所示:

@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
    final JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
    jwtAcccessTokenConverter.setSigningKey(this.secret);
    return jwtAcccessTokenConverter;
}

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

在查看 JwtTokenStore 类之后, storeRefreshToken 和 storeAccessToken 方法按预期为空,因为 token 已签名,因此不必存储。

我的计划是将生成的刷新 token 存储在数据库中,然后将其包含为刷新 token 有效的要求。

我一直在查看 JwtTokenStore 类,看起来它可以有一个可选的 ApprovalStore。
这是解决这个问题的正确方向吗?

最佳答案

我认为这个问题与 here 中描述的非常相似.因此,您可能想查看已接受的答案。

除此之外,我还有两个想法要分享:

删除客户端

这实际上取决于您如何使用客户端 ID。但是,您当然可以删除客户端 - 这会使刷新过程失败。

停用用户

从文档:

if you inject a UserDetailsService or if one is configured globally anyway (e.g. in a GlobalAuthenticationManagerConfigurer) then a refresh token grant will contain a check on the user details, to ensure that the account is still active



因此,如果您使用的是 UserDetailsService并且您的 token 与用户相关联,您可以停用该用户以使刷新过程失败。

关于spring-boot - 带有 jwt 的 Spring Security oauth2,撤销刷新 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44430427/

相关文章:

java - 在 Spring Boot 中将属性放在 application.yml 或 bootstrap.yml 上有什么区别?

java - 来自 CAS 的 LDAP 用户属性

javascript - 没有过载匹配此调用。最后一个重载给出了以下错误。输入 '"文本 "' is not assignable to type ' “json”'

c# - System.IdentityModel.Tokens.JwtSecurityToken 自定义属性

Spring Boot 应用程序作为 systemd 服务 : log file

spring - 在 AWS Elastic BeanStalk 上运行的 Spring Boot 应用程序中配置 AWS RDS

spring - 在 Spring Security 中实现每组授权的最佳方式

grails - 为什么可以忽略 Grails Spring Security 中的 @Secured?

android - 使用 Retrofit2 调用 API 来保存和检索 JWT Token

gradle - 为什么我的Spring Boot应用程序的bootRun任务无法在Gradle中完全运行?