0

在 grails 应用程序中,我成功地获得了用户选择的语言(添加到 url,“...?lang=xx_XX”),如下所示:

def locale = RequestContextUtils.getLocale(request)

使用 springsecurity,并设置了一个可以正常工作的特殊注销处理程序

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

我需要在 myLogoutHandler 中获取用户选择的语言环境,但以下不起作用(它只显示浏览器默认语言环境,而不是用户选择的语言环境)

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

我还尝试使用以下方式获取会话区域设置:

RequestContextHolder.currentRequestAttributes().getProperties()

但这给出了相同的结果,有人知道如何从 MyLogoutHandler 获取语言环境吗?

4

2 回答 2

1

解决方法

会话似乎已被 spring-security 清除,但您仍然可以发送参数。

所以在我得到的注销页面的控制器中:

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

我只是在 LogoutHandler 中获取参数:

void logout(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) {
    def myParam = httpServletRequest.parameterMap.get("foo")[0]
    ....
}
于 2012-09-20T14:49:28.663 回答
0

我可以使用以下代码获取语言环境:

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
    }
}
于 2012-09-20T13:07:49.583 回答