第三章 支撑体系
本章我们将介绍Acegi Security用到的辅助架构和支撑架构。Acegi Security具有的那些不是直接和安全相关的功能将在讨论。
3.1. Localization(本地化)
Acegi Security 支持本地化的异常信息,终端用户显然会更喜欢看到这些本地化的信息。如果你的应用程序是针对英文用户开发的,那么恭喜你,你不用做任何的工作,默认情况下Acegi Security信息都是英文的。如果你要支持其他的本地化特性,那么你需要了解这章节所有的信息了。所有的异常信息都可以本地化的,包括验证失败、访问被拒。开发者或者系统配置人员所关心的异常和日志(包括错误属性、interface contract violations, 错误使用构造方法、启动时候的验证、debug-level日志)等是不能本地化的,这些是用英文被硬编码在Acegi Security的代码中了。打开acegi-security-xx.jar,在org.acegisecurity包下面可以找到messages.properties文件。
This should be referred to by your ApplicationContext, as Acegi Security classes implement Spring's MessageSourceAware interface and expect the message resolver to be dependency injected at application context startup time. Usually all you need to do is register a bean inside your application context to refer to the messages. An example is shown below:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename"><value>org/acegisecurity/messages</value></property>
</bean>
The messages.properties is named in accordance with standard resource bundles and represents the
default language supported by Acegi Securtiy messages. This default file is in English. If you do not
register a message source, Acegi Security will still work correctly and fallback to hard-coded English
versions of the messages.
If you wish to customize the messages.properties file, or support other languages, you should copy the
file, rename it accordingly, and register it inside the above bean definition. There are not a large
number of message keys inside this file, so localization should not be considered a major initiative. If
you do perform localization of this file, please consider sharing your work with the community by
logging a JIRA task and attaching your appropriately-named localized version of messages.properties.
Rounding out the discussion on localization is the Spring ThreadLocal known as
org.springframework.context.i18n.LocaleContextHolder. You should set the LocaleContextHolder to
represent the preferred Locale of each user. Acegi Security will attempt to locate a message from the
message source using the Locale obtained from this ThreadLocal. Please refer to Springdocumentation
for further details on using LocaleContextHolder and the helper classes that can automatically set it for
you (eg AcceptHeaderLocaleResolver, CookieLocaleResolver, FixedLocaleResolver, SessionLocaleResolver
etc)
//本地化实在是个好东西,只是现在工作用不到,所以留在后面翻译了。