【问题标题】:How to fully logout? Spring Security如何完全注销?春季安全
【发布时间】:2015-10-02 08:43:04
【问题描述】:

我怎样才能完全退出?我的 xml 文件中有:

<security:logout logout-url="/logoutMe" />

当我输入 /logoutMe 网址时,我没有注销 - 在我的控制器上我添加了:

SecurityContextHolder.getContext().getAuthentication().getName()

并将名称添加到ModelAndView并显示在我的页面上,仍然有我的用户名,而不是像之前登录的anonymousUser。

如何完全退出? 我还尝试使用以下方法创建自己的 LogoutSuccessHandler 实现:

SecurityContextHolder.clearContext();

但是..它似乎不起作用

【问题讨论】:

标签: spring security logout


【解决方案1】:

这可能是 Spring 安全在注销时默认调用的方法(来自 SecurityContextLogoutHandler 类):

public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
    Assert.notNull(request, "HttpServletRequest required");
    if (invalidateHttpSession) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            logger.debug("Invalidating session: " + session.getId());
            session.invalidate();
        }
    }

    if(clearAuthentication) {
        SecurityContext context = SecurityContextHolder.getContext();
        context.setAuthentication(null);
    }

    SecurityContextHolder.clearContext();
}

您是否尝试过使用 spring security logout url /logout 提供的默认值?或者你必须自定义一些东西?

【讨论】:

  • 我尝试使用简单的 /logout 但我仍然保持登录状态。看起来 /logout 不起作用。也许我应该注册一些注销过滤器左右?如果我不手动使用 .clearContext() ,则不会发生任何事情。 /logout 选项似乎是..禁用?调用它什么都不做
  • 看这里:stackoverflow.com/questions/24108585/… /logout 需要 POST 而不是 GET
猜你喜欢
  • 1970-01-01
  • 2011-11-11
  • 2016-06-09
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
  • 1970-01-01
  • 2016-11-06
  • 2011-03-07
相关资源
最近更新 更多