使用AcceptHeaderLocaleResolver和i18n的Spring Security

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

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

我从spring安全包复制了messages.properties并重命名并添加到带有值列表的现有“messageSource”bean(ResourceBundleMessageSource)

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

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

更新:
我的xy-servlet.xml包含:

...  ...    defaultMessages securityMessages    

和文件

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

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

defaultMessages工作正常。 securityMessages没有。 我对所有securityMessages文件进行了小的更改,但忽略它们并显示硬编码的英语消息。

更新v2:我的dispatcher-servlet.xml:

            defaultMessages securityMessages org/springframework/security/messages_de          

最后,解决方案!

用于安全消息的Bean显然必须在applicationContext-security.xml中声明,而不是在应用程序上下文xml配置中声明…我在手册中没有找到它!

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

    secMessages   

感谢@bluefoot@jtoberon的一些想法。

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

     contextConfigLocation /WEB-INF/spring/applicationContext-security.xml    localizationFilter org.springframework.web.filter.RequestContextFilter   springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy    localizationFilter /*   springSecurityFilterChain /* REQUEST FORWARD INCLUDE ERROR    org.springframework.web.context.ContextLoaderListener    appServlet org.springframework.web.servlet.DispatcherServlet  contextConfigLocation /WEB-INF/spring/appServlet/servlet-context.xml  1   appServlet /   

在i18n评论后检查行。

我不知道你是怎么做的(你没说),但是为了使用spring security附带的消息包(而不是硬编码的文本消息),我只需要声明一个ResourceBundleMessageSource bean并设置basenames属性:

    org/springframework/security/messages_pt_BR    

这会将消息更改为pt_BR,方法是使用这个bean而不是默认的bean(不需要将文件复制到其他地方,当然,假设你的类路径上有jar)。

以下是一些想法:

  1. 是否有可能安全消息不在类路径上? 您的资源文件是否都在同一目录中? 你是把它们放在WEB-INF /类中,如果没有,那么你怎么知道它们在类路径上?
  2. 你有命名空间或密钥冲突吗? 换句话说,是否已在另一个资源文件(正在运行的文件中)中定义的安全错误消息的默认值?