如何使用 spring security 手动注销用户?

新手上路,请多包涵

可能答案很简单:如何在 spring security 中手动注销当前登录的用户?调用是否足够:

 SecurityContextHolder.getContext().getAuthentication().setAuthenticated(false);

原文由 Erik 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 575
2 个回答

在 Servlet 3.0 容器中,Spring 注销功能与 servlet 集成在一起,您只需在 logout() HttpServletRequest 。仍然需要编写有效的响应内容。

根据 文档(Spring 3.2):

HttpServletRequest.logout() 方法可用于注销当前用户。

通常这意味着 SecurityContextHolder 将被清除,HttpSession 将失效,任何“记住我”身份验证将被清除,等等。

原文由 Piotr Müller 发布,翻译遵循 CC BY-SA 3.0 许可协议

我很难确定您的代码是否足够。然而,标准 Spring-security 的注销实现是不同的。如果你看一下 SecurityContextLogoutHandler 你会看到他们这样做:

 SecurityContextHolder.clearContext();

此外,他们还可以选择使 HttpSession 无效:

 if (invalidateHttpSession) {
    HttpSession session = request.getSession(false);
    if (session != null) {
        session.invalidate();
    }
}

您可能会 在有关在 Spring Security 中注销的其他一些问题中 找到更多信息,并查看 org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler 的源代码

原文由 Grzegorz Oledzki 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题