patternjavaspringMinor
Spring oauth2 token store supported by redis
Viewed 0 times
springredisstoretokenoauth2supported
Problem
I had to make a demo for Spring oauth2 with redis store for tokens. Started with the sparklr2 (with tonr2) sample app from here. They are demo apps to show oauth2 powered by spring. Sparklr is the server and tonr is the client. This link has the details. Wanted to replace, the same JVM, in memory backed token store with one that uses redis. Could have used redis template directly. Decided to use spring cache, so that we can change to MemCache, Eh or other cache by change in configuration files.
Cloned the project and added a
Using
CacheTokenStore.java started of as a copy of InMemoryTokenStore. The basic solution and same methods like getTokenCount are there; commented out member variables, replaced with calls to redis.
```
package org.springframework.security.oauth.examples.sparklr.config;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.cache.DefaultRedisCachePrefix;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCachePrefix;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springf
Cloned the project and added a
CacheTokenStore, CacheTokenConfig.java and propertiesUsing
SpringCache, since redis api is more than store/ evict, used the Cache interface to talk to the Cache objects. CacheTokenStore.java started of as a copy of InMemoryTokenStore. The basic solution and same methods like getTokenCount are there; commented out member variables, replaced with calls to redis.
- Is this a good approach to use Cache interface?
- Any other code comments?
```
package org.springframework.security.oauth.examples.sparklr.config;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.cache.DefaultRedisCachePrefix;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCachePrefix;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springf
Solution
Disclaimer: I don't know squat about sparklr2, tonr2, and very little about oauth and Redis. I do have a couple of admittedly superficial remarks though.
Hardcoded 1 and 2 values
Why hardcode 1 and 2 to select between in-memory or cache as the store?
The numbers alone are meaningless,
and you could easily replace them with constants,
or put both into an
Use the enhanced for-each loop when possible
Instead of this:
Use an enhanced for-each loop:
You can simplify this:
Using
Other minor things
This is a bit unusual way to place annotations:
I'm used to this form instead:
Hardcoded 1 and 2 values
Why hardcode 1 and 2 to select between in-memory or cache as the store?
The numbers alone are meaningless,
and you could easily replace them with constants,
or put both into an
enum.Use the enhanced for-each loop when possible
Instead of this:
for (int i = 0; i < CACHE_NAMES.length; i++) {
ConcurrentMapCache cmc = new ConcurrentMapCache(CACHE_NAMES[i]);
caches.add(cmc);
}Use an enhanced for-each loop:
for (String name : CACHE_NAMES) {
ConcurrentMapCache cmc = new ConcurrentMapCache(name);
caches.add(cmc);
}Long.compareYou can simplify this:
return (diff == 0 ? 0 : ((diff < 0) ? -1 : 1));Using
Long.compare:return Long.compare(diff, 0);Other minor things
This is a bit unusual way to place annotations:
private @Value("${redis.host-name}") String redisHostName;I'm used to this form instead:
@Value("${redis.host-name}")
private String redisHostName;Code Snippets
for (int i = 0; i < CACHE_NAMES.length; i++) {
ConcurrentMapCache cmc = new ConcurrentMapCache(CACHE_NAMES[i]);
caches.add(cmc);
}for (String name : CACHE_NAMES) {
ConcurrentMapCache cmc = new ConcurrentMapCache(name);
caches.add(cmc);
}return (diff == 0 ? 0 : ((diff < 0) ? -1 : 1));return Long.compare(diff, 0);private @Value("${redis.host-name}") String redisHostName;Context
StackExchange Code Review Q#88498, answer score: 4
Revisions (0)
No revisions yet.