使用AcceptHeaderLocaleResolver和i18n的Spring Security [英] Spring Security with AcceptHeaderLocaleResolver and i18n

查看:110
本文介绍了使用AcceptHeaderLocaleResolver和i18n的Spring Security的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我卡住了,可能在文档中遗漏了一些内容或者犯了一些小错误。

I am stuck, probably missed something in docs or made some small mistake.

Spring Security 3.0.5已集成在我的Spring MVC 3.0.5应用程序中。 AcceptHeaderLocaleResolver 用于区域设置检测,本地化工作正常,但安全错误消息除外。

Spring Security 3.0.5 was integrated in my Spring MVC 3.0.5 app. AcceptHeaderLocaleResolver is used for Locale detection and localisation works ok except for Security error messages.

我从spring安全包中复制了messages.properties,并重命名并添加到现有的messageSourcebean(ResourceBundleMessageSource)并带有值列表。

I copied messages.properties from spring security package and renamed and added to existing "messageSource" bean (ResourceBundleMessageSource) with value list.

如前所述,所有文本和消息都已正确本地化,除了安全接缝以使用硬编码的英语消息。

As said earlier all texts and messages are localised correctly, except Security seams to use hardcoded English messages.

任何想法如何解决这个问题?

Any ideas how to solve this?

更新:

我的xy-servlet.xml包含:

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>

和文件


  • defaultMessages.properties

  • defaultMessages_en.properties

  • defaultMessages_de.properties

  • defaultMessages_sl.properties

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


  • securityMessages.properties

  • securityMessages_en.properties

  • securityMessages_de.properties

  • securityMessages_sl.properties

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

defaultMessages 工作正常。 securityMessages 没有。我在所有 securityMessages 文件中做了一些小改动,但忽略它们并显示硬编码的英文消息。

but defaultMessages work ok. securityMessages does not. I made small changes in all securityMessages files, but they are ignored and the hardcoded english messages are displayed.

UPDATE v2:
我的dispatcher-servlet.xml:

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>


推荐答案

最后,解决方案!


安全消息的Bean显然必须在applicationContext-security.xml
中声明,而不是在应用程序上下文中使用xml config ...我在手册中找不到任何内容!


在我的情况下,正确的解决方案是applicationContext-security.xml中的bean:

Finally, solution!

Bean for security messages apparently must me declared in applicationContext-security.xml and not in app context xml config... I did not find this anywhere in manual!

In my case correct solution is bean 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 对于某些想法。

更新:
要正常工作,web.xml必须包含 localizationFilter 之前 springSecurityFilterChain ,我的web.xml是:

UPDATE: To work this properly web.xml must contain localizationFilter before springSecurityFilterChain, my web.xml is:

<?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评论后检查行。


Check lines after i18n comments.

这篇关于使用AcceptHeaderLocaleResolver和i18n的Spring Security的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆