以编程方式更改spring security logout-success-url

时间:2011-02-17 20:24:18

标签: spring-security

我需要根据用户的角色将用户重定向到 2个不同的退出网址。我该怎么做呢?

我正在使用 spring security 2.0 ,我的xml看起来像这样:

    <s:http access-denied-page="/" >
        <s:intercept-url pattern="/pages/SplashPage.jsf" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <s:intercept-url pattern="/pages/Home.jsf" access="ROLE_USER,ROLE_MERCHANT"/>

        <s:anonymous/>
        <s:form-login
            login-page="/"
            login-processing-url="/j_spring_security_check"
            default-target-url="/pages/Home.jsf"
            authentication-failure-url="/" always-use-default-target='false' />
        <s:logout invalidate-session="true" logout-url="/pages/logout.jsf" logout-success-url="/" />
        <s:concurrent-session-control max-sessions="1" exception-if-maximum-exceeded="false"/>
    </s:http>

2 个答案:

答案 0 :(得分:2)

我无法找到任何正确的方法来做到这一点,所以我最终得到了一个黑客:

  1. invalidate-session
  2. logout-success-url更改为特殊重定向控制器
  3. 在该控制器中,拉出用户会话以告知用户类型
  4. 使会话无效
  5. 重定向到usertype的正确网址

答案 1 :(得分:0)

编辑 - 更新为Spring Security 2.0解决方案。

用您的子类替换LogoutFilter覆盖doFilterHttp:

public void doFilterHttp(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException,
        ServletException {

    if (requiresLogout(request, response)) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();

        if (logger.isDebugEnabled()) {
            logger.debug("Logging out user '" + auth + "' and redirecting to logout page");
        }

        for (int i = 0; i < handlers.length; i++) {
            handlers[i].logout(request, response, auth);
        }

        // Do role-specific logic here to determine targetUrl

        sendRedirect(request, response, targetUrl);

        return;
    }

    chain.doFilter(request, response);
}

按如下方式更换LogoutFilter:

<beans:bean id="myLogoutFilter" class="com.mycompany.MyLogoutFilter">
  <custom-filter position="LOGOUT_FILTER"/>
</beans:bean>