Announcement Announcement Module
No announcement yet.
Internationalization and spring security in thymeleaf Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Internationalization and spring security in thymeleaf

    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=" eBundleMessageSource">
    <property name="basename" value="locale/messages" />

    <bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.Sessio nLocaleResolver">
    <property name="defaultLocale" value="en" />

    <bean id="localeChangeInterceptor"
    class="org.springframework.web.servlet.i18n.Locale ChangeInterceptor">
    <property name="paramName" value="selectedLanguage" />

    For Spring security, we've following entry in web.xml

    <filter-class>org.springframework.web.filter.DelegatingFil terProxy</filter-class>

    Spring applicationContext-security.xml is as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns=""

    <authentication-manager alias="authenticationManager">
    <user name="admin" password="admin" authorities="ROLE_USER" />

    <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')" />



    We are using the following select for language dropdown:

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

    Please let us know the way forward.

  • #2
    LocaleChangeInterceptor is a Spring MVC interceptor that happens after the Spring Security Filter chain has already intercepted the request. If you wish to support changing the request on login, then you will need to create a Filter that establishes the local similar to how LocaleChangeInterceptor does. Note that RequestContextUtils.getLocaleResolver(request); will be null, so you will need to inject the LocalChangeInterceptor into the Filter rather than obtaining it from the request.