在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
@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;

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

Spring Boot Oauth2缓存UserDetails到Ehcache的更多相关文章

  1. spring boot redis缓存JedisPool使用

    spring boot redis缓存JedisPool使用 添加依赖pom.xml中添加如下依赖 <!-- Spring Boot Redis --> <dependency> ...

  2. thymeltesys-基于Spring Boot Oauth2的扫码登录框架

    thymeltesys thymelte是一个基于Spring Boot Oauth2的扫码登录框架,使用PostgreSQL存储数据,之后会慢慢支持其他关系型数据库.即使你不使用整个框架,只使用其中 ...

  3. 3行代码快速实现Spring Boot Oauth2服务

    这里的3行代码并不是指真的只需要写3行代码,而是基于我已经写好的一个Spring Boot Oauth2服务.仅仅需要修改3行数据库配置信息,即可得到一个Spring Boot Oauth2服务. 项 ...

  4. spring boot redis 缓存(cache)集成

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  5. Spring Boot 数据缓存 - EhCache

    EhCache 集成 EhCache 是一个纯 Java 的进程内缓存框架,具有快速.精干等特点,是 Hibernate 中默认的 CacheProvider. 在 Spring Boot 中集成 E ...

  6. spring boot oauth2的一些记录

    oauth2及时从一个项目A申请另一个项目B的访问的时候,不用在项目A输入项目B的用户名和密码,个人理解先跳转到项目B,利用项目B的用户名和密码得到一个code之类的,这里有点像openID,不过不是 ...

  7. spring boot redis缓存入门

    摘要: 原创出处 泥瓦匠BYSocket 下载工程 springboot-learning-example ,工程代码注解很详细.JeffLi1993/springboot-learning-exam ...

  8. spring Boot 学习(二、Spring Boot与缓存)

    一.概述1. 大多应用中,可通过消息服务中间件来提升系统异步通信.扩展解耦能力 2. 消息服务中两个重要概念: 消息代理(message broker)和目的地(destination) 当消息发送者 ...

  9. Spring Boot与缓存

    ---恢复内容开始--- JSR-107.Spring缓存抽象.整合Redis 一.JSR107 Java Caching定义了5个核心接口,分别是CachingProvider, CacheMana ...

随机推荐

  1. Android开发 - 掌握ConstraintLayout(一)传统布局的问题

    在传统的Android开发中,页面布局占用了我们很多的开发时间,而且面对复杂页面的时候,传统的一些布局会显得非常复杂,每种布局都有特定的应用场景,我们通常需要各种布局结合起来使用来实现复杂的页面.随着 ...

  2. JAVA学习路线——匹马行天下

  3. spring boot 下 thymeleaf 配置

    1. thymeleaf 配置参数 [参考文章]:spring-boot-starter-thymeleaf 避坑指南 #<!-- 关闭thymeleaf缓存 开发时使用 否则没有实时画面--& ...

  4. 请求报错:“应以Content-Type: application/x-www-form-urlencoded为请求类型,在form表单中提交登录信息。"

    竟然是post 方法少了参数 // // 摘要: // 以异步操作将 POST 请求发送给指定 URI. // // 参数: // requestUri: // 请求发送到的 URI. // // c ...

  5. Spring 声明事务中transactionAttributes属性 + - Exception 实现逻辑

    下面是一段典型的Spring 声明事务的配置: <bean id=“baseTxProxy” lazy-init=“true”class=“org.springframework.transac ...

  6. docker进阶篇(一) ---- Volume(数据卷)

    引言 docker的镜像是由多个只读的文件系统叠加在一起形成的.当我们在我启动一个容器的时候,docker会加载这些只读层并在这些只读层的上面(栈顶)增加一个读写层.这时如果修改正在运行的容器中已有的 ...

  7. 在线画UML图的工具

    工作需要在线画各种UML图,类图.协作图.用例图等等,调查了一些在线画UML图的工具,有的做的很好但要收费,例如:http://www.gliffy.com/,发现现在免费好用的是ProcessOn: ...

  8. 从零开始学 Web 之 ES6(六)ES6基础语法四

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...

  9. resin4.0.25 安装配置 及结合eclipse开发

    resin4.0.25 安装配置 及结合eclipse开发 本文大部分内容是对官网的翻译,及自己配置后的一些体会. 一.  基于win  ,resin基本安装1,安装jdk1.6或更高版本2,配置环境 ...

  10. VM虚拟机扩展硬盘容量

    VM虚拟机扩展硬盘容量 第一步,关闭系统,给虚拟机硬盘增加空间. 第二步,启动系统.查看硬盘大小和分区情况. 第三步,分区. 第四步,格式化分区. 第五步,挂载. 第六步,开机自动挂载. 第一步: 当 ...