2012-09-15 234 views
0

我配置我的項目是國際化,但現在我試圖改變春季安全自定義消息沒有成功...我已經閱讀並嘗試了一些東西,但沒有成功。我可以選擇普通的消息,但不是春季安全消息。Spring i18n Vs彈簧安全

這裏是我的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> 

這裏是我的彈簧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> 

更新1 我想你的解決方案,複製彈簧安全文件,並改變它們沒有成功.. 當我讀這個線程Spring Security with AcceptHeaderLocaleResolver and i18n我想,解決方案也沒有成功 這是我更新的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 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> 

這是我更新的web.xml

part1

part2

回答

1

我不知道你是否試過這個,

  1. 複製來自org.springframework.security的消息文件,並將它們更改爲
  2. 使用這些文件配置您的消息源。

默認情況下Spring Security使用org.springframework.security.core.SpringSecurityMessageSource這是從的javadoc

需要的messge本地化將 默認情況下使用這個類的所有Spring Security的類。但是,所有這些類也將實現MessageSourceAware ,以便應用程序上下文可以注入一個 替代消息源。因此,僅當 部署環境未指定替代消息 源時才使用此類。

+0

請問,我對主要問題的更新... – John

2

可能有點晚了答案,但別人面臨着類似的問題,可能會發現它有用:

您的問題是安全豆根Web應用程序上下文,但messageSource是在servlet應用程序上下文信息源。這意味着安全bean不知道您的消息來源。 只需將消息源定義移動到根Web應用程序上下文。

+0

您能否解釋我們如何將消息源定義移動到根Web應用程序上下文中? –

+0

您使用的是XML還是Java配置? –

+0

我正在使用XML,現在我定義了一個類org.springframework.context.support.ReloadableResourceBundleMessageSource的bean,它的基本名爲classpath:org/springframework/security/messages(根據http://docs.spring .IO/autorepo /文檔/彈簧安全/ 4.0.0。RELEASE/reference/htmlsingle /#localization),但它不適用於我的登錄頁面。 –