Sping安全重定向到注销时的另一个上下文

时间:2018-11-20 09:09:02

标签: spring-mvc spring-security

我正在使用Spring Security登录和注销。我的要求是成功注销后,我需要重定向到其他上下文。

<security:logout logout-url="/spring/logout" logout-success-url="/spring1" />

但是它不会重定向到其他上下文。有什么方法可以重定向?

1 个答案:

答案 0 :(得分:0)

您可以注册一个LogoutSuccessHandler并进行重定向:

public class RedirectingLogoutSuccessHandler implements LogoutSuccessHandler {

    @Override
    public void onLogoutSuccess(HttpServletRequest request, 
         HttpServletResponse response, Authentication authentication)
            throws IOException, ServletException {

        String basePath = request.getScheme() + "://" + request.getServerName()
                + (request.getServerPort() != 80 ? ":" + request.getServerPort() : "");
        String otherContext = "/spring1";

        response.sendRedirect(basePath + "/" + otherContext);
    }
}

将其注册为bean并将安全配置指向它:

<logout
  logout-url="/perform_logout"
  success-handler-ref="redirectingLogoutSuccessHandler " />