• Spring Security(05)——异常信息本地化


      Spring Security支持将展现给终端用户看的异常信息本地化,这些信息包括认证失败、访问被拒绝等。而对于展现给开发者看的异常信息和日志信息(如配置错误)则是不能够进行本地化的,它们是以英文硬编码在Spring Security的代码中的。在Spring-Security-core-xxx.jar包的org.springframework.security包下拥有一个以英文异常信息为基础的messages.properties文件,以及其它一些常用语言的异常信息对应的文件,如messages_zh_CN.properties文件。那么对于用户而言所需要做的就是在自己的ApplicationContext中定义如下这样一个bean。

       <bean id="messageSource"

       class="org.springframework.context.support.ReloadableResourceBundleMessageSource">

          <property name="basename"

             value="classpath:org/springframework/security/messages" />

       </bean>

           如果要自己定制messages.properties文件,或者需要新增本地化支持文件,则可以copy Spring Security提供的默认messages.properties文件,将其中的内容进行修改后再注入到上述bean中。比如我要定制一些中文的提示信息,那么我可以在copy一个messages.properties文件到类路径的“com/xxx”下,然后将其重命名为messages_zh_CN.properties,并修改其中的提示信息。然后通过basenames属性注入到上述bean中,如:

       <bean id="messageSource"

       class="org.springframework.context.support.ReloadableResourceBundleMessageSource">

          <property name="basenames">

             <array>

                <!-- 将自定义的放在Spring Security内置的之前 -->

                <value>classpath:com/xxx/messages</value>

                <value>classpath:org/springframework/security/messages</value>

             </array>

          </property>

       </bean>

           有一点需要注意的是将自定义的messages.properties文件路径定义在Spring Security内置的message.properties路径定义之前。

    (注:本文是基于Spring Security3.1.6所写)

  • 相关阅读:
    从HDFS下载文件到本地
    oracle jdbc jar包异常
    spring jdbctemplate 启动报错(oracle驱动bug)
    solr搜索异常
    jdbc 处理Clob
    Wavosaur音频编辑软件: 功能专业,体积超小(500KB)
    isa server 2004中找不到HTTP过滤选项。
    cisco pix logging facility含义
    IPSec基础-密钥交换和密钥保护
    cisco 831 使用 SDM
  • 原文地址:https://www.cnblogs.com/fenglan/p/5912908.html
Copyright © 2020-2023  润新知