zoukankan      html  css  js  c++  java
  • Spring Security(05)——异常信息本地化

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

    (注:原创文章,转载请注明出处。原文地址:http://elim.iteye.com/blog/2156769

    异常信息本地化

           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所写)

    (注:原创文章,转载请注明出处。原文地址:http://elim.iteye.com/blog/2156769

  • 相关阅读:
    解决Error:com.intellij.util.indexing.StorageException
    Spring AOP (上)
    spring aop expression简单说明
    Spring AOP 详解
    基于Spring AOP实现对外接口的耗时监控
    JavaEE参考示例 SpringSide 4.0 GA版杀青
    MyBatis学习 之 四、MyBatis配置文件
    MyBatis学习 之 三、动态SQL语句
    MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存
    MyBatis学习 之 二、SQL语句映射文件(1)resultMap
  • 原文地址:https://www.cnblogs.com/Jeely/p/11944158.html
Copyright © 2011-2022 走看看