Search code examples
internationalizationspring-securitylocale

How do you enable LocaleInterceptor to change the locale in spring-security login page?


Pardon me if this question has been asked before, but I haven't gotten a straight answer that helped me solve my problem. I have a gwt application that I have secured using spring-security. Spring security just authenticates the user and redirects to the gwt application. Now i want the user to be able to change the locale from a link on the login page after which the locale will be stored on a cookie and used in the app.

I have the following configurations, in my applicationContext.xml

<http auto-config="true">
    <intercept-url pattern="/mywebapp/**" access="ROLE_USER"/>
    <intercept-url pattern="/gwt/**" access="ROLE_USER"/>
    <intercept-url pattern="/**/*.html" access="ROLE_USER"/>
    <intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    <intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
    <form-login login-page="/login.jsp"/>
</http>
<beans:bean id="userDetailsService"
    class="com.kibet.mywebapp.server.auth.UserDetailsServiceImpl">
</beans:bean>

...
<!-- Application Message Bundle -->
<beans:bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <beans:property name="basename" value="classpath:messages" />
    <beans:property name="defaultEncoding" value="UTF-8"/>
</beans:bean>

<beans:bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <beans:property name="paramName" value="lang" />
</beans:bean>

<beans:bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <beans:property name="defaultLocale" value="pt"/>
</beans:bean>

<beans:bean id="urlMapping"
     class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <beans:property name="interceptors">
    <beans:list>
        <beans:ref bean="localeChangeInterceptor"/>
    </beans:list>
    </beans:property>
    <beans:property name="mappings">
    <beans:value>/login.jsp=userDetailsService</beans:value>
    </beans:property>
</beans:bean>

This doesn't seem to work. I have the locale properties files messages_en.properties, messages_es.properties and messages_pt.properties in my classpath. The only time it works is when I change the browsers default locale. As far as I can tell, the login page is generated by spring-security and the handler mapping cannot intercept the locale change request. If this is the reason how would I go about this? Help is highly appreciated.

Here's my Custom filter code.

public class InternationalizationFilter implements Filter {

@Override
public void destroy() {
    // TODO Auto-generated method stub

}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain filterChain) throws IOException, ServletException {
    String newLocale = request.getParameter("lang");
    if (newLocale != null) {
        Locale locale = StringUtils.parseLocaleString(newLocale
                .toLowerCase());
        LocaleContextHolder.setLocale(locale);
    }
    try {
        filterChain.doFilter(request, response);
    } finally {

        LocaleContextHolder.resetLocaleContext();
    }
}

@Override
public void init(FilterConfig arg0) throws ServletException {

}}

Solution

  • The LocaleChangeInterceptor is a part of Spring MVC and that means they don't come in picture in spring security filters. You will have to set locale yourself within the filter chain. Please also see Spring Security/SEC-1527: Internationalize one of the sample applications