Internationalization and spring security in thymeleaf

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Internationalization and spring security in thymeleaf

Priya
we need to implement internationalization and spring security using spring and thymeleaf in our project.
 
As per our requirement , locale will be a user input on login page. we've used a select drop down for languges. The form is being validated using spring security. It fails to get the selected language. As such, interntionalization is working fine. Also, spring security is working fine too. But together we aren't able to make them work, for we aren't able to pass selected language.

Following is the entry in dispatcher for locale:
<bean id="messageSource"
                class="org.springframework.context.support.ResourceBundleMessageSource">
                <property name="basename" value="locale/messages" />
        </bean>
       
        <bean id="localeResolver"
                class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
                <property name="defaultLocale" value="en" />
        </bean>
       
<mvc:interceptors>
<bean id="localeChangeInterceptor"
                class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
                <property name="paramName" value="selectedLanguage" />
        </bean>
</mvc:interceptors>
 
For Spring security, we've following entry in web.xml
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/applicationContext.xml
    </param-value>
</context-param>
       
         
          <filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
            <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
          </filter> 
          <filter-mapping> 
            <filter-name>springSecurityFilterChain</filter-name> 
            <url-pattern>/*</url-pattern> 
          </filter-mapping> 

Spring applicationContext-security.xml is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
                                 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                                 http://www.springframework.org/schema/security
                                 http://www.springframework.org/schema/security/spring-security-3.1.xsd">
       
   
 
  <authentication-manager alias="authenticationManager">
        <authentication-provider>
            <user-service>
                <user name="admin" password="admin" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>   
       
    <http auto-config="true" use-expressions="true">
       
        <form-login login-page="/login.html" default-target-url="/abc.html" authentication-failure-url="/login-error.html"  />
        <logout />
       
  <intercept-url pattern="/login.html" access="permitAll" />
  <intercept-url pattern="/login-error.html" access="permitAll" />
  <intercept-url pattern="/calendar/**" access="permitAll" />
  <intercept-url pattern="/css/**" access="permitAll" />
  <intercept-url pattern="/img/**" access="permitAll" />
  <intercept-url pattern="/js/**" access="permitAll" />
  <intercept-url pattern="/**" access="hasAnyRole('ROLE_USER')" />
 
       
       
    </http>
   
</beans:beans>

We are using the following select for language dropdown:

<label for="j_selectedLanguage">Select Language : </label><select
                                        th:field="*{selectedLanguage}"
                                        style="width: 100px;">
                                                <option th:each="lang : ${langList}"
                                                        th:selected="${login.selectedLanguage}" th:value="${lang}"
                                                        th:text="${lang}">Category</option>
                                </select>


Please let us know the way forward.
Reply | Threaded
Open this post in threaded view
|

Re: Internationalization and spring security in thymeleaf

hgrimm
First verify that LocaleChangeInterceptor is working correctly by placing this suffix on a url in your webapp:
?selectedLanguage=fr

for instance:
/test/index.html?selectedLanguage=fr

If your locale changed than your configuration is correct.

Your select box will simply re-request the current URL with the suffix for the chosen language. Perhaps you can bind to the change event on the select box to do this.
Reply | Threaded
Open this post in threaded view
|

Re: Internationalization and spring security in thymeleaf

Priya
Thanks hgrimm!!

I tried changing the form action to "/xyz/j_spring_security_check?selectedLanguage=fr" on login form submit, yet am unable to achieve internationalization.

I've also verified that LocaleChangeInterceptor is working correctly by placing this suffix on a url in my webapp:
?selectedLanguage=fr

Please let me know how to proceed forward.
Reply | Threaded
Open this post in threaded view
|

Re: Internationalization and spring security in thymeleaf

hgrimm
The LocaleChangeInterceptor has nothing to do with spring security.  By passing ?selectedLanguage=fr to j_spring_security_check, the SecurityInterceptor is intercepting the call and bypassing the LocaleChangeInterceptor.  Try passing ?selectedLanguage=fr to any URL but j_spring_security_check.
Reply | Threaded
Open this post in threaded view
|

Re: Internationalization and spring security in thymeleaf

sunil0791
I've also verified that LocaleChangeInterceptor is working correctly by placing this suffix on a url in my webapp:
?selectedLanguage=fr

Which means it works,

Try onChange of select tag using jquery and trigger an href event from javascript and have href as something like /login.html?selectedLanguage=**