1
0
mirror of synced 2026-04-19 07:48:51 +08:00

自定义prefix和超时时间

This commit is contained in:
Yangkai.Shen
2019-08-31 11:35:38 +08:00
parent 30e7f01a78
commit 3f5989c917
4 changed files with 11 additions and 9 deletions

View File

@@ -58,7 +58,7 @@ public class JustAuthAutoConfiguration {
if (CacheProperties.CacheType.REDIS == justAuthProperties.getCache().getType()) {
log.debug("JustAuth 使用 Redis 缓存存储 state 数据");
assert justAuthRedisCacheTemplate != null;
return new RedisStateCache(justAuthRedisCacheTemplate, justAuthProperties.getCache().getTimeout());
return new RedisStateCache(justAuthRedisCacheTemplate, justAuthProperties.getCache());
} else if (CacheProperties.CacheType.CUSTOM == justAuthProperties.getCache().getType()) {
log.debug("JustAuth 使用 自定义缓存存储 state 数据,请自行实现 me.zhyd.oauth.cache.AuthStateCache");
return null;

View File

@@ -17,11 +17,11 @@
package com.xkcoding.justauth.cache;
import com.xkcoding.justauth.properties.CacheProperties;
import lombok.RequiredArgsConstructor;
import me.zhyd.oauth.cache.AuthStateCache;
import org.springframework.data.redis.core.RedisTemplate;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
/**
@@ -35,7 +35,7 @@ import java.util.concurrent.TimeUnit;
@RequiredArgsConstructor
public class RedisStateCache implements AuthStateCache {
private final RedisTemplate<String, String> redisTemplate;
private final Duration timeout;
private final CacheProperties cacheProperties;
/**
* 存入缓存
@@ -45,7 +45,7 @@ public class RedisStateCache implements AuthStateCache {
*/
@Override
public void cache(String key, String value) {
this.cache(key, value, timeout.toMillis());
this.cache(key, value, cacheProperties.getTimeout().toMillis());
}
/**
@@ -57,7 +57,7 @@ public class RedisStateCache implements AuthStateCache {
*/
@Override
public void cache(String key, String value, long timeout) {
redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.MILLISECONDS);
redisTemplate.opsForValue().set(cacheProperties.getPrefix() + key, value, timeout, TimeUnit.MILLISECONDS);
}
/**
@@ -68,7 +68,7 @@ public class RedisStateCache implements AuthStateCache {
*/
@Override
public String get(String key) {
return redisTemplate.opsForValue().get(key);
return redisTemplate.opsForValue().get(cacheProperties.getPrefix() + key);
}
/**
@@ -79,7 +79,7 @@ public class RedisStateCache implements AuthStateCache {
*/
@Override
public boolean containsKey(String key) {
Long expire = redisTemplate.getExpire(key, TimeUnit.MILLISECONDS);
Long expire = redisTemplate.getExpire(cacheProperties.getPrefix() + key, TimeUnit.MILLISECONDS);
if (expire == null) {
expire = 0L;
}

View File

@@ -40,9 +40,9 @@ public class CacheProperties {
private CacheType type = CacheType.DEFAULT;
/**
* 缓存前缀,只在配置外部缓存时生效
* 缓存前缀,只在配置外部缓存时生效,默认 JUSTAUTH::STATE::
*/
private String prefix = "";
private String prefix = "JUSTAUTH::STATE::";
/**
* 超时时长默认3分钟

View File

@@ -22,6 +22,7 @@ import lombok.Setter;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import java.util.HashMap;
import java.util.Map;
@@ -51,6 +52,7 @@ public class JustAuthProperties {
/**
* 缓存配置类
*/
@NestedConfigurationProperty
private CacheProperties cache;
}