Spring Security Tutorial (5)----internationalization configuration and Usercache

Source: Internet
Author: User
Tags getmessage locale

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 
  1. <!--define the internationalization of messages returned by the context--
  2. <Bean id="Messagesource"
  3. class="Org.springframework.context.support.ReloadableResourceBundleMessageSource">
  4. <property name="basename"
  5. value="classpath:config/messages_zh_cn"/>
  6. </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 
    1. @Autowired
    2. Private Messagesource Messagesource;

This can be used in the class as Messagesource, Messagesource provides the following three methods

[HTML]View PlainCopy 
    1. String GetMessage (String code, object[] args, string defaultmessage, locale locale);
    2. String GetMessage (String code, object[] args, locale locale) throws nosuchmessageexception;
    3. String getMessage (messagesourceresolvable resolvable, locale locale) throws nosuchmessageexception;

For example, we define the following message in the property file

[Java]View PlainCopy 
    1. userdetails.islocked= user has been locked
    2. 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 
  1. <!--enable the user's cache function-
  2. <bean id="Usercache"
  3. class="Org.springframework.security.core.userdetails.cache.EhCacheBasedUserCache" >
  4. <property name="cache" ref="Userehcache"/>
  5. </bean>
  6. <bean id="Userehcache" class="Org.springframework.cache.ehcache.EhCacheFactoryBean" >
  7. <property name="CacheName" value="Usercache"/>
  8. <property name="CacheManager" ref="CacheManager"/>
  9. </bean>
  10. <bean id="CacheManager"
  11. class="Org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>

Ehcache.xml

[HTML]View PlainCopy 
  1. <cache
  2. name="Usercache"
  3. maxelementsinmemory="
  4. eternal="false"
  5. timetoidleseconds="
  6. timetoliveseconds="3600"
  7. overflowtodisk="true"
  8. />

Inject Ecache

[Java]View PlainCopy 
    1. @Autowired
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.