My first question is here, and I will try to be specific. I'm new to Spring, and I'm trying to create a fairly simple backup system (but it really doesn't matter). The important thing is that I create a basic template, which then fills with real web pages. The application is in sleep mode, mysql, I also install i18n and Spring protection. The problem is that I can’t change my language. The only thing that works is changing the default value. First I dropped Web A LOT, and I found out that using i18n along with Spring Security is more complicated than usual. I found out that I need an additional filter:
<filter> <filter-name>localizationFilter</filter-name> <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class> </filter> <filter-mapping> <filter-name>localizationFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
What I found out is that this filter is actually processed before security, however it does not parse the request in the form: http://someserver.com/bla/home?locale=en . I debugged it, and it seems that it is not created for this purpose (and what I need). This is taken from Spring 'contacts' samples, but in this example I could not find any code that was actually aimed at changing the language. The effect is that it just doesn't work. He always tries to change the locale to my default. The good news is that if in debug mode I manually changed the locale setting to another, it worked fine, so I felt hope in my heart ...; -)
Then I found another way - by creating my own filter. I did this to combine the found example (don’t remember the author) with how RequestContextFilter is created. In the end, RequestContextFilter works just fine - just parse my requests. This new filter code is:
public class InternationalizationFilter extends OncePerRequestFilter { @Override public void destroy() {
As you can see, the query language is parameterized and the locale is set. There are 2 problems: 1. After sending the request xxxxx?locale=en it creates a Locale without the "country" attribute (only the language is set). Honestly, I do not know if there are problems - maybe not. 2. A more serious problem is that it does not work ... I mean it in the right place in the filter chain (before security), it creates the correct locale and sets it in the same style as RequestContextFilter .. But it just doesn’t work.
I would be very happy if someone could tell me how to make i18n work with spring-security, based on my example or any other ...
Thanks!
FURTHER INFORMATION: I did some experiments, and it seems that the Locale instance from the request is somehow defined.
Take a look at this code (change the RequestContextFilter class):
@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); } (...)
if to this method: LocaleContextHolder.setLocale(l, this.threadContextInheritable); I turn to the language, “it does not work at all. I mean that the language standard does not change, even if it has clearly changed. On the other hand, if I go through Locale 'l2', which has been changed to German (in debug mode ), it works great!
This means that for some reason, the Locale instance from request.getLocale() somehow approved, maybe something happens later in the code that I don't know / don't know ...
Please let me know how I should use this i18n along with the security reason, I have reached the point where I have to admit that I have no idea what is happening ...
- ==== - ====== - ====== - ======= - ====
FINAL DECISION / ANSWER (but still with a small question) Thanks to Ralph, I managed to fix my problem. I used to go in the wrong direction, but the project created by Row pushed me forward. It seems that I continued to add the interceptor incorrectly / inaccurately (previous code):
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> </bean> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> <property name="defaultLocale" value="pl"/> </bean> <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <ref bean="localeChangeInterceptor" /> </property> </bean>
Thus, the interceptor was never called for any reason.
After changing the interceptor permission to:
<mvc:interceptors> <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> </bean> </mvc:interceptors> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> <property name="defaultLocale" value="pl"/> </bean> <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> </bean>
... it started working fine without any changes to security / web.xml.
Now the problem is gone, but I'm not sure what happened. From what I understand in the second example (the one that works), I made the interceptor "global." But why did the interceptor in the first example not work? Any hint?
Thanks again for the help! N.