詳解Spring Boot Oauth2緩存UserDetails到Ehcache
在Spring中有一個類CachingUserDetailsService實現(xiàn)了UserDetailsService接口,該類使用靜態(tài)代理模式為UserDetailsService提供緩存功能。該類源碼如下:
CachingUserDetailsService.java
public class CachingUserDetailsService implements UserDetailsService { private UserCache userCache = new NullUserCache(); 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(username); if (user == null) { user = this.delegate.loadUserByUsername(username); } Assert.notNull(user, "UserDetailsService " + this.delegate + " returned null for username " + username + ". This is an interface contract violation"); this.userCache.putUserInCache(user); return user; } }
CachingUserDetailsService默認的userCache屬性值為new NullUserCache(),
該對象并未實現(xiàn)緩存。因為我打算使用EhCache來緩存UserDetails,所以需要使用Spring的EhCacheBasedUserCache類,該類是UserCache接口的實現(xiàn)類,主要是緩存操作。
緩存UserDetails到Ehcache的具體實現(xiàn)如下:
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 @Configuration public class UserDetailsCacheConfig { @Autowired private CustomUserDetailsService customUserDetailsService; @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 = null; 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; } }
使用
@Autowired private UserDetailsService userDetailsService;
歡迎關注我的oauthserver項目,僅僅需要運行建表sql,修改數(shù)據(jù)庫的連接配置,即可得到一個Spring Boot Oauth2 Server微服務。項目地址https://github.com/jeesun/oauthserver
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 在Mybatis中使用自定義緩存ehcache的方法
- SpringBoot2 整合Ehcache組件,輕量級緩存管理的原理解析
- Spring Boot集成Ehcache緩存解決方式
- SpringBoot中Shiro緩存使用Redis、Ehcache的方法
- 使用ehcache三步搞定springboot緩存的方法示例
- spring-boot整合ehcache實現(xiàn)緩存機制的方法
- Spring Boot緩存實戰(zhàn) EhCache示例
- Java Ehcache緩存框架入門級使用實例
- 詳解SpringBoot緩存的實例代碼(EhCache 2.x 篇)
- Spring+EHcache緩存實例詳解
- 詳解Spring MVC 集成EHCache緩存
- Java緩存ehcache的使用步驟
相關文章
解決IDEA和CMD中java命令提示錯誤: 找不到或無法加載主類的問題
這篇文章主要介紹了解決IDEA和CMD中java命令提示錯誤: 找不到或無法加載主類的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09MyBatis如何通過xml方式實現(xiàn)SaveOrUpdate
這篇文章主要講如何通過xml方式實現(xiàn)SaveOrUpdate,但是仍然建議在Service中實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-06-06MyBatis-Plus QueryWrapper及LambdaQueryWrapper的使用詳解
這篇文章主要介紹了MyBatis-Plus QueryWrapper及LambdaQueryWrapper的使用詳解,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03Java圖形化界面設計之布局管理器之BorderLayout案例詳解
這篇文章主要介紹了Java圖形化界面設計之布局管理器之BorderLayout案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08使用Feign調(diào)用注解組件(實現(xiàn)字段賦值功能)
這篇文章主要介紹了使用Feign調(diào)用注解組件(實現(xiàn)字段賦值功能),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Spring的循環(huán)依賴、三級緩存解決方案源碼詳細解析
這篇文章主要介紹了Spring的循環(huán)依賴、三級緩存解決方案源碼詳細解析,在Spring中,由于IOC的控制反轉(zhuǎn),創(chuàng)建對象不再是簡單的new出來,而是交給Spring去創(chuàng)建,會經(jīng)歷一系列Bean的生命周期才創(chuàng)建出相應的對象,需要的朋友可以參考下2024-01-01java substring(a)與substring(a,b)的使用說明
這篇文章主要介紹了java substring(a)與substring(a,b)的使用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10