詳解Spring Boot Oauth2緩存UserDetails到Ehcache
在Spring中有一個(gè)類CachingUserDetailsService實(shí)現(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默認(rèn)的userCache屬性值為new NullUserCache(),該對(duì)象并未實(shí)現(xiàn)緩存。因?yàn)槲掖蛩闶褂肊hCache來緩存UserDetails,所以需要使用Spring的EhCacheBasedUserCache類,該類是UserCache接口的實(shí)現(xiàn)類,主要是緩存操作。
緩存UserDetails到Ehcache的具體實(shí)現(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;
歡迎關(guān)注我的oauthserver項(xiàng)目,僅僅需要運(yùn)行建表sql,修改數(shù)據(jù)庫的連接配置,即可得到一個(gè)Spring Boot Oauth2 Server微服務(wù)。項(xiàng)目地址https://github.com/jeesun/oauthserver
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 在Mybatis中使用自定義緩存ehcache的方法
- SpringBoot2 整合Ehcache組件,輕量級(jí)緩存管理的原理解析
- Spring Boot集成Ehcache緩存解決方式
- SpringBoot中Shiro緩存使用Redis、Ehcache的方法
- 使用ehcache三步搞定springboot緩存的方法示例
- spring-boot整合ehcache實(shí)現(xiàn)緩存機(jī)制的方法
- Spring Boot緩存實(shí)戰(zhàn) EhCache示例
- Java Ehcache緩存框架入門級(jí)使用實(shí)例
- 詳解SpringBoot緩存的實(shí)例代碼(EhCache 2.x 篇)
- Spring+EHcache緩存實(shí)例詳解
- 詳解Spring MVC 集成EHCache緩存
- Java緩存ehcache的使用步驟
相關(guān)文章
SpringMVC基于阻塞隊(duì)列LinkedBlockingQueue的同步長(zhǎng)輪詢功能實(shí)現(xiàn)詳解
這篇文章主要介紹了SpringMVC基于阻塞隊(duì)列LinkedBlockingQueue的同步長(zhǎng)輪詢功能實(shí)現(xiàn)詳解,本文介紹的也是生產(chǎn)者消費(fèi)者的一種實(shí)現(xiàn),生產(chǎn)者不必是一個(gè)始終在執(zhí)行的線程,它可以是一個(gè)接口,接受客戶端的請(qǐng)求,向隊(duì)列中插入消息,需要的朋友可以參考下2023-07-07
IntelliJ IDEA本地代碼覆蓋后恢復(fù)原來的代碼圖解
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA本地代碼覆蓋后恢復(fù)原來的代碼圖解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
mybatis-plus添加數(shù)據(jù)時(shí)id自增問題及解決
這篇文章主要介紹了mybatis-plus添加數(shù)據(jù)時(shí)id自增問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
SpringBoot框架DataSource多數(shù)據(jù)源配置方式
這篇文章主要介紹了SpringBoot框架DataSource多數(shù)據(jù)源配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Springboot全局異常捕獲及try catch區(qū)別解析
這篇文章主要介紹了Springboot全局異常捕獲及try catch區(qū)別解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
關(guān)于@Value注入List,Map及設(shè)置默認(rèn)值問題
這篇文章主要介紹了@Value注入List,Map及設(shè)置默認(rèn)值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05

