diff --git a/src/main/java/com/xkcoding/justauth/JustAuthAutoConfiguration.java b/src/main/java/com/xkcoding/justauth/JustAuthAutoConfiguration.java index ef312f8..85b4a04 100644 --- a/src/main/java/com/xkcoding/justauth/JustAuthAutoConfiguration.java +++ b/src/main/java/com/xkcoding/justauth/JustAuthAutoConfiguration.java @@ -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; diff --git a/src/main/java/com/xkcoding/justauth/cache/RedisStateCache.java b/src/main/java/com/xkcoding/justauth/cache/RedisStateCache.java index 9867ec4..6a8ddab 100644 --- a/src/main/java/com/xkcoding/justauth/cache/RedisStateCache.java +++ b/src/main/java/com/xkcoding/justauth/cache/RedisStateCache.java @@ -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 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; } diff --git a/src/main/java/com/xkcoding/justauth/properties/CacheProperties.java b/src/main/java/com/xkcoding/justauth/properties/CacheProperties.java index 5e829d1..40dc2e3 100644 --- a/src/main/java/com/xkcoding/justauth/properties/CacheProperties.java +++ b/src/main/java/com/xkcoding/justauth/properties/CacheProperties.java @@ -40,9 +40,9 @@ public class CacheProperties { private CacheType type = CacheType.DEFAULT; /** - * 缓存前缀,只在配置外部缓存时生效 + * 缓存前缀,只在配置外部缓存时生效,默认 JUSTAUTH::STATE:: */ - private String prefix = ""; + private String prefix = "JUSTAUTH::STATE::"; /** * 超时时长,默认3分钟 diff --git a/src/main/java/com/xkcoding/justauth/properties/JustAuthProperties.java b/src/main/java/com/xkcoding/justauth/properties/JustAuthProperties.java index 69d234d..d1afa94 100644 --- a/src/main/java/com/xkcoding/justauth/properties/JustAuthProperties.java +++ b/src/main/java/com/xkcoding/justauth/properties/JustAuthProperties.java @@ -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; }