在spring中有一个类cachinguserdetailsservice实现了userdetailsservice接口,该类使用静态代理模式为userdetailsservice提供缓存功能。该类源码如下:
cachinguserdetailsservice.java
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
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
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?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
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
@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;
}
}
|
使用
?
1
2
|
@autowired
private userdetailsservice userdetailsservice;
|
欢迎关注我的oauthserver项目,仅仅需要运行建表sql,修改数据库的连接配置,即可得到一个spring boot oauth2 server微服务。项目地址https://github.com/jeesun/oauthserver
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://www.cnblogs.com/rainmer/p/9417108.html
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 95
-
2025-06-04 82
-
2025-05-27 36
-
2025-06-04 69
-
2025-05-25 75
热门评论