2011-10-17 53 views
4

在弹簧安全版本3.0.6中,修复了CRLF注销漏洞(https://jira.springsource.org/browse/SEC-1790),他们禁用了'spring-security-redirect'参数。Grails重定向后注销使用spring-security-core-3.0.6 +

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

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

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' 

下不再适用于春季安全的版本3.0.6+

回答

14

可以以编程方式退出,并在控制器的动作做手工重定向:

// 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 
} 
+0

凡验证类SecurityContextHolder中发现了什么? –

+2

import org.springframework.security.core.Authentication import org.springframework.security.core.context.SecurityContextHolder – mpccolorado

1

这是一个非常专门化的主题,这里是研究解决办法:

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

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

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

/** 
* 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)注册这个bean在resources.groovy

logoutSuccessHandler(com.example.package.RedirectLogoutSuccessHandler) 

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