|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.providers.x509.cache; |
|
17 |
| |
|
18 |
| import org.acegisecurity.providers.x509.X509UserCache; |
|
19 |
| import org.acegisecurity.userdetails.UserDetails; |
|
20 |
| |
|
21 |
| import net.sf.ehcache.Cache; |
|
22 |
| import net.sf.ehcache.CacheException; |
|
23 |
| import net.sf.ehcache.Element; |
|
24 |
| |
|
25 |
| import org.apache.commons.logging.Log; |
|
26 |
| import org.apache.commons.logging.LogFactory; |
|
27 |
| |
|
28 |
| import org.springframework.beans.factory.InitializingBean; |
|
29 |
| |
|
30 |
| import org.springframework.dao.DataRetrievalFailureException; |
|
31 |
| |
|
32 |
| import org.springframework.util.Assert; |
|
33 |
| |
|
34 |
| import java.security.cert.X509Certificate; |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| public class EhCacheBasedX509UserCache implements X509UserCache, |
|
46 |
| InitializingBean { |
|
47 |
| |
|
48 |
| |
|
49 |
| private static final Log logger = LogFactory.getLog(EhCacheBasedX509UserCache.class); |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| private Cache cache; |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
1
| public void setCache(Cache cache) {
|
|
58 |
1
| this.cache = cache;
|
|
59 |
| } |
|
60 |
| |
|
61 |
3
| public UserDetails getUserFromCache(X509Certificate userCert) {
|
|
62 |
3
| Element element = null;
|
|
63 |
| |
|
64 |
3
| try {
|
|
65 |
3
| element = cache.get(userCert);
|
|
66 |
| } catch (CacheException cacheException) { |
|
67 |
0
| throw new DataRetrievalFailureException("Cache failure: "
|
|
68 |
| + cacheException.getMessage()); |
|
69 |
| } |
|
70 |
| |
|
71 |
3
| if (logger.isDebugEnabled()) {
|
|
72 |
0
| String subjectDN = "unknown";
|
|
73 |
| |
|
74 |
0
| if ((userCert != null) && (userCert.getSubjectDN() != null)) {
|
|
75 |
0
| subjectDN = userCert.getSubjectDN().toString();
|
|
76 |
| } |
|
77 |
| |
|
78 |
0
| logger.debug("X.509 Cache hit. SubjectDN: " + subjectDN);
|
|
79 |
| } |
|
80 |
| |
|
81 |
3
| if (element == null) {
|
|
82 |
2
| return null;
|
|
83 |
| } else { |
|
84 |
1
| return (UserDetails) element.getValue();
|
|
85 |
| } |
|
86 |
| } |
|
87 |
| |
|
88 |
1
| public void afterPropertiesSet() throws Exception {
|
|
89 |
1
| Assert.notNull(cache, "cache is mandatory");
|
|
90 |
| } |
|
91 |
| |
|
92 |
1
| public void putUserInCache(X509Certificate userCert, UserDetails user) {
|
|
93 |
1
| Element element = new Element(userCert, user);
|
|
94 |
| |
|
95 |
1
| if (logger.isDebugEnabled()) {
|
|
96 |
0
| logger.debug("Cache put: " + userCert.getSubjectDN());
|
|
97 |
| } |
|
98 |
| |
|
99 |
1
| cache.put(element);
|
|
100 |
| } |
|
101 |
| |
|
102 |
1
| public void removeUserFromCache(X509Certificate userCert) {
|
|
103 |
1
| if (logger.isDebugEnabled()) {
|
|
104 |
0
| logger.debug("Cache remove: " + userCert.getSubjectDN());
|
|
105 |
| } |
|
106 |
| |
|
107 |
1
| cache.remove(userCert);
|
|
108 |
| } |
|
109 |
| } |