我们知到授权服颁发Token我们可以存储在授权服中,也可以存储在MySQL中,当然也可以存储在Redis中,这个我们是通过TokenStore来实现的,这个我这里就不做重点介绍了,但是授权服中不止给第三方办法Token,当我们在做授权码认证的时候我们还向客户端颁发了一个授权码!那么AuthorizationCodeServices对象就是关于这个授权码颁发后怎么存储的对象!
1、AuthorizationCodeServices
public interface AuthorizationCodeServices {
//为指定的身份验证创建授权代码。
String createAuthorizationCode(OAuth2Authentication authentication);
//使用授权码。
OAuth2Authentication consumeAuthorizationCode(String code)throws InvalidGrantException;
}
2、RandomValueAuthorizationCodeServices
我们自定义的AuthorizationCodeServices基本上都继承这个类
public abstract class RandomValueAuthorizationCodeServices implements AuthorizationCodeServices {
private RandomValueStringGenerator generator = new RandomValueStringGenerator();
public String createAuthorizationCode(OAuth2Authentication authentication) {
String code = generator.generate();
store(code, authentication);
return code;
}
public OAuth2Authentication consumeAuthorizationCode(String code) throws InvalidGrantException {
OAuth2Authentication auth = this.remove(code);
if (auth == null) {throw new InvalidGrantException("Invalid authorization code: " + code);}
return auth;
}
protected abstract void store(String code, OAuth2Authentication authentication);
protected abstract OAuth2Authentication remove(String code);
}
RandomValueAuthorizationCodeServices为我们指定了授权码的生成方式,同时也开放了授权码的存储和删除。这为我们后面AuthorizationCodeServices的自定义提供了方便,因为我们只要继承RandomValueAuthorizationCodeServices,实现他的store和remove方法就行了。
RandomValueAuthorizationCodeServices有2个子类InMemoryAuthorizationCodeServices和JdbcAuthorizationCodeServices,一个是把授权码存储到内存中,另一个是把授权码存储到数据库中。
3、AuthorizationCodeServices的注册逻辑
@Configuration
@Import(TokenKeyEndpointRegistrar.class)
public class AuthorizationServerEndpointsConfiguration {
private AuthorizationServerEndpointsConfigurer endpoints = new AuthorizationServerEndpointsConfigurer();
//忽略代码....
@Bean
public AuthorizationEndpoint authorizationEndpoint() throws Exception {
AuthorizationEndpoint authorizationEndpoint = new AuthorizationEndpoint();
FrameworkEndpointHandlerMapping mapping = getEndpointsConfigurer().getFrameworkEndpointHandlerMapping();
authorizationEndpoint.setUserApprovalPage(extractPath(mapping, "/oauth/confirm_access"));
authorizationEndpoint.setProviderExceptionHandler(exceptionTranslator());
authorizationEndpoint.setErrorPage(extractPath(mapping, "/oauth/error"));
authorizationEndpoint.setTokenGranter(tokenGranter());
authorizationEndpoint.setClientDetailsService(clientDetailsService);
//默认的AuthorizationCodeServices在这里注册
//底层调用的是AuthorizationServerEndpointsConfigurer的authorizationCodeServices()方法
authorizationEndpoint.setAuthorizationCodeServices(authorizationCodeServices());
authorizationEndpoint.setOAuth2RequestFactory(oauth2RequestFactory());
authorizationEndpoint.setOAuth2RequestValidator(oauth2RequestValidator());
authorizationEndpoint.setUserApprovalHandler(userApprovalHandler());
authorizationEndpoint.setRedirectResolver(redirectResolver());
return authorizationEndpoint;
}
//忽略代码....
}
AuthorizationServerEndpointsConfigurer的authorizationCodeServices方法
public final class AuthorizationServerEndpointsConfigurer {
private AuthorizationCodeServices authorizationCodeServices() {
if (authorizationCodeServices == null) {
authorizationCodeServices = new InMemoryAuthorizationCodeServices();
}
return authorizationCodeServices;
}
}
如果我们没有注册AuthorizationCodeServices,那么默认注册InMemoryAuthorizationCodeServices
4、把授权码存储到Redis中
需要导入的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
相关配置
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.database=0
Redis配置类
@Configuration
public class RedisConfiguration {
/**
* 注入 RedisConnectionFactory
*/
@Autowired
private RedisConnectionFactory redisConnectionFactory;
/**
* 实例化 RedisTemplate 对象
*/
@Bean
public RedisTemplate<String, Object> functionDomainRedisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
initDomainRedisTemplate(redisTemplate, redisConnectionFactory);
return redisTemplate;
}
/**
* 设置数据存入 redis 的序列化方式
*/
private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setConnectionFactory(factory);
}
}
基于RedisTemplate实现
@Service
public class RedisRepository {
/**
* Spring Redis Template
*/
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public RedisRepository(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
/**
* 获取链接工厂
*/
public RedisConnectionFactory getConnectionFactory() {
return this.redisTemplate.getConnectionFactory();
}
/**
* 获取 RedisTemplate对象
*/
public RedisTemplate<String, Object> getRedisTemplate() {
return redisTemplate;
}
/**
* 清空DB
*
* @param node redis 节点
*/
public void flushDB(RedisClusterNode node) {
this.redisTemplate.opsForCluster().flushDb(node);
}
/**
* 添加到带有 过期时间的 缓存
*
* @param key redis主键
* @param value 值
* @param time 过期时间(单位秒)
*/
public void setExpire(final byte[] key, final byte[] value, final long time) {
redisTemplate.execute((RedisCallback<Long>) connection -> {
connection.setEx(key, time, value);
return 1L;
});
}
/**
* 添加到带有 过期时间的 缓存
*
* @param key redis主键
* @param value 值
* @param time 过期时间
* @param timeUnit 过期时间单位
*/
public void setExpire(final String key, final Object value, final long time, final TimeUnit timeUnit) {
redisTemplate.opsForValue().set(key, value, time, timeUnit);
}
public void setExpire(final String key, final Object value, final long time) {
this.setExpire(key, value, time, TimeUnit.SECONDS);
}
public void setExpire(final String key, final Object value, final long time, final TimeUnit timeUnit, RedisSerializer<Object> valueSerializer) {
byte[] rawKey = rawKey(key);
byte[] rawValue = rawValue(value, valueSerializer);
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
potentiallyUsePsetEx(connection);
return null;
}
public void potentiallyUsePsetEx(RedisConnection connection) {
if (!TimeUnit.MILLISECONDS.equals(timeUnit) || !failsafeInvokePsetEx(connection)) {
connection.setEx(rawKey, TimeoutUtils.toSeconds(time, timeUnit), rawValue);
}
}
private boolean failsafeInvokePsetEx(RedisConnection connection) {
boolean failed = false;
try {
connection.pSetEx(rawKey, time, rawValue);
} catch (UnsupportedOperationException e) {
failed = true;
}
return !failed;
}
}, true);
}
/**
* 一次性添加数组到 过期时间的 缓存,不用多次连接,节省开销
*
* @param keys redis主键数组
* @param values 值数组
* @param time 过期时间(单位秒)
*/
public void setExpire(final String[] keys, final Object[] values, final long time) {
for (int i = 0; i < keys.length; i++) {
redisTemplate.opsForValue().set(keys[i], values[i], time, TimeUnit.SECONDS);
}
}
/**
* 一次性添加数组到 过期时间的 缓存,不用多次连接,节省开销
*
* @param keys the keys
* @param values the values
*/
public void set(final String[] keys, final Object[] values) {
for (int i = 0; i < keys.length; i++) {
redisTemplate.opsForValue().set(keys[i], values[i]);
}
}
/**
* 添加到缓存
*
* @param key the key
* @param value the value
*/
public void set(final String key, final Object value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 查询在以keyPatten的所有 key
*
* @param keyPatten the key patten
* @return the set
*/
public Set<String> keys(final String keyPatten) {
return redisTemplate.keys(keyPatten + "*");
}
/**
* 根据key获取对象
*
* @param key the key
* @return the byte [ ]
*/
public byte[] get(final byte[] key) {
return redisTemplate.execute((RedisCallback<byte[]>) connection -> connection.get(key));
}
/**
* 根据key获取对象
*
* @param key the key
* @return the string
*/
public Object get(final String key) {
return redisTemplate.opsForValue().get(key);
}
/**
* 根据key获取对象
*
* @param key the key
* @param valueSerializer 序列化
* @return the string
*/
public Object get(final String key, RedisSerializer<Object> valueSerializer) {
byte[] rawKey = rawKey(key);
return redisTemplate.execute(connection -> deserializeValue(connection.get(rawKey), valueSerializer), true);
}
/**
* Ops for hash hash operations.
*
* @return the hash operations
*/
public HashOperations<String, String, Object> opsForHash() {
return redisTemplate.opsForHash();
}
/**
* 对HashMap操作
*
* @param key the key
* @param hashKey the hash key
* @param hashValue the hash value
*/
public void putHashValue(String key, String hashKey, Object hashValue) {
opsForHash().put(key, hashKey, hashValue);
}
/**
* 获取单个field对应的值
*
* @param key the key
* @param hashKey the hash key
* @return the hash values
*/
public Object getHashValues(String key, String hashKey) {
return opsForHash().get(key, hashKey);
}
/**
* 根据key值删除
*
* @param key the key
* @param hashKeys the hash keys
*/
public void delHashValues(String key, Object... hashKeys) {
opsForHash().delete(key, hashKeys);
}
/**
* key只匹配map
*
* @param key the key
* @return the hash value
*/
public Map<String, Object> getHashValue(String key) {
return opsForHash().entries(key);
}
/**
* 批量添加
*
* @param key the key
* @param map the map
*/
public void putHashValues(String key, Map<String, Object> map) {
opsForHash().putAll(key, map);
}
/**
* 集合数量
*
* @return the long
*/
public long dbSize() {
return redisTemplate.execute(RedisServerCommands::dbSize);
}
/**
* 清空redis存储的数据
*
* @return the string
*/
public String flushDB() {
return redisTemplate.execute((RedisCallback<String>) connection -> {
connection.flushDb();
return "ok";
});
}
/**
* 判断某个主键是否存在
*
* @param key the key
* @return the boolean
*/
public boolean exists(final String key) {
return redisTemplate.hasKey(key);
}
/**
* 删除key
*
* @param keys the keys
* @return the long
*/
public boolean del(final String... keys) {
boolean result = false;
for (String key : keys) {
result = redisTemplate.delete(key);
}
return result;
}
/**
* 对某个主键对应的值加一,value值必须是全数字的字符串
*
* @param key the key
* @return the long
*/
public long incr(final String key) {
return redisTemplate.opsForValue().increment(key);
}
/**
* redis List 引擎
*
* @return the list operations
*/
public ListOperations<String, Object> opsForList() {
return redisTemplate.opsForList();
}
/**
* redis List数据结构 : 将一个或多个值 value 插入到列表 key 的表头
*
* @param key the key
* @param value the value
* @return the long
*/
public Long leftPush(String key, Object value) {
return opsForList().leftPush(key, value);
}
/**
* redis List数据结构 : 移除并返回列表 key 的头元素
*
* @param key the key
* @return the string
*/
public Object leftPop(String key) {
return opsForList().leftPop(key);
}
/**
* redis List数据结构 :将一个或多个值 value 插入到列表 key 的表尾(最右边)。
*
* @param key the key
* @param value the value
* @return the long
*/
public Long in(String key, Object value) {
return opsForList().rightPush(key, value);
}
/**
* redis List数据结构 : 移除并返回列表 key 的末尾元素
*
* @param key the key
* @return the string
*/
public Object rightPop(String key) {
return opsForList().rightPop(key);
}
/**
* redis List数据结构 : 返回列表 key 的长度 ; 如果 key 不存在,则 key 被解释为一个空列表,返回 0 ; 如果 key 不是列表类型,返回一个错误。
*
* @param key the key
* @return the long
*/
public Long length(String key) {
return opsForList().size(key);
}
/**
* redis List数据结构 : 根据参数 i 的值,移除列表中与参数 value 相等的元素
*
* @param key the key
* @param i the
* @param value the value
*/
public void remove(String key, long i, Object value) {
opsForList().remove(key, i, value);
}
/**
* redis List数据结构 : 将列表 key 下标为 index 的元素的值设置为 value
*
* @param key the key
* @param index the index
* @param value the value
*/
public void set(String key, long index, Object value) {
opsForList().set(key, index, value);
}
/**
* redis List数据结构 : 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 end 指定。
*
* @param key the key
* @param start the start
* @param end the end
* @return the list
*/
public List<Object> getList(String key, int start, int end) {
return opsForList().range(key, start, end);
}
/**
* redis List数据结构 : 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 end 指定。
*
* @param key the key
* @param start the start
* @param end the end
* @param valueSerializer 序列化
* @return the list
*/
public List<Object> getList(String key, int start, int end, RedisSerializer<Object> valueSerializer) {
byte[] rawKey = rawKey(key);
return redisTemplate.execute(connection -> deserializeValues(connection.lRange(rawKey, start, end), valueSerializer), true);
}
/**
* redis List数据结构 : 批量存储
*
* @param key the key
* @param list the list
* @return the long
*/
public Long leftPushAll(String key, List<String> list) {
return opsForList().leftPushAll(key, list);
}
/**
* redis List数据结构 : 将值 value 插入到列表 key 当中,位于值 index 之前或之后,默认之后。
*
* @param key the key
* @param index the index
* @param value the value
*/
public void insert(String key, long index, Object value) {
opsForList().set(key, index, value);
}
private byte[] rawKey(Object key) {
Assert.notNull(key, "non null key required");
if (key instanceof byte[]) {
return (byte[]) key;
}
RedisSerializer<Object> redisSerializer = (RedisSerializer<Object>) redisTemplate.getKeySerializer();
return redisSerializer.serialize(key);
}
private byte[] rawValue(Object value, RedisSerializer valueSerializer) {
if (value instanceof byte[]) {
return (byte[]) value;
}
return valueSerializer.serialize(value);
}
private List deserializeValues(List<byte[]> rawValues, RedisSerializer<Object> valueSerializer) {
if (valueSerializer == null) {
return rawValues;
}
return SerializationUtils.deserialize(rawValues, valueSerializer);
}
private Object deserializeValue(byte[] value, RedisSerializer<Object> valueSerializer) {
if (valueSerializer == null) {
return value;
}
return valueSerializer.deserialize(value);
}
}
基于redis存储的RedisAuthorizationCodeServices
@Service
public class RedisAuthorizationCodeServices extends RandomValueAuthorizationCodeServices {
String AUTH_CODE_PREFIX = "yiyi:code:auth:";
private final RedisRepository redisRepository;
private final RedisSerializer<Object> valueSerializer;
public RedisAuthorizationCodeServices(RedisRepository redisRepository) {
this.redisRepository = redisRepository;
this.valueSerializer = RedisSerializer.java();
}
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 将存储code到redis,并设置过期时间,10分钟
*/
@Override
protected void store(String code, OAuth2Authentication authentication) {
redisRepository.setExpire(redisKey(code), authentication, 10, TimeUnit.MINUTES, valueSerializer);
}
@Override
protected OAuth2Authentication remove(final String code) {
String codeKey = redisKey(code);
OAuth2Authentication token = (OAuth2Authentication) redisRepository.get(codeKey, valueSerializer);
redisRepository.del(codeKey);
return token;
}
/**
* redis中 code key的前缀
*/
private String redisKey(String code) {
return AUTH_CODE_PREFIX + code;
}
}
从新注入AuthorizationCodeServices为RandomValueAuthorizationCodeServices
@Configuration
@EnableAuthorizationServer
public class Authorizationservercontig2 extends AuthorizationServerConfigurerAdapter {
//忽略代码.....
@Autowired
private RandomValueAuthorizationCodeServices authorizationCodeServices;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
//忽略代码....
endpoints .authorizationCodeServices(authorizationCodeServices)
//忽略代码....
}