如何在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
似乎已清除。
答案 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
}
}