Spring security + i18n = how to make it work together?

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() { // 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(); } } } 

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.

+6
source share
2 answers
  • After sending xxxxx request? locale = en it creates a Locale without the "country" attribute (only the language is set).

This is the expected behavior. There is some kind of hierarchy in java. Language is more general than country.

The idea is that you can have, for example, text in a more general language, but some units (for example, currency) in country-specific files.

@see: http://java.sun.com/developer/technicalArticles/Intl/IntlIntro/


  • A more serious problem is that it does not work ...

It should work without any manual implementation!

You need to register a local change hook, and you need to set allowAll for the login page.

 <mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang"/> </mvc:interceptors> <http auto-config="true" use-expressions="true"> <form-login login-processing-url="/resources/j_spring_security_check" login-page="/login" authentication-failure-url="/login?login_error=t"/> <logout logout-url="/resources/j_spring_security_logout"/> <!-- Configure these elements to secure URIs in your application --> <intercept-url pattern="/login" access="permitAll" /> <intercept-url pattern="/resources/**" access="permitAll" /> <intercept-url pattern="/**" access="isAuthenticated()" /> </http> 

To see this example, run the roo project with this roo script:

 // Spring Roo 1.1.5.RELEASE [rev d3a68c3] log opened at 2011-12-13 09:32:23 project --topLevelPackage de.humanfork.test --projectName localtest --java 6 persistence setup --database H2_IN_MEMORY --provider HIBERNATE ent --class ~.domain.Stuff field string --fieldName title controller all --package ~.web security setup web mvc language --code de web mvc language --code es 

Then you only need to change the filter filters for the disproportionate protection URLs as shown above ( applicationContext-security.xml )!

Now you have an application in which the user can change his local code through the local interceptor of changes in the application (when the user logs in), and also when he is not logged in (on the login page)

+1
source

I had a similar problem with Localization when I was working with a GWT application. I noticed that when we display

 <filter-mapping> <filter-name>localizationFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 

to the filter, Even image requests are sent to the filter. These queries once did not take into account the locale parameter, and therefore, when several requests fell into the filter, the Locale parameter was not. Therefore, as soon as I received the locale parameter, I put it into the session. Record all request headers and values, and you can find the root cause.

0
source

Source: https://habr.com/ru/post/903610/


All Articles