1
0
mirror of synced 2026-08-06 10:18:52 +00:00

SEC-2114: Polishing Spring Based Cache

This commit is contained in:
Rob Winch
2013-01-04 11:12:08 -06:00
parent 01ea39ce35
commit 6b81f97081
6 changed files with 41 additions and 121 deletions
@@ -17,7 +17,6 @@ package org.springframework.security.cas.authentication;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.cache.Cache;
import org.springframework.util.Assert;
@@ -29,21 +28,24 @@ import org.springframework.util.Assert;
* @since 3.2
*
*/
public class SpringCacheBasedTicketCache implements StatelessTicketCache, InitializingBean {
public class SpringCacheBasedTicketCache implements StatelessTicketCache {
//~ Static fields/initializers =====================================================================================
private static final Log logger = LogFactory.getLog(SpringCacheBasedTicketCache.class);
//~ Instance fields ================================================================================================
private Cache cache;
private final Cache cache;
//~ Constructors ===================================================================================================
public SpringCacheBasedTicketCache(Cache cache) throws Exception {
Assert.notNull(cache, "cache mandatory");
this.cache = cache;
}
//~ Methods ========================================================================================================
public void afterPropertiesSet() throws Exception {
Assert.notNull(cache, "cache mandatory");
}
public CasAuthenticationToken getByTicketId(final String serviceTicket) {
final Cache.ValueWrapper element = serviceTicket != null ? cache.get(serviceTicket) : null;
@@ -54,10 +56,6 @@ public class SpringCacheBasedTicketCache implements StatelessTicketCache, Initia
return element == null ? null : (CasAuthenticationToken) element.get();
}
public Cache getCache() {
return cache;
}
public void putTicketInCache(final CasAuthenticationToken token) {
String key = token.getCredentials().toString();
@@ -79,8 +77,4 @@ public class SpringCacheBasedTicketCache implements StatelessTicketCache, Initia
public void removeTicketFromCache(final String serviceTicket) {
cache.evict(serviceTicket);
}
public void setCache(final Cache cache) {
this.cache = cache;
}
}
@@ -15,10 +15,8 @@
package org.springframework.security.cas.authentication;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
@@ -35,6 +33,7 @@ public class SpringCacheBasedTicketCacheTests extends AbstractStatelessTicketCac
private static CacheManager cacheManager;
//~ Methods ========================================================================================================
@BeforeClass
public static void initCacheManaer() {
cacheManager = new ConcurrentMapCacheManager();
@@ -43,9 +42,7 @@ public class SpringCacheBasedTicketCacheTests extends AbstractStatelessTicketCac
@Test
public void testCacheOperation() throws Exception {
SpringCacheBasedTicketCache cache = new SpringCacheBasedTicketCache();
cache.setCache(cacheManager.getCache("castickets"));
cache.afterPropertiesSet();
SpringCacheBasedTicketCache cache = new SpringCacheBasedTicketCache(cacheManager.getCache("castickets"));
final CasAuthenticationToken token = getToken();
@@ -62,19 +59,8 @@ public class SpringCacheBasedTicketCacheTests extends AbstractStatelessTicketCac
assertNull(cache.getByTicketId("UNKNOWN_SERVICE_TICKET"));
}
@Test
@Test(expected = IllegalArgumentException.class)
public void testStartupDetectsMissingCache() throws Exception {
SpringCacheBasedTicketCache cache = new SpringCacheBasedTicketCache();
try {
cache.afterPropertiesSet();
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
Cache myCache = cacheManager.getCache("castickets");
cache.setCache(myCache);
assertEquals(myCache, cache.getCache());
new SpringCacheBasedTicketCache(null);
}
}