title: Spring security + i18n = how to make it work together?
body: <p>My first question here and i'll try to be specific. I am quite new to Spring and i'm trying to create quite simple reservation system (but this actually doesn't matter). What matters is that I am creating some basic template which i will then fill in by real webpages. Application works on hibernate,mysql, I also setup i18n and spring security. The poblem is that I cannot change my locale. The only thing which works is changing the default one.
  First I browed Web A LOT and I found out that usage a i18n together with spring security is more complicated that usually. What i found out is that i need to have additional filter:</p>
  
  <pre><code>&lt;filter&gt;
      &lt;filter-name&gt;localizationFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.RequestContextFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;localizationFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>What I found out is that this filter is indeed processed before the security one however it does not parse the request in a form: <code>http://someserver.com/bla/home?locale=en</code>. I debugged it and it seems that it's not created for such purpose (and that's what I need).
  This is taken from spring sample "contacts" however in this example I couldn't find any code that was actually targeting in changing the language. The effect is that it simply doesn't work. It always tries to change locale to my default one. The good news is that if in debug mode I manualy changed the locale-to-set to some other one it worked fine so i felt hope in my heart... ;-)</p>
  
  <p>Then i've found some other way - by creating our own filter. What i did is to merge found example (don't remeber author) together with the way how <code>RequestContextFilter</code> is created. After all the <code>RequestContextFilter</code> works fine - just donest parse my requests. That's code of the new filter:</p>
  
  <pre><code>public class InternationalizationFilter extends OncePerRequestFilter {
  
  @Override
  public void destroy() {
      // TODO Auto-generated method stub
  
  }
  
  
  @Override
  protected void doFilterInternal(final HttpServletRequest request,
          final HttpServletResponse response, final FilterChain filterChain)
          throws ServletException, IOException {
      final String newLocale = request.getParameter("locale");
      if (newLocale != null) {
          final Locale locale = StringUtils.parseLocaleString(newLocale
                  .toLowerCase());
          LocaleContextHolder.setLocale(locale);
      }
      try {
          filterChain.doFilter(request, response);
      } finally {
  
          LocaleContextHolder.resetLocaleContext();
      }
  }
  
  }
  </code></pre>
  
  <p>As you can see the request paramter locale is parsed and the locale is set. There are 2 problems:
  1. After sending request <code>xxxxx?locale=en</code> it creates Locale without "country" attribute (only language is set). To be honest I don't know if it's any problem - maybe not.
  2. The more serious problem is that it doesn't work... i mean it's in the right place in the filter chain (before the security one), it produces right locale and sets it in exackly the same way like <code>RequestContextFilter</code>... but it simply doesnt work.</p>
  
  <p>I would be very happy if someone could let me know how to make i18n work with spring-security basing on my example given or any other...</p>
  
  <p>Thanks!</p>
  
  <p>ADDITIONAL INFO:
  I made some experiments and it seems that the Locale instance from request is somehow specific.</p>
  
  <p>Look at this code (modified the <code>RequestContextFilter</code> class):</p>
  
  <pre><code>    @Override
  protected void doFilterInternal(final HttpServletRequest request,
          final HttpServletResponse response, final FilterChain filterChain)
          throws ServletException, IOException {
  
      final ServletRequestAttributes attributes = new ServletRequestAttributes(
              request);
      final Locale l = Locale.GERMAN;
      final Locale l2 = request.getLocale();
      LocaleContextHolder.setLocale(l,
              this.threadContextInheritable);
      RequestContextHolder.setRequestAttributes(attributes,
              this.threadContextInheritable);
      if (logger.isDebugEnabled()) {
          logger.debug("Bound request context to thread: " + request);
      }
  (...)
  </code></pre>
  
  <p>if to this method: <code>LocaleContextHolder.setLocale(l, this.threadContextInheritable);</code>
  I pass locale 'l' it doesn't work at all. I mean the locale doesn't change even thou it's explicitly changed.
  On the other hand if I pass there Locale 'l2' which is modified to german (in debug mode) it works fine!</p>
  
  <p>This means that for some reason the Locale instance from <code>request.getLocale()</code> is somehow favored, maybe something is going on later on in the code which I don't know/understant...</p>
  
  <p>Please let me know how should I use this i18n together with security cause I got to the point where I must admit that I have no idea what's going on...</p>
  
  <p>-====-======-======--=======-====</p>
  
  <p><strong>FINAL SOLUTION/ANSWER</strong> (but still with little question)
  Thanks to Ralph I managed to fix my issue. Previously I was going the wrong direction but the roo generated project pushed me forward. 
  It seems that I kept adding the interceptor in a wrong/not accurate way (previous code):</p>
  
  <pre><code>&lt;bean id="localeChangeInterceptor"
  class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"&gt;
  &lt;/bean&gt;
  &lt;bean id="localeResolver"
      class="org.springframework.web.servlet.i18n.CookieLocaleResolver"&gt;
      &lt;property name="defaultLocale" value="pl"/&gt;
  &lt;/bean&gt;
  &lt;bean id="handlerMapping"
      class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"&gt;
      &lt;property name="interceptors"&gt;
          &lt;ref bean="localeChangeInterceptor" /&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>This way the interceptor was never invoked for some reason.</p>
  
  <p>After changing interceptor def to:</p>
  
  <pre><code>&lt;mvc:interceptors&gt;
  &lt;bean id="localeChangeInterceptor"
  class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"&gt;
  &lt;/bean&gt;
  &lt;/mvc:interceptors&gt;
  
  &lt;bean id="localeResolver"
      class="org.springframework.web.servlet.i18n.CookieLocaleResolver"&gt;
      &lt;property name="defaultLocale" value="pl"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="handlerMapping"
      class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>... it started to work fine without any other changes to security/web.xml.</p>
  
  <p>Now the problem is gone however I am not sure what happened. From what i understand in the second example (the one that works) I made the interceptor "global". But why the interceptor definded in the first example didn't work? Any hint?</p>
  
  <p>Thanks again for help!
  N.</p>