Spring i18n对Spring安全

时间:2012-09-15 21:22:19

标签: spring internationalization spring-security

我将我的项目配置为国际,但现在我正在尝试更改Spring安全自定义消息但没有成功...我已经阅读并尝试了一些但没有成功的事情。我可以查看常用消息,但不能发送春季安全消息。

这是我的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>

这是我的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>

更新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

2 个答案:

答案 0 :(得分:2)

可能有点迟到的答案,但遇到类似问题的其他人可能会发现它很有用:

您的问题是安全bean位于根Web应用程序上下文中,但messageSource位于servlet应用程序上下文源中。这意味着安全bean不知道您的消息源。 只需将您的邮件来源定义移至根网络应用程序上下文。

答案 1 :(得分:1)

我不知道你是否尝试过这个,

  1. org.springframework.security复制邮件文件并更改
    它们。
  2. 使用这些文件配置您的消息来源。
  3. 默认情况下,Spring安全性使用org.springframework.security.core.SpringSecurityMessageSource来自javadocs

      

    所有需要信息本地化的Spring Security类都将通过   默认使用此类。但是,所有这些类也将实现   MessageSourceAware使应用程序上下文可以注入   替代消息来源。因此这个类只用于   部署环境尚未指定备用消息   源。