首页 > 编程语言 > 详细

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

时间:2014-11-17 02:06:17      阅读:263      评论:0      收藏:0      [点我收藏+]

异常信息本地化

?

?????? 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://haohaoxuexi.iteye.com/blog/2156769

?

?

?

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

原文:http://haohaoxuexi.iteye.com/blog/2156769

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!