Spring Security with AcceptHeaderLocaleResolver and i18n

I’m stuck, maybe I missed something in the documents or made a small mistake.

Spring Security 3.0.5 was included in my Spring MVC 3.0.5 application. AcceptHeaderLocaleResolver is used for local discovery, and localization is working fine except for security error messages.

I copied messages.properties from the Spring Security Pack and renamed and added a bean (ResourceBundleMessageSource) to the existing "messageSource" list of values.

As mentioned earlier, all texts and messages are correctly localized, except for protective seams, for using hard-coded English messages.

Any ideas how to solve this?

UPDATE:
My xy-servlet.xml contains:

...
<mvc:resources mapping="/resources/**" location="/resources/" />
...
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>defaultMessages</value>
            <value>securityMessages</value>
        </list>
    </property>
</bean>

and files

  • defaultMessages.properties
  • defaultMessages_en.properties
  • defaultMessages_de.properties
  • defaultMessages_sl.properties

and

  • securityMessages.properties
  • securityMessages_en.properties
  • securityMessages_de.properties
  • securityMessages_sl.properties

but it defaultMessagesworks fine. securityMessagesno. I made small changes to all the securityMessagesfiles, but they are ignored and hardcoded English messages are displayed.

UPDATE v2: My dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

<context:component-scan base-package="com.example.sampleapp1" />
<context:annotation-config />

<mvc:annotation-driven/>

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/views/" p:suffix=".jsp" />

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>defaultMessages</value>
            <value>securityMessages</value>
            <value>org/springframework/security/messages_de</value>
        </list>
    </property>
</bean> 

<!-- Persistence -->
<bean id="myPMF" class="org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean">
    <property name="persistenceManagerFactoryName" value="transactions-optional"/>
</bean>     

<!-- Form Validator -->

</beans>
+5
source share
3 answers

Finally, the solution!

The bean for security messages, apparently, I should have declared in applicationContext-security.xml and not in the context of the xml config application ... I did not find it anywhere in the manual!

In my case, the correct bean solution in applicationContext-security.xml:

    <b:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <b:property name="basenames">
            <b:value>secMessages</b:value>
        </b:property>
    </b:bean>

@bluefoot @jtoberon .

UPDATE: web.xml localizationFilter springSecurityFilterChain, web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/applicationContext-security.xml</param-value>
</context-param>

<!-- i18n -->
<filter>
    <filter-name>localizationFilter</filter-name>
    <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

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

<filter-mapping> 
  <filter-name>springSecurityFilterChain</filter-name> 
  <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping> 


<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>


i18n.

+5

, ( ), , spring ( ), ResourceBundleMessageSource bean basenames

<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>org/springframework/security/messages_pt_BR</value>
        </list>
    </property>
</bean>

pt_BR, spring bean ( , , , , ).

+3

:

  • , ? ? WEB-INF/, , , ?
  • ? , , (, )?
+1