Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change locale on login

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).

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.

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?

like image 915
Ralph Avatar asked Dec 16 '11 08:12

Ralph


1 Answers

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();    
}

Configuration snippets:

<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>
like image 94
Michiel Trimpe Avatar answered Oct 01 '22 14:10

Michiel Trimpe