在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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/rainmer/p/9417108.html