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