多平台统一管理软件接口,如何实现多平台统一管理软件接口
284
2023-01-24
详解Spring Boot Oauth2缓存UserDetails到Ehcache
在Spring中有一个类CachingUserDetailsService实现了UserDetailsService接口,该类使用静态代理模式为UserDetai
lsService提供缓存功能。该类源码如下:
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
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> maxElementsInMemory="0" eternal="true" overflowToDisk="true" diskPersistent="true" memoryStoreEvictionPolicy="LRU">
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
maxElementsInMemory="0" eternal="true" overflowToDisk="true" diskPersistent="true" memoryStoreEvictionPolicy="LRU">
maxElementsInMemory="0"
eternal="true"
overflowToDisk="true"
diskPersistent="true"
memoryStoreEvictionPolicy="LRU">
UserDetailsCacheConfig.java
@Slf4j
@Configuration
public class UserDetailsCacheConfig {
@Autowired
private CustomUserDetailsService customUserDehttp://tailsService;
@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
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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支
持我们。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~