Announcement Announcement Module
Collapse
No announcement yet.
Internationalization and spring security in thymeleaf Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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="org.springframework.context.support.Resourc eBundleMessageSource">
    <property name="basename" value="locale/messages" />
    </bean>

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

    <mvc:interceptors>
    <bean id="localeChangeInterceptor"
    class="org.springframework.web.servlet.i18n.Locale ChangeInterceptor">
    <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.DelegatingFil terProxy</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/schem...-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.

  • #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.

    Comment

    Working...
    X