org.springframework.boot
spring-boot-autoconfigure
diff --git a/src/main/java/com/xkcoding/justauth/JustAuthAutoConfiguration.java b/src/main/java/com/xkcoding/justauth/JustAuthAutoConfiguration.java
index 0ea3b95..ef312f8 100644
--- a/src/main/java/com/xkcoding/justauth/JustAuthAutoConfiguration.java
+++ b/src/main/java/com/xkcoding/justauth/JustAuthAutoConfiguration.java
@@ -17,14 +17,19 @@
package com.xkcoding.justauth;
+import com.xkcoding.justauth.cache.RedisStateCache;
+import com.xkcoding.justauth.properties.CacheProperties;
import com.xkcoding.justauth.properties.JustAuthProperties;
+import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.cache.AuthDefaultStateCache;
import me.zhyd.oauth.cache.AuthStateCache;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.core.RedisTemplate;
/**
*
@@ -34,9 +39,12 @@ import org.springframework.context.annotation.Configuration;
* @author yangkai.shen
* @date Created in 2019-07-22 10:52
*/
+@Slf4j
@Configuration
@EnableConfigurationProperties(JustAuthProperties.class)
public class JustAuthAutoConfiguration {
+ @Autowired(required = false)
+ private RedisTemplate justAuthRedisCacheTemplate;
@Bean
@ConditionalOnProperty(prefix = "justauth", value = "enabled", havingValue = "true", matchIfMissing = true)
@@ -46,8 +54,18 @@ public class JustAuthAutoConfiguration {
@Bean
@ConditionalOnMissingBean
- public AuthStateCache authStateCache() {
- return AuthDefaultStateCache.INSTANCE;
+ public AuthStateCache authStateCache(JustAuthProperties justAuthProperties) {
+ if (CacheProperties.CacheType.REDIS == justAuthProperties.getCache().getType()) {
+ log.debug("JustAuth 使用 Redis 缓存存储 state 数据");
+ assert justAuthRedisCacheTemplate != null;
+ return new RedisStateCache(justAuthRedisCacheTemplate, justAuthProperties.getCache().getTimeout());
+ } else if (CacheProperties.CacheType.CUSTOM == justAuthProperties.getCache().getType()) {
+ log.debug("JustAuth 使用 自定义缓存存储 state 数据,请自行实现 me.zhyd.oauth.cache.AuthStateCache");
+ return null;
+ } else {
+ log.debug("JustAuth 使用 默认缓存存储 state 数据");
+ return AuthDefaultStateCache.INSTANCE;
+ }
}
}
diff --git a/src/main/java/com/xkcoding/justauth/JustAuthRedisCacheConfig.java b/src/main/java/com/xkcoding/justauth/JustAuthRedisCacheConfig.java
new file mode 100644
index 0000000..5df64e1
--- /dev/null
+++ b/src/main/java/com/xkcoding/justauth/JustAuthRedisCacheConfig.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com).
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package com.xkcoding.justauth;
+
+import lombok.RequiredArgsConstructor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.AutoConfigureBefore;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
+import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+
+import java.io.Serializable;
+
+/**
+ *
+ * Redis缓存配置类
+ *
+ *
+ * @author yangkai.shen
+ * @date Created in 2019/8/31 10:26
+ */
+@Configuration
+@ConditionalOnBean(RedisConnectionFactory.class)
+@AutoConfigureBefore(RedisAutoConfiguration.class)
+@RequiredArgsConstructor(onConstructor_ = @Autowired)
+public class JustAuthRedisCacheConfig {
+
+ /**
+ * 默认情况下的模板只能支持RedisTemplate,也就是只能存入字符串,因此支持序列化
+ */
+ @Bean
+ public RedisTemplate justAuthRedisCacheTemplate(RedisConnectionFactory redisConnectionFactory) {
+ RedisTemplate template = new RedisTemplate<>();
+ template.setKeySerializer(new StringRedisSerializer());
+ template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
+ template.setConnectionFactory(redisConnectionFactory);
+ return template;
+ }
+
+}
diff --git a/src/main/java/com/xkcoding/justauth/cache/RedisStateCache.java b/src/main/java/com/xkcoding/justauth/cache/RedisStateCache.java
new file mode 100644
index 0000000..9867ec4
--- /dev/null
+++ b/src/main/java/com/xkcoding/justauth/cache/RedisStateCache.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com).
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package com.xkcoding.justauth.cache;
+
+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;
+
+/**
+ *
+ * Redis作为JustAuth的State的缓存
+ *
+ *
+ * @author yangkai.shen
+ * @date Created in 2019-08-02 15:10
+ */
+@RequiredArgsConstructor
+public class RedisStateCache implements AuthStateCache {
+ private final RedisTemplate redisTemplate;
+ private final Duration timeout;
+
+ /**
+ * 存入缓存
+ *
+ * @param key 缓存key
+ * @param value 缓存内容
+ */
+ @Override
+ public void cache(String key, String value) {
+ this.cache(key, value, timeout.toMillis());
+ }
+
+ /**
+ * 存入缓存
+ *
+ * @param key 缓存key
+ * @param value 缓存内容
+ * @param timeout 指定缓存过期时间(毫秒)
+ */
+ @Override
+ public void cache(String key, String value, long timeout) {
+ redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.MILLISECONDS);
+ }
+
+ /**
+ * 获取缓存内容
+ *
+ * @param key 缓存key
+ * @return 缓存内容
+ */
+ @Override
+ public String get(String key) {
+ return redisTemplate.opsForValue().get(key);
+ }
+
+ /**
+ * 是否存在key,如果对应key的value值已过期,也返回false
+ *
+ * @param key 缓存key
+ * @return true:存在key,并且value没过期;false:key不存在或者已过期
+ */
+ @Override
+ public boolean containsKey(String key) {
+ Long expire = redisTemplate.getExpire(key, TimeUnit.MILLISECONDS);
+ if (expire == null) {
+ expire = 0L;
+ }
+ return expire > 0;
+ }
+}
diff --git a/src/main/java/com/xkcoding/justauth/properties/CacheProperties.java b/src/main/java/com/xkcoding/justauth/properties/CacheProperties.java
new file mode 100644
index 0000000..5e829d1
--- /dev/null
+++ b/src/main/java/com/xkcoding/justauth/properties/CacheProperties.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com).
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package com.xkcoding.justauth.properties;
+
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+
+import java.time.Duration;
+
+/**
+ *
+ * 缓存配置类
+ *
+ *
+ * @author yangkai.shen
+ * @date Created in 2019/8/31 10:18
+ */
+@Getter
+@Setter
+public class CacheProperties {
+ /**
+ * 缓存类型
+ */
+ private CacheType type = CacheType.DEFAULT;
+
+ /**
+ * 缓存前缀,只在配置外部缓存时生效
+ */
+ private String prefix = "";
+
+ /**
+ * 超时时长,默认3分钟
+ */
+ private Duration timeout = Duration.ofMinutes(3);
+
+ /**
+ * 缓存类型
+ */
+ @Getter
+ @ToString
+ public enum CacheType {
+ /**
+ * 使用JustAuth内置的缓存
+ */
+ DEFAULT,
+ /**
+ * 使用Redis缓存
+ */
+ REDIS,
+ /**
+ * 自定义缓存
+ */
+ CUSTOM;
+ }
+}
diff --git a/src/main/java/com/xkcoding/justauth/properties/JustAuthProperties.java b/src/main/java/com/xkcoding/justauth/properties/JustAuthProperties.java
index c647c05..69d234d 100644
--- a/src/main/java/com/xkcoding/justauth/properties/JustAuthProperties.java
+++ b/src/main/java/com/xkcoding/justauth/properties/JustAuthProperties.java
@@ -48,4 +48,9 @@ public class JustAuthProperties {
*/
private Map type = new HashMap<>();
+ /**
+ * 缓存配置类
+ */
+ private CacheProperties cache;
+
}
diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories
index aa36d57..2b3bbe8 100644
--- a/src/main/resources/META-INF/spring.factories
+++ b/src/main/resources/META-INF/spring.factories
@@ -1 +1,3 @@
-org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xkcoding.justauth.JustAuthAutoConfiguration
\ No newline at end of file
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+ com.xkcoding.justauth.JustAuthAutoConfiguration,\
+ com.xkcoding.justauth.JustAuthRedisCacheConfig