【问题标题】:Grails Redirect Post-Logout Using spring-security-core-3.0.6+Grails 使用 spring-security-core-3.0.6+ 重定向注销后
【发布时间】:2011-12-08 23:08:12
【问题描述】:

在 spring security 版本 3.0.6 中,修复了 CRLF 注销漏洞 (https://jira.springsource.org/browse/SEC-1790) 他们禁用了“spring-security-redirect”参数的使用。

默认支持注销 URL 中的重定向参数 在 3.0.6 中被删除。在 3.1 中它已经需要启用 明确的。

有没有办法重新打开重定向参数,以便我可以在我的 Grails Spring Security 注销控制器中动态重定向?

LogoutContoller.groovy

def user = springSecurityService.currentUser

if (params.redirect) {
    // this needs to log the user out and then redirect, so don't redirect until we log the user out here
    log.info "Redirecting " + springSecurityService.currentUser.username + " to " + params.redirect
    // the successHandler.targetUrlParameter is spring-security-redirect, which should redirect after successfully logging the user out
    redirect uri: SpringSecurityUtils.securityConfig.logout.filterProcessesUrl + "?spring-security-redirect="+params.redirect
    return;
}


redirect uri: SpringSecurityUtils.securityConfig.logout.filterProcessesUrl // '/j_spring_security_logout'

以下内容不再适用于 spring security 3.0.6+ 的版本

【问题讨论】:

    标签: grails spring-security grails-plugin grails-controller


    【解决方案1】:

    您可以通过编程方式注销并在控制器的操作中进行手动重定向:

    // Bean where Spring Security store logout handlers
    def logoutHandlers
    // logout action
    def logout = {
        // Logout programmatically
            Authentication auth = SecurityContextHolder.context.authentication
        if (auth) {
            logoutHandlers.each  { handler->
                handler.logout(request,response,auth)
            }
        }
        redirect uri:params.redirect
    }
    

    【讨论】:

    • 在哪里找到 Authentication 类和 SecurityContextHolder?
    • import org.springframework.security.core.Authentication import org.springframework.security.core.context.SecurityContextHolder
    【解决方案2】:

    这是一个非常专业的话题,这里是研究的解决方案:

    这是删除重定向的 3.0.x 提交:http://git.springsource.org/spring-security/spring-security/commit/a087e828a63edf0932e4eecf174cf816cbe6a58a

    基本思想是,他们通过删除 targetUrlParameter 删除了默认 LogoutSuccessHandler bean 处理重定向的能力(将其设置为 null 会导致不发生重定向)。

    因此,问题的解决方案是 1)创建一个不将targetUrlParameter设置为null的简单LogoutSuccessHandler bean:

    /**
     * Handles the navigation on logout by delegating to the {@link AbstractAuthenticationTargetUrlRequestHandler}
     * base class logic.
     */
    public class RedirectLogoutSuccessHandler extends AbstractAuthenticationTargetUrlRequestHandler
            implements LogoutSuccessHandler {
    
        public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
                throws IOException, ServletException {
            super.handle(request, response, authentication);
        }
    
    }
    

    并且 2)在resources.groovy注册这个bean:

     logoutSuccessHandler(com.example.package.RedirectLogoutSuccessHandler)
    

    默认行为是允许发生注销重定向。

    【讨论】:

      猜你喜欢
      • 2011-06-08
      • 2014-11-11
      • 2016-07-21
      • 2012-05-18
      • 2016-07-14
      • 2016-07-05
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多