春天安全。如何注销用户(撤销oauth2令牌) [英] Spring security. How to log out user (revoke oauth2 token)

查看:468
本文介绍了春天安全。如何注销用户(撤销oauth2令牌)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想要注销时,我会调用此代码:

When I want to get logout I invoke this code:

request.getSession().invalidate();
SecurityContextHolder.getContext().setAuthentication(null);

但在它之后(在使用旧的oauth令牌的下一个请求中)我调用

But after it (in next request using old oauth token) I invoke

SecurityContextHolder.getContext()。getAuthentication();

我看到了我的老用户那里。

and I see my old user there.

如何解决?

推荐答案

这里是我的实现(Spring OAuth2):

Here's my implementation (Spring OAuth2):

@Controller
public class OAuthController {
    @Autowired
    private TokenStore tokenStore;

    @RequestMapping(value = "/oauth/revoke-token", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public void logout(HttpServletRequest request) {
        String authHeader = request.getHeader("Authorization");
        if (authHeader != null) {
            String tokenValue = authHeader.replace("Bearer", "").trim();
            OAuth2AccessToken accessToken = tokenStore.readAccessToken(tokenValue);
            tokenStore.removeAccessToken(accessToken);
        }
    }
}

用于测试:

curl -X GET -H "Authorization: Bearer $TOKEN" http://localhost:8080/backend/oauth/revoke-token

这篇关于春天安全。如何注销用户(撤销oauth2令牌)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆