登录时更改语言环境 [英] Change locale on login

查看:25
本文介绍了登录时更改语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在登录后使用 Spring Security (3.0) 将语言环境更改为存储在用户帐户 Spring MVC Application (3.0) 中的默认语言环境.

I want to change the locale after login to an default locale stored in the user account Spring MVC Application (3.0) with Spring Security (3.0).

我已经使用了 LocaleChangeInterceptor,因此(未登录以及已登录的)用户可以更改其区域设置(默认来自接受标头).但客户确实想要该帐户特定的默认值.

I already use the LocaleChangeInterceptor so a (not logged in, as well a logged in) user can change its locale (with default from the the accept header). But the customer really want that account specific default.

所以我的问题是,登录后更改语言环境的最佳方法是什么,或者 Spring/Security 中是否已经有一些内置功能?

So my question is, what would be the best way to change the locale after login, or is there already some build in functionality in Spring/Security?

推荐答案

我能找到的最佳解决方案是在 AuthenticationSuccessHandler 中处理这个问题.

The best solution I was able to find was to handle this in the AuthenticationSuccessHandler.

以下是我为创业写的一些代码:

The following is some code I wrote for my startup:

public class LocaleSettingAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
    @Resource
    private LocaleResolver localeResolver;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        setLocale(authentication, request, response);
        super.onAuthenticationSuccess(request, response, authentication);
    }

    protected void setLocale(Authentication authentication, HttpServletRequest request, HttpServletResponse response) {
        if (authentication != null) {
            Object principal = authentication.getPrincipal();
            if (principal instanceof LocaleProvider) {
                LocaleProvider localeProvider = (LocaleProvider) principal;
                Locale providedLocale = localeProvider.getLocale();
                localeResolver.setLocale(request, response, providedLocale);
            }
        }
    }
}

您的主体类应提供以下接口.这不是必需的,但我正在使用它,因为我有多个能够为会话提供语言环境的对象.

And the following interface should be offered by your principal class. This is not needed but I'm using it since I have multiple objects capable of providing a locale for the session.

public interface LocaleProvider {    
    Locale getLocale();    
}

配置片段:

<security:http ...>
    <security:custom-filter ref="usernamePasswordAuthenticationFilter" position="FORM_LOGIN_FILTER"/>
</security:http>

<bean id="usernamePasswordAuthenticationFilter"
    class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
    <property name="filterProcessesUrl" value="/login/j_spring_security_check"/>
    <property name="authenticationManager" ref="authenticationManager"/>
    <property name="authenticationFailureHandler">
        <bean class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
            <property name="defaultFailureUrl" value="/login?login_error=t"/>
        </bean>
    </property>
    <property name="authenticationSuccessHandler">
        <bean class="LocaleSettingAuthenticationSuccessHandler">
    </property>
</bean>

这篇关于登录时更改语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆