Grails Redirect после выхода из системы с использованием spring-security-core-3.0.6+

В весенней версии безопасности 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+


person tmarthal    schedule 17.10.2011    source источник


Ответы (2)


Вы можете выйти из системы программно и выполнить ручное перенаправление в действии контроллера:

// 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
}
person mr Konno    schedule 22.03.2012
comment
Где находится класс аутентификации и SecurityContextHolder? - person Miheretab Alemu; 18.07.2014
comment
импортировать org.springframework.security.core.Authentication импортировать org.springframework.security.core.context.SecurityContextHolder - person mpccolorado; 02.08.2014

Это довольно специализированная тема, вот исследованное решение:

Вот коммит 3.0.x, который удалил перенаправление: http://git.springsource.org/spring-security/spring-security/commit/a087e828a63edf0932e4eecf174cf816cbe6a58a

Основная идея заключается в том, что они удалили возможность для bean-компонента LogoutSuccessHandler по умолчанию обрабатывать перенаправления, удалив targetUrlParameter (установка его на нуль приводит к тому, что перенаправления не происходят).

Таким образом, решение проблемы состоит в том, чтобы 1) создать простой bean-компонент 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) Зарегистрируйте этот компонент в resources.groovy:

 logoutSuccessHandler(com.example.package.RedirectLogoutSuccessHandler)

И поведение по умолчанию заключается в том, чтобы разрешить перенаправление выхода из системы.

person tmarthal    schedule 26.10.2011