国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

詳解Spring Boot Oauth2緩存UserDetails到Ehcache

2024-07-14 08:41:58
字體:
來源:轉載
供稿:網友

在Spring中有一個類CachingUserDetailsService實現了UserDetailsService接口,該類使用靜態代理模式為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(),該對象并未實現緩存。因為我打算使用EhCache來緩存UserDetails,所以需要使用Spring的EhCacheBasedUserCache類,該類是UserCache接口的實現類,主要是緩存操作。

緩存UserDetails到Ehcache的具體實現如下:

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 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;  }}

使用

@Autowiredprivate UserDetailsService userDetailsService;

歡迎關注我的oauthserver項目,僅僅需要運行建表sql,修改數據庫的連接配置,即可得到一個Spring Boot Oauth2 Server微服務。項目地址https://github.com/jeesun/oauthserver

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蒙城县| 鄯善县| 武汉市| 九江市| 个旧市| 尖扎县| 汶川县| 西华县| 南丹县| 通道| 和龙市| 横山县| 隆林| 璧山县| 霍城县| 宁陕县| 辰溪县| 楚雄市| 武夷山市| 贵州省| 饶平县| 杭锦旗| 嫩江县| 积石山| 土默特右旗| 通化县| 苍山县| 朝阳区| 广饶县| 陆丰市| 绥阳县| 仙游县| 泌阳县| 孙吴县| 和田县| 栖霞市| 黄浦区| 台前县| 宜兰市| 无锡市| 克什克腾旗|