资讯专栏INFORMATION COLUMN

Spring Boot Oauth2缓存UserDetails到Ehcache

Coding01 / 1010人阅读

摘要:在中有一个类实现了接口,该类使用静态代理模式为提供缓存功能。该类源码如下默认的属性值为,该对象并未实现缓存。缓存到的具体实现如下磁盘缓存位置使用欢迎关注我的项目,仅仅需要运行建表,修改数据库的连接配置,即可得到一个微服务。

在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


    
    

    
    
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;

欢迎关注我的oauthserver项目,仅仅需要运行建表sql,修改数据库的连接配置,即可得到一个Spring Boot Oauth2 Server微服务。项目地址https://github.com/jeesun/oauthserver

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/76623.html

相关文章

  • ApiBoot - ApiBoot Security Oauth 依赖使用文档

    摘要:如果全部使用默认值的情况话不需要做任何配置方式前提项目需要添加数据源依赖。获取通过获取启用在使用格式化时非常简单的,配置如下所示开启转换转换时所需加密,默认为恒宇少年于起宇默认不启用,签名建议进行更换。 ApiBoot是一款基于SpringBoot1.x,2.x的接口服务集成基础框架, 内部提供了框架的封装集成、使用扩展、自动化完成配置,让接口开发者可以选着性完成开箱即...

    Tonny 评论0 收藏0
  • Springboot应用缓存实践之:Ehcache加持

    摘要:但本文将讲述如何将缓存应用到应用中。这是的使用注解之一,除此之外常用的还有和,分别简单介绍一下配置在方法上表示其返回值将被加入缓存。 showImg(https://segmentfault.com/img/remote/1460000016643568); 注: 本文首发于 博客 CodeSheep · 程序羊,欢迎光临 小站!本文共 851字,阅读大约需要 3分钟 ! 本文内...

    luzhuqun 评论0 收藏0
  • SpringBoot手动使用EhCache

    摘要:的配置文件,使用前缀的属性进行配置。在方法的调用前并不会检查缓存,方法始终都会被调用。手动使用在实际开发过程中,存在不使用注解,需要自己添加缓存的情况。如果该属性值为,则表示对象可以无限期地存在于缓存中。 SpringBoot在annotation的层面实现了数据缓存的功能,基于Spring的AOP技术。所有的缓存配置只是在annotation层面配置,像声明式事务一样。 Spring...

    Hydrogen 评论0 收藏0
  • SpringBoot手动使用EhCache

    摘要:的配置文件,使用前缀的属性进行配置。在方法的调用前并不会检查缓存,方法始终都会被调用。手动使用在实际开发过程中,存在不使用注解,需要自己添加缓存的情况。如果该属性值为,则表示对象可以无限期地存在于缓存中。 SpringBoot在annotation的层面实现了数据缓存的功能,基于Spring的AOP技术。所有的缓存配置只是在annotation层面配置,像声明式事务一样。 Spring...

    魏宪会 评论0 收藏0
  • spring boot + redis

    摘要:而的缓存独立存在于我们的应用之外,我们对数据库中数据做了更新操作之后,没有通知去更新相应的内容,因此我们取到了缓存中未修改的数据,导致了数据库与缓存中数据的不一致。 redis缓存参照网址:http://blog.didispace.com/spr...项目目录D:testgitCloneSpringBoot-LearningChapter4-4-1git地址:https://gith...

    crossea 评论0 收藏0

发表评论

0条评论

Coding01

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<