0

I configured my project to be internation but now i am trying to change spring security custom messages without success... I already read and tried some things but without success. I can se usual messages, but not spring security messages.

here is my servlet-context.xml:

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

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

<!-- Tiles -->
<beans:bean  id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <beans:property name="definitions">
        <beans:list>
            <beans:value>/WEB-INF/tiles.xml</beans:value>
        </beans:list>
    </beans:property>
</beans:bean>

<beans:bean   class="org.springframework.web.servlet.view.tiles2.TilesViewResolver">      
    <beans:property 
        name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>                
</beans:bean>

<context:component-scan base-package="com.company.projectMvc" />


<!-- default locale  -->
<beans:bean class="org.springframework.web.servlet.i18n.SessionLocaleResolver" id="localeResolver"> 
    <beans:property name="defaultLocale" value="es"/> 
 </beans:bean> 


 <interceptors> 
    <beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
         <beans:property name="paramName" value="language"/> 
    </beans:bean> 
 </interceptors> 

<!-- Messages files -->
<beans:bean id="messageSource"  name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <beans:property name="basename" value="/resources/messages/messages" />
</beans:bean>

here is my spring-security.xml:

<http auto-config="true" use-expressions="true" >
    <intercept-url pattern="/test" access="isAuthenticated()" />
    <intercept-url pattern="/**" access="isAuthenticated()" />
    <intercept-url pattern="/home" access="permitAll" />

    <form-login  default-target-url="/home"/>
    <logout logout-success-url="/test" />
</http>


<authentication-manager>
   <authentication-provider>
    <jdbc-user-service data-source-ref="dataSource"

       users-by-username-query="
          select username,password,enabled,fullname
          from users where username=?" 

       authorities-by-username-query="
          select u.username, ur.authority from users u, user_roles ur 
          where u.user_id = ur.user_id and u.username =?  " 

    />
   </authentication-provider>
</authentication-manager>

UPDATE 1 I tried your solution, copying spring security files and change them without success.. After i read this thread Spring Security with AcceptHeaderLocaleResolver and i18n i tried that solution too without success here is my updated servlet-context.xml file:

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

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

<!-- Tiles -->
<beans:bean  id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <beans:property name="definitions">
        <beans:list>
            <beans:value>/WEB-INF/tiles.xml</beans:value>
        </beans:list>
    </beans:property>
</beans:bean>

<beans:bean   class="org.springframework.web.servlet.view.tiles2.TilesViewResolver">      
    <beans:property 
        name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>                
</beans:bean>

<context:component-scan base-package="com.company.projectMvc" />

 <!-- default locale pt_PT -->
<beans:bean class="org.springframework.web.servlet.i18n.SessionLocaleResolver" id="localeResolver"> 
    <beans:property name="defaultLocale" value="pt_PT"/> 
 </beans:bean> 

<!-- Messages files -->
<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <beans:property name="basenames">
    <beans:list>
        <beans:value>/resources/messages/messages</beans:value>
        <beans:value>/resources/messages/securityMessages</beans:value>
    </beans:list>
 </beans:property>



</beans:bean>

<!-- com o parametro locale no URL conseguimos definir o locale que quisermos -->
 <interceptors> 
    <beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
         <beans:property name="paramName" value="lang"/> 
    </beans:bean> 
 </interceptors> 

Here is my updated web.xml

part1

part2

Community
  • 1
  • 1
John
  • 1,697
  • 4
  • 27
  • 53

2 Answers2

2

Probably a little late answer, but someone else facing similar issue might find it useful:

Your problem is that security beans are in root web application context, but messageSource is in the servlet application context source. This means that security beans are unaware of your message source. Simply move your message source definition to the root web application context.

Pavel Horal
  • 17,782
  • 3
  • 65
  • 89
  • Can you explain how we can move the message source definition to the root web application context? – Grégoire C Apr 02 '15 at 09:55
  • Are you using XML or Java config? – Pavel Horal Apr 02 '15 at 09:56
  • I'm using XML, at the moment I define a bean of class `org.springframework.context.support.ReloadableResourceBundleMessageSource` with basename `classpath:org/springframework/security/messages` (as per http://docs.spring.io/autorepo/docs/spring-security/4.0.0.RELEASE/reference/htmlsingle/#localization) but it doesn't work with my login page. – Grégoire C Apr 02 '15 at 10:01
  • If you can update your answer to be more precise it would be great, otherwise I might ask a new question about this. – Grégoire C Apr 02 '15 at 10:02
  • Well this boils down to the topic of understanding what *root* context is and what *servlet* context is (answered many times here on SO). The important thing is in which XML file you are defining the *message source* bean. It needs to be defined in XML loaded by `ContextLoaderListener` in your `web.xml`. – Pavel Horal Apr 02 '15 at 10:05
  • 1
    My `ReloadableResourceBundleMessageSource` bean is defined in a XML file which is defined in `contextConfigLocation` context-param. So I guess that the message source is in root web application context already. Thanks you for your help. – Grégoire C Apr 02 '15 at 10:17
1

I do not know if you tried this,

  1. Copy message files from org.springframework.security and change
    them.
  2. Configure your message source with these files.

By default Spring security uses org.springframework.security.core.SpringSecurityMessageSource This is from javadocs

All Spring Security classes requiring messge localization will by default use this class. However, all such classes will also implement MessageSourceAware so that the application context can inject an alternative message source. Therefore this class is only used when the deployment environment has not specified an alternative message source.

Adisesha
  • 5,200
  • 1
  • 32
  • 43