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