自己写 AuthenticationSuccessHandler
和 LogoutSuccessHandler
。
例:
spring-security.xml :
<security:form-login login-page="/login" login-processing-url="/login_check" authentication-failure-url="/login?error=true" authentication-success-handler-ref="myAuthenticationSuccessHandler"/><security:logout logout-url="/logout" success-handler-ref="myLogoutSuccessHandler"/>
AuthenticationSuccessHandler
@Componentpublic class MyAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { @Autowired private UserService userService; @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { // changeLastLoginTime(username) userService.changeLastLoginTime(authentication.getName()); setDefaultTargetUrl("/home"); super.onAuthenticationSuccess(request, response, authentication); }}注销成功处理程序
@Componentpublic class MyLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler { @Override public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { if (authentication != null) { // do something } setDefaultTargetUrl("/login"); super.onLogoutSuccess(request, response, authentication);}}


