LocaleResolver in filter null shows so far that it has been auto-updated! Spring MVC 3.1.1 / Spring Security 3.1.0

I am using Spring Security 3.1.0 and Spring MVC 3.1.1. I would like to be able to change the locale based on the ie url:

http://localhost:8080/abc?lang=fr

Now it works in “normal” circumstances, that is, moving from one page to another page, HOWEVER, in my application, if I go from an unprotected page to a protected page, it first gets to my login page, kindly provided by Spring Security, before how does it get to the page I want.

This is the usual Spring security behavior (to intercept a protected resource), so there are no problems with this behavior.

The problem is that the locale does not change when I am on a secure page! It remains as the default standard. i..e lang = fr does not understand.

I played with the definition of locale-related beans inside dispatcher-servlet.xml and outside, in the context application file, to do the following:

<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang" />
</mvc:interceptors>

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" p:defaultLocale="en" />

I also tried splitting the above 2 beans, having only localResolver in the app-context configuration.

I did TONS research on this and basically, I understand that I need to manually change the locale.

Even Spring Docs for Spring Security 3.1.0 say you need your own filter, or you can use RequestContextFilter. RequestContextFilter, however, does not parse the locale parameter in the query string.

Spring Security relies on Spring localization support in order to actually lookup   
the appropriate message. In order for this to work, you have to make sure that the 
locale from the incoming request is stored in Spring 
org.springframework.context.i18n.LocaleContextHolder. Spring MVC DispatcherServlet 
does this for your application automatically, but since Spring Security filters are 
invoked before this, the LocaleContextHolder needs to be set up to contain the correct 
Locale before the filters are called.  You can either do this in a filter yourself 
(which must come before the Spring Security filters in web.xml) or you can use 
Spring RequestContextFilter.

I would like to intercept the request before it hits the controller, and so I wrote my own filter.

, , , - LocaleResolver. tomcat , , "localeResolver" ( ). localeResolver NULL.

, , , LocaleResolver ... , LocaleResolver, .

? .

p.s. , , Spring . , , - NPE LocaleResolver.

, .

+3
1

web.xml? , Spring, . Spring , .

, <filter-class> web.xml org.springframework.web.filter.DelegatingFilterProxy targetBeanName bean , :

<filter>
    <filter-name>My Locale Filter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
        <param-name>targetBeanName</param-name>
        <param-value>myLocaleFilter</param-value>
    </init-param>
</filter>

Spring <bean id="myLocaleFilter"> Filter.

Filter GenericFilterBean, Filter.

+4

All Articles