View Javadoc
1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Caches <code>User</code> objects using a Spring IoC defined <a
35   * href="http://ehcache.sourceforge.net">EHCACHE</a>.
36   * <p>Migrated from Spring Security 2 since it has been removed in Spring Security 3.</p>
37   *
38   * @author Luke Taylor
39   * @author Ben Alex
40   */
41  public class EhCacheBasedX509UserCache implements X509UserCache, InitializingBean {
42      //~ Static fields/initializers =====================================================================================
43  
44      private static final Log logger = LogFactory.getLog(EhCacheBasedX509UserCache.class);
45  
46      //~ Instance fields ================================================================================================
47  
48      private Ehcache cache;
49  
50      //~ Methods ========================================================================================================
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 }