Skip to content

Commit 40c8447

Browse files
committed
SWS-1033 - Introduce Spring-based X.509 cache support
Deprecate EhCache-based X.509 caching and provide a path to migrate toward Spring-based caching.
1 parent fdf638a commit 40c8447

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

spring-ws-security/src/main/java/org/springframework/ws/soap/security/x509/cache/EhCacheBasedX509UserCache.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@
3737
*
3838
* @author Luke Taylor
3939
* @author Ben Alex
40+
* @author Greg Turnquist
41+
*
42+
* @deprecated Migrate to {@link SpringBasedX509UserCache} and inject a platform neutral Spring-based {@link org.springframework.cache.Cache}.
4043
*/
44+
@Deprecated
4145
public class EhCacheBasedX509UserCache implements X509UserCache, InitializingBean {
4246
//~ Static fields/initializers =====================================================================================
4347

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2005-2014 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.apache.commons.logging.Log;
22+
import org.apache.commons.logging.LogFactory;
23+
24+
import org.springframework.beans.factory.InitializingBean;
25+
import org.springframework.cache.Cache;
26+
import org.springframework.security.core.userdetails.UserDetails;
27+
import org.springframework.util.Assert;
28+
29+
30+
/**
31+
* Caches {@code User} objects using a Spring Framework-based {@link Cache}.
32+
*
33+
* <p>Migrated from Spring Security 2 since it has been removed in Spring Security 3.</p>
34+
*
35+
* @author Luke Taylor
36+
* @author Ben Alex
37+
* @author Greg Turnquist
38+
*/
39+
public class SpringBasedX509UserCache implements X509UserCache, InitializingBean {
40+
41+
private static final Log logger = LogFactory.getLog(SpringBasedX509UserCache.class);
42+
43+
private Cache cache;
44+
45+
@Override
46+
public void afterPropertiesSet() throws Exception {
47+
Assert.notNull(cache, "cache is mandatory");
48+
}
49+
50+
@Override
51+
public UserDetails getUserFromCache(X509Certificate userCert) {
52+
53+
if (logger.isDebugEnabled()) {
54+
55+
String subjectDN = "unknown";
56+
57+
if ((userCert != null) && (userCert.getSubjectDN() != null)) {
58+
subjectDN = userCert.getSubjectDN().toString();
59+
}
60+
61+
logger.debug("X.509 Cache hit. SubjectDN: " + subjectDN);
62+
}
63+
64+
return cache.get(userCert, UserDetails.class);
65+
}
66+
67+
@Override
68+
public void putUserInCache(X509Certificate userCert, UserDetails user) {
69+
70+
if (logger.isDebugEnabled()) {
71+
logger.debug("Cache put: " + userCert.getSubjectDN());
72+
}
73+
74+
cache.put(userCert, user);
75+
}
76+
77+
@Override
78+
public void removeUserFromCache(X509Certificate userCert) {
79+
80+
if (logger.isDebugEnabled()) {
81+
logger.debug("Cache remove: " + userCert.getSubjectDN());
82+
}
83+
84+
cache.evict(userCert);
85+
}
86+
87+
public void setCache(Cache cache) {
88+
this.cache = cache;
89+
}
90+
}

0 commit comments

Comments
 (0)