Spring Security - 注销时获取用户名

时间:2012-11-21 17:06:46

标签: spring-security logout

如何在UserDetail中获取用户名或logout-success-url对象?以下是我的配置:

<form-login default-target-url="/security/loginSuccess.action" 
            always-use-default-target="false" 
            authentication-failure-url="/security/loginFailed.action"
            login-page="/security/restrictUser.action"/>    

<logout invalidate-session="true" logout-success-url="/security/logoutUser.action" />

<session-management 
  session-authentication-strategy-ref="customMsgAwareConcurrentSessionControlStartegy" 
  invalid-session-url="/security/logoutUser.action"/>

我想获取请求在logout-success-url内注销的用户名(对我而言,这是一个Struts 2操作方法)。我已检查SecurityContextHolder,但当控件到达SecurityContextHolder中的操作方法时,logout-success-url似乎已清除。

1 个答案:

答案 0 :(得分:5)

使用自定义LogoutSuccessHandler。在配置中定义bean并使用success-handler-ref标记的<logout>属性。

<logout success-handler-ref="customLogoutSuccessHandler" />
<beans:bean id="customLogoutSuccessHandler" class="..." />

CustomLogoutSuccessHandler内,onLogoutSuccess方法有Authentication个参数。

public class CustomLogoutSuccessHandler extends
    AbstractAuthenticationTargetUrlRequestHandler implements
    LogoutSuccessHandler {

    public void onLogoutSuccess(HttpServletRequest request,
        HttpServletResponse response, Authentication authentication)
        throws IOException, ServletException {
         // use authentication to get user name
    }
}