1
0
mirror of synced 2026-08-05 01:36:56 +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
@@ -2,19 +2,18 @@ package org.springframework.security.core.userdetails.cache;
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.security.core.userdetails.UserCache;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.Assert;
/**
* Caches {@link UserDetails} intances in a Spring defined {@link Cache}.
* Caches {@link UserDetails} instances in a Spring defined {@link Cache}.
*
* @author Marten Deinum
* @since 3.2
*/
public class SpringCacheBasedUserCache implements UserCache, InitializingBean {
public class SpringCacheBasedUserCache implements UserCache {
//~ Static fields/initializers =====================================================================================
@@ -23,18 +22,17 @@ public class SpringCacheBasedUserCache implements UserCache, InitializingBean {
//~ Instance fields ================================================================================================
private Cache cache;
private final Cache cache;
//~ Constructors ===================================================================================================
public SpringCacheBasedUserCache(Cache cache) throws Exception {
Assert.notNull(cache, "cache mandatory");
this.cache = cache;
}
//~ Methods ========================================================================================================
public void afterPropertiesSet() throws Exception {
Assert.notNull(cache, "cache mandatory");
}
public Cache getCache() {
return cache;
}
public UserDetails getUserFromCache(String username) {
Cache.ValueWrapper element = username != null ? cache.get(username) : null;
@@ -67,8 +65,4 @@ public class SpringCacheBasedUserCache implements UserCache, InitializingBean {
public void removeUserFromCache(String username) {
cache.evict(username);
}
public void setCache(Cache cache) {
this.cache = cache;
}
}
@@ -61,9 +61,7 @@ public class SpringCacheBasedUserCacheTests {
@Test
public void cacheOperationsAreSuccessful() throws Exception {
SpringCacheBasedUserCache cache = new SpringCacheBasedUserCache();
cache.setCache(getCache());
cache.afterPropertiesSet();
SpringCacheBasedUserCache cache = new SpringCacheBasedUserCache(getCache());
// Check it gets stored in the cache
cache.putUserInCache(getUser());
@@ -80,13 +78,6 @@ public class SpringCacheBasedUserCacheTests {
@Test(expected = IllegalArgumentException.class)
public void startupDetectsMissingCache() throws Exception {
SpringCacheBasedUserCache cache = new SpringCacheBasedUserCache();
cache.afterPropertiesSet();
fail("Should have thrown IllegalArgumentException");
Cache myCache = getCache();
cache.setCache(myCache);
assertEquals(myCache, cache.getCache());
new SpringCacheBasedUserCache(null);
}
}