溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring Boot Oauth2緩存UserDetails到Ehcache的示例分析

發(fā)布時(shí)間:2021-08-30 11:41:37 來源:億速云 閱讀:117 作者:小新 欄目:編程語言

小編給大家分享一下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;

以上是“Spring Boot Oauth2緩存UserDetails到Ehcache的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI