简体   繁体   English

Spring安全oauth2与jwt,撤销刷新令牌

[英]Spring Security oauth2 with jwt, revocation of refresh token

I currently have an implementation of spring security with oauth2 running on spring boot. 我目前有一个spring security的实现,oauth2在spring boot上运行。 It is working as expected, and I have set the validity of access tokens to 10 minutes and refresh tokens to 30 days. 它按预期工作,我已将访问令牌的有效性设置为10分钟,并将令牌刷新为30天。

However, I would like to be able to invalidate the refresh token if a user has lost a device and wants that client to be logged out. 但是,如果用户丢失了设备并希望该客户端注销,我希望能够使刷新令牌无效。

My token store looks as following: 我的令牌存储看起来如下:

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

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

After looking on the JwtTokenStore class the storeRefreshToken and storeAccessToken methods are blank as expected since the tokens are signed they don't have to be stored. 在查看JwtTokenStore类之后,storeRefreshToken和storeAccessToken方法按预期为空,因为令牌已签名,因此无需存储它们。

My plan was to store the generated refresh tokens in a database and then include this as a requirement for the refresh token to be valid. 我的计划是将生成的刷新令牌存储在数据库中,然后将其作为刷新令牌有效的要求。

I've been looking at the JwtTokenStore class and it looks like it can have an optional ApprovalStore. 我一直在看JwtTokenStore类,看起来它可以有一个可选的ApprovalStore。 Is this the right direction to go to solve this problem? 这是解决这个问题的正确方向吗?

I think the problem is very similar to the one described here . 我认为问题与这里描述的问题非常相似。 So you might want to look at the accepted answer. 所以你可能想看看接受的答案。

Apart from this, I have two additional ideas I would like to share: 除此之外,我还有两个想要分享的想法:

Delete the client 删除客户端

It really depends on how you use client ids. 这实际上取决于您如何使用客户端ID。 But you could, of course, delete a client - this would make the refresh process fail. 但是,您当然可以删除客户端 - 这会使刷新过程失败。

Deactivate the user 停用用户

From the documentation: 从文档:

if you inject a UserDetailsService or if one is configured globally anyway (eg in a GlobalAuthenticationManagerConfigurer) then a refresh token grant will contain a check on the user details, to ensure that the account is still active 如果您注入UserDetailsS​​ervice或者无论如何全局配置(例如在GlobalAuthenticationManagerConfigurer中),那么刷新令牌授权将包含对用户详细信息的检查,以确保该帐户仍处于活动状态

So if you are using a UserDetailsService and your token is associated with a user you could deactivate the user to make the refresh process fail. 因此,如果您使用UserDetailsService并且您的令牌与用户关联,则可以停用该用户以使刷新过程失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM