This chapter is to explain the foundation of the following, mainly introduced under the international configuration and Usercache configuration and use
Internationalization configuration
[HTML]View PlainCopy
- <!--define the internationalization of messages returned by the context--
- <Bean id="Messagesource"
- class="Org.springframework.context.support.ReloadableResourceBundleMessageSource">
- <property name="basename"
- value="classpath:config/messages_zh_cn"/>
- </Bean>
The path to the message file is configured in the basename
International files can be found under the org.springframework.security in the Spring-security-core-3.2.0.m1.jar package and can be taken directly, and this class can also be used in projects
[Java]View PlainCopy
- @Autowired
- Private Messagesource Messagesource;
This can be used in the class as Messagesource, Messagesource provides the following three methods
[HTML]View PlainCopy
- String GetMessage (String code, object[] args, string defaultmessage, locale locale);
- String GetMessage (String code, object[] args, locale locale) throws nosuchmessageexception;
- String getMessage (messagesourceresolvable resolvable, locale locale) throws nosuchmessageexception;
For example, we define the following message in the property file
[Java]View PlainCopy
- userdetails.islocked= user has been locked
- userdetails.usernotfound= user {0} does not exist
Then use the GetMessage method
GetMessage ("userdetails.islocked", null,null)//user has been locked
GetMessage ("userdetails.islocked", New object[]{"admin"},null)//user admin does not exist
Usercache configuration, implemented by Ecahe
[Java]View PlainCopy
- <!--enable the user's cache function-
- <bean id="Usercache"
- class="Org.springframework.security.core.userdetails.cache.EhCacheBasedUserCache" >
- <property name="cache" ref="Userehcache"/>
- </bean>
- <bean id="Userehcache" class="Org.springframework.cache.ehcache.EhCacheFactoryBean" >
- <property name="CacheName" value="Usercache"/>
- <property name="CacheManager" ref="CacheManager"/>
- </bean>
- <bean id="CacheManager"
- class="Org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>
Ehcache.xml
[HTML]View PlainCopy
- <cache
- name="Usercache"
- maxelementsinmemory="
- eternal="false"
- timetoidleseconds="
- timetoliveseconds="3600"
- overflowtodisk="true"
- />
Inject Ecache
[Java]View PlainCopy
- @Autowired
- Private Usercache Usercache;
This can be done in the program by
This.userCache.getUserFromCache (username); Gets the user object in the cache
User object is Userdetails type
Spring Security Tutorial (5)----internationalization configuration and Usercache