亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

Spring Boot Oauth2緩存UserDetails到Ehcache

Coding01 / 1193人閱讀

摘要:在中有一個類實現(xiàn)了接口,該類使用靜態(tài)代理模式為提供緩存功能。該類源碼如下默認的屬性值為,該對象并未實現(xiàn)緩存。緩存到的具體實現(xiàn)如下磁盤緩存位置使用歡迎關(guān)注我的項目,僅僅需要運行建表,修改數(shù)據(jù)庫的連接配置,即可得到一個微服務(wù)。

在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


    
    

    
    
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 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項目,僅僅需要運行建表sql,修改數(shù)據(jù)庫的連接配置,即可得到一個Spring Boot Oauth2 Server微服務(wù)。項目地址https://github.com/jeesun/oauthserver

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/76623.html

相關(guān)文章

  • ApiBoot - ApiBoot Security Oauth 依賴使用文檔

    摘要:如果全部使用默認值的情況話不需要做任何配置方式前提項目需要添加數(shù)據(jù)源依賴。獲取通過獲取啟用在使用格式化時非常簡單的,配置如下所示開啟轉(zhuǎn)換轉(zhuǎn)換時所需加密,默認為恒宇少年于起宇默認不啟用,簽名建議進行更換。 ApiBoot是一款基于SpringBoot1.x,2.x的接口服務(wù)集成基礎(chǔ)框架, 內(nèi)部提供了框架的封裝集成、使用擴展、自動化完成配置,讓接口開發(fā)者可以選著性完成開箱即...

    Tonny 評論0 收藏0
  • Springboot應(yīng)用緩存實踐之:Ehcache加持

    摘要:但本文將講述如何將緩存應(yīng)用到應(yīng)用中。這是的使用注解之一,除此之外常用的還有和,分別簡單介紹一下配置在方法上表示其返回值將被加入緩存。 showImg(https://segmentfault.com/img/remote/1460000016643568); 注: 本文首發(fā)于 博客 CodeSheep · 程序羊,歡迎光臨 小站!本文共 851字,閱讀大約需要 3分鐘 ! 本文內(nèi)...

    luzhuqun 評論0 收藏0
  • SpringBoot手動使用EhCache

    摘要:的配置文件,使用前綴的屬性進行配置。在方法的調(diào)用前并不會檢查緩存,方法始終都會被調(diào)用。手動使用在實際開發(fā)過程中,存在不使用注解,需要自己添加緩存的情況。如果該屬性值為,則表示對象可以無限期地存在于緩存中。 SpringBoot在annotation的層面實現(xiàn)了數(shù)據(jù)緩存的功能,基于Spring的AOP技術(shù)。所有的緩存配置只是在annotation層面配置,像聲明式事務(wù)一樣。 Spring...

    Hydrogen 評論0 收藏0
  • SpringBoot手動使用EhCache

    摘要:的配置文件,使用前綴的屬性進行配置。在方法的調(diào)用前并不會檢查緩存,方法始終都會被調(diào)用。手動使用在實際開發(fā)過程中,存在不使用注解,需要自己添加緩存的情況。如果該屬性值為,則表示對象可以無限期地存在于緩存中。 SpringBoot在annotation的層面實現(xiàn)了數(shù)據(jù)緩存的功能,基于Spring的AOP技術(shù)。所有的緩存配置只是在annotation層面配置,像聲明式事務(wù)一樣。 Spring...

    魏憲會 評論0 收藏0
  • spring boot + redis

    摘要:而的緩存獨立存在于我們的應(yīng)用之外,我們對數(shù)據(jù)庫中數(shù)據(jù)做了更新操作之后,沒有通知去更新相應(yīng)的內(nèi)容,因此我們?nèi)〉搅司彺嬷形葱薷牡臄?shù)據(jù),導(dǎo)致了數(shù)據(jù)庫與緩存中數(shù)據(jù)的不一致。 redis緩存參照網(wǎng)址:http://blog.didispace.com/spr...項目目錄D:testgitCloneSpringBoot-LearningChapter4-4-1git地址:https://gith...

    crossea 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<