【问题标题】:Unwanted i18n in Spring Security's error messageSpring Security 的错误消息中不需要的 i18n
【发布时间】:2011-08-17 22:08:51
【问题描述】:

我对 Spring Security 3 和 i18n 有疑问。我将 Velocity 用于我的视图模板,其中一个中有以下代码:

#if($SPRING_SECURITY_LAST_EXCEPTION)
    <div class="error"><p>#springMessage($SPRING_SECURITY_LAST_EXCEPTION.message)</p></div>
#end

例如,当用户尝试登录并传递了错误的凭据时,页面上应该会显示错误消息。问题是应用程序尝试在区域设置“pl”中解析消息,我收到以下异常:

org.springframework.context.NoSuchMessageException: No message found under code 'Bad credentials' for locale 'pl'.

在我的配置文件中,我没有关于 i18n 的任何信息,因为我只是不需要它。 我注意到可以从请求(标头Accept-Language: pl,en-us;q=0.7,en;q=0.3)中检索有关“pl”语言环境的信息。

如何忽略这一点并仅以默认语言提供页面?

【问题讨论】:

    标签: spring spring-mvc spring-security


    【解决方案1】:

    如果您不想要 i18n,请不要使用 #springMessage$SPRING_SECURITY_LAST_EXCEPTION.message 是消息本身,而不是消息代码,因此您需要按原样输出它(尽管我不确定 Velocity 中的 HTML 转义):

    #if($SPRING_SECURITY_LAST_EXCEPTION)
        <div class="error"><p>${SPRING_SECURITY_LAST_EXCEPTION.message}</p></div>
    #end 
    

    【讨论】:

      【解决方案2】:

      过去我玩过 il8n。我所做的是强制 spring 仅使用默认语言,我通过在配置文件中使用 FixedLocaleResolver 来完成此操作。将值更改为您希望使用的默认区域设置。

      <bean id="localeResolver" class="org.springframework.web.servlet.i18n.FixedLocaleResolver">
          <property name="defaultLocale" value="en_US" />
      </bean> 
      

      【讨论】:

      • 问题由axtavt解决。我的身份验证错误自定义消息位于我的 messages.properties 文件中。是这样配置的:&lt;bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="messages" /&gt;.
      猜你喜欢
      • 2012-10-13
      • 1970-01-01
      • 2014-01-01
      • 2016-03-27
      • 1970-01-01
      • 2017-10-26
      • 2011-03-26
      • 2011-05-15
      • 2011-05-08
      相关资源
      最近更新 更多