Spring Boot Oauth2 Cache userdetails to Ehcache

Source: Internet
Author: User
Tags assert

There is a class cachinguserdetailsservice in spring that implements the Userdetailsservice interface, which uses the static proxy mode to provide caching functionality for Userdetailsservice. This type of source code is as follows:

Cachinguserdetailsservice.java
  public class Cachinguserdetailsservice implements Userdetailsservice {private Usercache Usercache = new Nul    Lusercache ();    Private final Userdetailsservice delegate;    Cachinguserdetailsservice (Userdetailsservice delegate) {this.delegate = delegate;    } public Usercache Getusercache () {return this.usercache;    } public void Setusercache (Usercache usercache) {this.usercache = Usercache; } public userdetails Loaduserbyusername (String username) {Userdetails user = This.userCache.getUserFromCache (US        Ername);        if (user = = null) {user = This.delegate.loadUserByUsername (username); } assert.notnull (User, "Userdetailsservice" + this.delegate + "returned NULL for username" + username + ".        This was an interface contract violation ");        This.userCache.putUserInCache (user);    return user; }}

Cachinguserdetailsservice the default Usercache property value is new NullUserCache() that the object does not implement caching. Because I'm going to use Ehcache to cache userdetails, I need to use Spring's Ehcachebasedusercache class, which is the implementation class for the Usercache interface, primarily the caching operation.

The specific implementation of cache userdetails to Ehcache is as follows:

Ehcache.xml
<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">    <!-- 磁盘缓存位置 -->    <diskStore path="java.io.tmpdir" />    <cache name="userCache"           maxElementsInMemory="0"           eternal="true"           overflowToDisk="true"           diskPersistent="true"           memoryStoreEvictionPolicy="LRU">    </cache></ehcache>
Userdetailscacheconfig.java
@Slf4j @configurationpublic class Userdetailscacheconfig {@Autowired private customuserdetailsservice Customuserdeta    Ilsservice; @Bean public Usercache Usercache () {try {ehcachebasedusercache Usercache = new Ehcachebasedusercache            ();            Val CacheManager = Cachemanager.getinstance ();            Val cache = Cachemanager.getcache ("Usercache");            Usercache.setcache (cache);        return usercache;            } catch (Exception e) {e.printstacktrace ();        Log.error (E.getmessage ());    } return null; } @Bean Public Userdetailsservice Userdetailsservice () {constructor<cachinguserdetailsservice> ctor = n        Ull        try {ctor = CachingUserDetailsService.class.getDeclaredConstructor (Userdetailsservice.class);        } catch (Nosuchmethodexception e) {e.printstacktrace ();        } Assert.notnull (ctor, "Cachinguserdetailsservice constructor is null"); Ctor.setaccEssible (TRUE);        Cachinguserdetailsservice Cachinguserdetailsservice = Beanutils.instantiateclass (ctor, CustomUserDetailsService);        Cachinguserdetailsservice.setusercache (Usercache ());    return cachinguserdetailsservice; }}
Use
@Autowiredprivate UserDetailsService userDetailsService;

Welcome to my Oauthserver project, just run the SQL build table and modify the connection configuration of the database to get a spring Boot Oauth2 server micro-service. Project Address Https://github.com/jeesun/oauthserver

Spring Boot Oauth2 Cache userdetails to Ehcache

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.