5

我被卡住了,可能錯過了文檔中的某些東西或者犯了一些小錯誤。Spring Security with AcceptHeaderLocaleResolver和i18n

Spring Security 3.0.5被集成到我的Spring MVC 3.0.5應用程序中。 AcceptHeaderLocaleResolver用於區域設置檢測和本地化工作正常,除了安全錯誤消息。

我複製spring安全包中的messages.properties並重命名並添加到現有的帶有值列表的「messageSource」bean(ResourceBundleMessageSource)

如前所述,所有文本和消息都已正確本地化,除了安全性接口使用硬編碼的英文消息。

任何想法如何解決這個問題?

UPDATE:
我的XY-servlet.xml中包含:

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

和文件

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

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

defaultMessages WOR好的。 securityMessages沒有。我對所有securityMessages文件進行了小的更改,但忽略它們並顯示硬編碼的英文消息。

更新版: 我的調度員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

最後,解決方案!

用於安全性消息的Bean顯然必須在applicationContext-security.xml中聲明 而不是在應用程序上下文中xml config ...我在手冊中找不到這個地方!

在我的情況下正確的解決辦法是在豆的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文件必須包含localizationFilterspringSecurityFilterChain,我的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> 


檢查行國際化的意見。

+0

我對Spring MVC沒有多少經驗,我不知道文件'xy-servlet.xml'不是應用程序上下文配置文件。無論如何,我很高興它的工作。 – bluefoot

+0

我使用的是Spring 3.2,似乎也面臨同樣的問題,但不幸的是,這個解決方案似乎不適合我。 – Ryan

3

我不知道你是如何做到的(你沒有說),而是利用信息包是隨春季安全(而不是硬編碼的文本消息),我只需要聲明一個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> 

,這將改變MESSA ges到pt_BR,通過使用這個bean來代替默認的bean(不需要將文件複製到別的地方,當然,假設你的類路徑中有jar)。

+0

我同意,這也適用於我。 – carlspring

+0

@bluefoot請檢查我更新的問題。我不知道如何使用你的答案。我應該使用:' org/springframework/security/securityMessages_sl'? – Solata

+0

你爲什麼要創建自己的屬性?其中一些已經進入了彈簧安全核心的罐子。可能你的文件沒有被發現到類路徑中。只需刪除'默認消息 '並將其替換爲' org/springframework/security/messages_de'(假設xy-servlet.xml是您的應用程序上下文配置文件)並查看會發生什麼情況。 – bluefoot

1

這裏有一些想法:

  1. 是否有機會,安全消息是不是在類路徑?你的資源文件都在同一個目錄中嗎?你是把它們放在WEB-INF/classes中,如果不是那麼你怎麼知道它們在類路徑中?
  2. 您是否有命名空間或密鑰衝突?換句話說,安全錯誤消息的默認值是否已經在另一個資源文件(正在工作的那個文件)中定義?
+0

感謝您的想法! ** 1。**如何檢查消息是否在類路徑中?所有文件都在同一個目錄和WEB-INF/classes ** 2中。**在我的默認消息中,我不認爲我有命名空間或密鑰衝突。 – Solata