【问题标题】:How is possible to access to the logged out user name in Spring Security?如何访问 Spring Security 中已注销的用户名?
【发布时间】:2017-05-20 02:02:21
【问题描述】:

我们使用 Spring Security 4.0.x,我需要找到访问已注销用户名的方法。 我已经配置了LogoutSuccessHandler:

<logout logout-url="/logout" success-handler-ref="logoutSuccessHandler" />

我在方法签名中看到了authentication 对象:

onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)

不幸的是,authentication 对象是空的。 我看到LogoutHandler (SecurityContextLogoutHandler) 在logoutSuccessHandler 之前清除了身份验证,但我找不到通过&lt;logout .. 配置配置LogoutHandler 的方法。

如何在 Spring Security 中访问已注销的用户名?

【问题讨论】:

    标签: spring-security spring-security4


    【解决方案1】:
    if (requiresLogout(request, response)) {
                Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    
                if (logger.isDebugEnabled()) {
                    logger.debug("Logging out user '" + auth
                            + "' and transferring to logout destination");
                }
    
                this.handler.logout(request, response, auth);
    
                logoutSuccessHandler.onLogoutSuccess(request, response, auth);
    
                return;
            }
    

    如你所见,过滤器得到了Authentication,所以即使SecurityContextLogoutHandler 清除了SecurityContextHolder 中的Authentication,auth 仍然保留Authentication,你还有其他代码吗在LogoutFilter 之前清楚Authentication 吗?

    【讨论】:

    • 我已经调试了LogoutFilter,但我看到认证已经是null。调用SecurityContextHolder#clearContext 的唯一代码是SecurityContextPersistenceFilter 和FilterChainProxy。怎么了?
    • 你有没有找到任何关于SecurityContextHolder.getContext().setAuthentication(null or null object)的代码?
    • 我找到了在 spring security 之前清理会话的代码。感谢您的帮助!
    猜你喜欢
    • 2012-11-10
    • 2013-01-23
    • 2017-04-22
    • 2011-03-05
    • 2013-04-29
    • 2012-07-31
    • 2011-03-09
    • 1970-01-01
    • 2016-09-30
    相关资源
    最近更新 更多