Note that there are some explanatory texts on larger screens.

plurals
  1. POLocaleResolver in filter null yet shows it has been autowired! Spring MVC 3.1.1/Spring Security 3.1.0
    text
    copied!<p>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 URL i.e.:</p> <pre><code>http://localhost:8080/abc?lang=fr </code></pre> <p>Now, this works under "normal" circumstances, i.e. going from one page to another page HOWEVER, in my application, if I go from a non-secure page to a secure page, it first hit's my login page, courtesy of Spring Security BEFORE it hits the page I want.</p> <p>This is normal Spring Security behaviour (to intercept a secure resource) so there are no problems with this behavuour.</p> <p>The problem is rather that the locale does not change when I arrive at the secured page! It stays as the default locale. i..e lang=fr is not parsed out.</p> <p>I have played with defining the locale related beans inside dispatcher-servlet.xml AND outside, in an app-context file to do the following:</p> <pre><code>&lt;mvc:interceptors&gt; &lt;bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang" /&gt; &lt;/mvc:interceptors&gt; &lt;bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" p:defaultLocale="en" /&gt; </code></pre> <p>I have also tried to split up the above 2 beans, having just <em>localResolver</em> in the app-context config.</p> <p>I have done TONS of research on this and basically, I understand I need to change the locale manually. </p> <p>Even the Spring Docs for Spring Security 3.1.0 say that you need your own "filter" or you can use RequestContextFilter. RequestContextFilter, however does not parse out the locale param in the query String.</p> <pre><code>Spring Security relies on Spring's 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's org.springframework.context.i18n.LocaleContextHolder. Spring MVC's DispatcherServlet does this for your application automatically, but since Spring Security's 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's RequestContextFilter. </code></pre> <p>I would like to intercept the request BEFORE it hits the Controller and so, I have written my own filter.</p> <p>My solution, based on what others have done as well, is to autowire the LocaleResolver. When tomcat fires up, it shows in my logs that "localeResolver" has been autowired (otherwise the app would fail right there) HOWEVER at run time, localeResolver is NULL.</p> <p>Again, there have been posts that say to define LocaleResolver in the application-context...I have done so but I still end up getting a null LocaleResolver when a request happens.</p> <p>Any ideas?? Much appreciated.</p> <p>p.s. the filter I have defined comes before the Spring Security filters. I can debug it, it hits it first but then it dies because of the NPE on LocaleResolver.</p> <p>Appreciate this, thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload