klogd
klogd

Reputation: 1179

Grails/SpringSecurity: Getting locale in LogoutHandler

In a grails app, I'm successfully getting the language the user has selected(added to url, "...?lang=xx_XX") like this:

def locale = RequestContextUtils.getLocale(request)

Using springsecurity, and got a special logout handler set up that works fine

grails.plugins.springsecurity.logout.handlerNames = ['securityContextLogoutHandler', 'myLogoutHandler']

I need to get the user selected locale in myLogoutHandler, but the following is not working (it only shows the browser default locale, not the one selected by the user)

class MyLogoutHandler implements LogoutHandler {
    void logout(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) {
        def locale2 = RequestContextUtils.getLocale(httpServletRequest);
    }
}

I have also tried getting the session locale with:

RequestContextHolder.currentRequestAttributes().getProperties()

but that gives the same result, anyone got an idea how to get the locale from the MyLogoutHandler?

Upvotes: 0

Views: 831

Answers (2)

klogd
klogd

Reputation: 1179

Workaround

The session appears to be cleared by spring-security but you can still send in parameters.

so in the controller for the logout page I got:

def index = {
    def myParam = "bar"
    redirect(uri: SpringSecurityUtils.securityConfig.logout.filterProcessesUrl + "?foo=" + myParam) // '/j_spring_security_logout'
}

And I just get the parameter in the LogoutHandler:

void logout(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) {
    def myParam = httpServletRequest.parameterMap.get("foo")[0]
    ....
}

Upvotes: 1

Marco
Marco

Reputation: 15929

I can get the locales by using the code below:

class CustomLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {

    private static final ThreadLocal<Authentication> AUTH_HOLDER = new ThreadLocal<Authentication>()

    void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        AUTH_HOLDER.set authentication

        // reading locales...
        request.locales.each {locale ->println locale.toString()}

        try {
            super.handle(request, response, authentication)
        }
        finally {
            AUTH_HOLDER.remove()
        }
    }

    @Override
    protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response) {
        Authentication auth = AUTH_HOLDER.get()

        String url = super.determineTargetUrl(request, response)

        // do something with the url based on session data..

        url
    }
}

Upvotes: 0

Related Questions