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

(注:原創文章,轉載請註明出處。原文地址:http://haohaoxuexi.iteye.com/blog/2156769

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章