提问者:小点点

如何完全注销?Spring Security


如何完全注销?我的xml文件中有:

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

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

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

并将名称添加到ModelAndView并显示在我的页面上,并且仍然存在我的用户名,而不是像之前登录时那样匿名用户。

如何完全注销?我还尝试创建自己的LogoutRestessHandler实现:

SecurityContextHolder.clearContext();

但是…它似乎不起作用


共1个答案

匿名用户

这可能是Spring Security在注销时默认调用的方法(来自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提供的默认值?或者你必须自定义一些东西?