From e03aa81f33db36d9ac165cedf61f6d6c34a5479c Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Sat, 31 Aug 2019 11:04:25 +0800 Subject: [PATCH 1/6] =?UTF-8?q?:sparkles:=20=E9=9B=86=E6=88=90=20redis=20?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E9=85=8D=E7=BD=AE=EF=BC=8C=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E9=80=9A=E8=BF=87=E9=85=8D=E7=BD=AE=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E7=BC=93=E5=AD=98=EF=BC=8C=E4=B8=8D=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E8=87=AA=E8=A1=8C=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 13 ++- .../justauth/JustAuthAutoConfiguration.java | 22 ++++- .../justauth/JustAuthRedisCacheConfig.java | 60 +++++++++++++ .../justauth/cache/RedisStateCache.java | 88 +++++++++++++++++++ .../justauth/properties/CacheProperties.java | 71 +++++++++++++++ .../properties/JustAuthProperties.java | 5 ++ src/main/resources/META-INF/spring.factories | 4 +- 7 files changed, 259 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/xkcoding/justauth/JustAuthRedisCacheConfig.java create mode 100644 src/main/java/com/xkcoding/justauth/cache/RedisStateCache.java create mode 100644 src/main/java/com/xkcoding/justauth/properties/CacheProperties.java diff --git a/pom.xml b/pom.xml index 5c1372b..7326b04 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ com.xkcoding justauth-spring-boot-starter - 1.0.1 + 1.0.2-SNAPSHOT justauth-spring-boot-starter https://github.com/xkcoding/justauth-spring-boot-starter @@ -76,6 +76,17 @@ JustAuth ${justauth.version} + + org.springframework.boot + spring-boot-starter-data-redis + provided + + + + org.apache.commons + commons-pool2 + provided + 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 From 263930ed34f363a2ff6a57ce1f74b07a10ab7b4f Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Sat, 31 Aug 2019 11:35:38 +0800 Subject: [PATCH 2/6] =?UTF-8?q?:sparkles:=20=E8=87=AA=E5=AE=9A=E4=B9=89pre?= =?UTF-8?q?fix=E5=92=8C=E8=B6=85=E6=97=B6=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xkcoding/justauth/JustAuthAutoConfiguration.java | 2 +- .../com/xkcoding/justauth/cache/RedisStateCache.java | 12 ++++++------ .../justauth/properties/CacheProperties.java | 4 ++-- .../justauth/properties/JustAuthProperties.java | 2 ++ 4 files changed, 11 insertions(+), 9 deletions(-) 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; } From 1a890c4497860d1c0d8bf83818f262ca3ef27908 Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Sat, 31 Aug 2019 12:34:34 +0800 Subject: [PATCH 3/6] =?UTF-8?q?:art:=20=E4=BC=98=E5=8C=96=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E8=A3=85=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../justauth/JustAuthAutoConfiguration.java | 27 +---- .../justauth/JustAuthRedisCacheConfig.java | 60 ---------- .../JustAuthStateCacheConfiguration.java | 105 ++++++++++++++++++ src/main/resources/META-INF/spring.factories | 3 +- 4 files changed, 111 insertions(+), 84 deletions(-) delete mode 100644 src/main/java/com/xkcoding/justauth/JustAuthRedisCacheConfig.java create mode 100644 src/main/java/com/xkcoding/justauth/JustAuthStateCacheConfiguration.java diff --git a/src/main/java/com/xkcoding/justauth/JustAuthAutoConfiguration.java b/src/main/java/com/xkcoding/justauth/JustAuthAutoConfiguration.java index 85b4a04..824f7dc 100644 --- a/src/main/java/com/xkcoding/justauth/JustAuthAutoConfiguration.java +++ b/src/main/java/com/xkcoding/justauth/JustAuthAutoConfiguration.java @@ -17,19 +17,14 @@ 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; +import org.springframework.context.annotation.Import; /** *

@@ -43,8 +38,6 @@ import org.springframework.data.redis.core.RedisTemplate; @Configuration @EnableConfigurationProperties(JustAuthProperties.class) public class JustAuthAutoConfiguration { - @Autowired(required = false) - private RedisTemplate justAuthRedisCacheTemplate; @Bean @ConditionalOnProperty(prefix = "justauth", value = "enabled", havingValue = "true", matchIfMissing = true) @@ -52,20 +45,10 @@ public class JustAuthAutoConfiguration { return new AuthRequestFactory(properties, authStateCache); } - @Bean - @ConditionalOnMissingBean - 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()); - } 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; - } + @Configuration + @Import({JustAuthStateCacheConfiguration.Default.class, JustAuthStateCacheConfiguration.Redis.class, JustAuthStateCacheConfiguration.Custom.class}) + protected static class AuthStateCacheAutoConfiguration { + } } diff --git a/src/main/java/com/xkcoding/justauth/JustAuthRedisCacheConfig.java b/src/main/java/com/xkcoding/justauth/JustAuthRedisCacheConfig.java deleted file mode 100644 index 5df64e1..0000000 --- a/src/main/java/com/xkcoding/justauth/JustAuthRedisCacheConfig.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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/JustAuthStateCacheConfiguration.java b/src/main/java/com/xkcoding/justauth/JustAuthStateCacheConfiguration.java new file mode 100644 index 0000000..6a19c55 --- /dev/null +++ b/src/main/java/com/xkcoding/justauth/JustAuthStateCacheConfiguration.java @@ -0,0 +1,105 @@ +/* + * 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 com.xkcoding.justauth.cache.RedisStateCache; +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.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; +import org.springframework.context.annotation.Bean; +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; + +/** + *

+ * JustAuth 缓存装配类,{@link JustAuthAutoConfiguration.AuthStateCacheAutoConfiguration} + *

+ * + * @author yangkai.shen + * @date Created in 2019/8/31 12:00 + */ +@Slf4j +abstract class JustAuthStateCacheConfiguration { + /** + * Redis 缓存 + */ + @ConditionalOnClass(RedisTemplate.class) + @ConditionalOnMissingBean(AuthStateCache.class) + @AutoConfigureBefore(RedisAutoConfiguration.class) + @ConditionalOnProperty(name = "justauth.cache.type", havingValue = "redis", matchIfMissing = true) + static class Redis { + static { + log.debug("JustAuth 使用 Redis 缓存存储 state 数据"); + } + + @Bean + public RedisTemplate justAuthRedisCacheTemplate(RedisConnectionFactory redisConnectionFactory) { + RedisTemplate template = new RedisTemplate<>(); + template.setKeySerializer(new StringRedisSerializer()); + template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); + template.setConnectionFactory(redisConnectionFactory); + return template; + } + + @Bean + public AuthStateCache authStateCache(RedisTemplate justAuthRedisCacheTemplate, JustAuthProperties justAuthProperties) { + return new RedisStateCache(justAuthRedisCacheTemplate, justAuthProperties.getCache()); + } + } + + /** + * 默认缓存 + */ + @ConditionalOnMissingBean(AuthStateCache.class) + @ConditionalOnProperty(name = "justauth.cache.type", havingValue = "default", matchIfMissing = true) + static class Default { + static { + log.debug("JustAuth 使用 默认缓存存储 state 数据"); + } + + @Bean + public AuthStateCache authStateCache() { + return AuthDefaultStateCache.INSTANCE; + } + } + + /** + * 默认缓存 + */ + @ConditionalOnProperty(name = "justauth.cache.type", havingValue = "custom") + static class Custom { + static { + log.debug("JustAuth 使用 自定义缓存存储 state 数据"); + } + + @Bean + @ConditionalOnMissingBean(AuthStateCache.class) + public AuthStateCache authStateCache() { + log.error("请自行实现 me.zhyd.oauth.cache.AuthStateCache"); + throw new RuntimeException(); + } + } +} diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories index 2b3bbe8..2341e0f 100644 --- a/src/main/resources/META-INF/spring.factories +++ b/src/main/resources/META-INF/spring.factories @@ -1,3 +1,2 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ - com.xkcoding.justauth.JustAuthAutoConfiguration,\ - com.xkcoding.justauth.JustAuthRedisCacheConfig + com.xkcoding.justauth.JustAuthAutoConfiguration From 9dbe77b7034a2242d9f6e16e231fe3e8e5ba9ec6 Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Sat, 31 Aug 2019 12:38:42 +0800 Subject: [PATCH 4/6] =?UTF-8?q?:memo:=20=E8=A1=A5=E5=85=85=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xkcoding/justauth/properties/CacheProperties.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/xkcoding/justauth/properties/CacheProperties.java b/src/main/java/com/xkcoding/justauth/properties/CacheProperties.java index 40dc2e3..a1a610e 100644 --- a/src/main/java/com/xkcoding/justauth/properties/CacheProperties.java +++ b/src/main/java/com/xkcoding/justauth/properties/CacheProperties.java @@ -40,12 +40,12 @@ public class CacheProperties { private CacheType type = CacheType.DEFAULT; /** - * 缓存前缀,只在配置外部缓存时生效,默认 JUSTAUTH::STATE:: + * 缓存前缀,目前只对redis缓存生效,默认 JUSTAUTH::STATE:: */ private String prefix = "JUSTAUTH::STATE::"; /** - * 超时时长,默认3分钟 + * 超时时长,目前只对redis缓存生效,默认3分钟 */ private Duration timeout = Duration.ofMinutes(3); From 59da8a1c61916d213d461d48783d5d3edd0da0a7 Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Sat, 31 Aug 2019 13:12:06 +0800 Subject: [PATCH 5/6] =?UTF-8?q?:memo:=20=E6=B7=BB=E5=8A=A0=20CHANGELOG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 203dd9a..9ef3f82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ ## 版本更新记录 +### 【1.0.2-SNAPSHOT】2019-08-31 + +- 添加内置的 Redis 缓存策略,通过配置项可配,不需要额外实现 +- 支持 Redis 缓存自定义缓存前缀、自定义缓存过期时间 + ### 【1.0.1】2019-08-19 - 升级 `JustAuth` 版本:1.10.1,AuthUser添加构造函数,支持反序列化 From cf6ef0de493304de7575b079870b88a35839f7ea Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Sat, 31 Aug 2019 13:12:19 +0800 Subject: [PATCH 6/6] =?UTF-8?q?:memo:=20=E6=9B=B4=E6=96=B0=20README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 300 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 187 insertions(+), 113 deletions(-) diff --git a/README.md b/README.md index 6b0b539..83fb3dd 100644 --- a/README.md +++ b/README.md @@ -22,13 +22,15 @@ https://github.com/xkcoding/justauth-spring-boot-starter-demo ## 快速开始 +### 1. 基础配置 + - 引用依赖 ```xml com.xkcoding justauth-spring-boot-starter - 1.0.1 + 1.0.2-SNAPSHOT ``` @@ -42,6 +44,8 @@ justauth: client-id: 10**********6 client-secret: 1f7d08**********5b7**********29e redirect-uri: http://oauth.xkcoding.com/demo/oauth/qq/callback + cache: + type: default ``` - 然后就开始玩耍吧~ @@ -83,134 +87,170 @@ public class TestController { } ``` -- 如果需要自定义 State 的缓存(此处举例 Redis,其余缓存同理) +### 2. 缓存配置 - > 1. 自定义缓存实现 `AuthStateCache` 接口 - > 2. 将自定义缓存加入 Spring 容器 +> starter 内置了2种缓存实现,一种是上面的默认实现,另一种是基于 Redis 的缓存实现。 +> +> 当然了,你也可以自定义实现你自己的缓存。 - 1.添加Redis依赖 +#### 2.1. 默认缓存实现 - ```xml - - org.springframework.boot - spring-boot-starter-data-redis - - - - - org.apache.commons - commons-pool2 - - ``` +在配置文件配置如下内容即可 - 2.自定义缓存 `RedisStateCache` +```yaml +justauth: + cache: + type: default +``` - ```java - /** - *

- * 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 static final long DEF_TIMEOUT = 3 * 60 * 1000; - - /** - * 存入缓存 - * - * @param key 缓存key - * @param value 缓存内容 - */ - @Override - public void cache(String key, String value) { - this.cache(key, value, DEF_TIMEOUT); - } - - /** - * 存入缓存 - * - * @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; - } - } - ``` +#### 2.2. Redis 缓存实现 - 3.自动装配 `JustAuthConfig` +1.添加 Redis 相关依赖 - ```java - /** - *

- * JustAuth配置类 - *

- * - * @author yangkai.shen - * @date Created in 2019-08-02 15:08 - */ - @Configuration - public class JustAuthConfig { - /** - * 默认情况下的模板只能支持RedisTemplate,也就是只能存入字符串,因此支持序列化 - */ - @Bean - public RedisTemplate redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) { - RedisTemplate template = new RedisTemplate<>(); - template.setKeySerializer(new StringRedisSerializer()); - template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); - template.setConnectionFactory(redisConnectionFactory); - return template; - } - - @Bean - public AuthStateCache authStateCache(RedisTemplate redisCacheTemplate) { - return new RedisStateCache(redisCacheTemplate); - } - - } - ``` +```xml + + org.springframework.boot + spring-boot-starter-data-redis + + + + + org.apache.commons + commons-pool2 + +``` + +2.配置文件配置如下内容即可 + +```yaml +justauth: + cache: + type: redis + # 缓存前缀,目前只对redis缓存生效,默认 JUSTAUTH::STATE:: + prefix: '' + # 超时时长,目前只对redis缓存生效,默认3分钟 + timeout: 1h +spring: + redis: + host: localhost + # 连接超时时间(记得添加单位,Duration) + timeout: 10000ms + # Redis默认情况下有16个分片,这里配置具体使用的分片 + # database: 0 + lettuce: + pool: + # 连接池最大连接数(使用负值表示没有限制) 默认 8 + max-active: 8 + # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1 + max-wait: -1ms + # 连接池中的最大空闲连接 默认 8 + max-idle: 8 + # 连接池中的最小空闲连接 默认 0 + min-idle: 0 +``` + +#### 2.3. 自定义缓存实现 + +1.配置文件配置如下内容 + +```yaml +justauth: + cache: + type: custom +``` + +2.自定义缓存实现 `AuthStateCache` 接口 + +```java +/** + *

+ * 自定义缓存实现 + *

+ * + * @author yangkai.shen + * @date Created in 2019/8/31 12:53 + */ +public class MyAuthStateCache implements AuthStateCache { + /** + * 存入缓存 + * + * @param key 缓存key + * @param value 缓存内容 + */ + @Override + public void cache(String key, String value) { + // TODO: 自定义存入缓存 + } + + /** + * 存入缓存 + * + * @param key 缓存key + * @param value 缓存内容 + * @param timeout 指定缓存过期时间(毫秒) + */ + @Override + public void cache(String key, String value, long timeout) { + // TODO: 自定义存入缓存 + } + + /** + * 获取缓存内容 + * + * @param key 缓存key + * @return 缓存内容 + */ + @Override + public String get(String key) { + // TODO: 自定义获取缓存内容 + return null; + } + + /** + * 是否存在key,如果对应key的value值已过期,也返回false + * + * @param key 缓存key + * @return true:存在key,并且value没过期;false:key不存在或者已过期 + */ + @Override + public boolean containsKey(String key) { + // TODO: 自定义判断key是否存在 + return false; + } +} +``` + +3.自动装配 `JustAuthConfig` + +```java +/** + *

+ * 自定义缓存装配 + *

+ * + * @author yangkai.shen + * @date Created in 2019/8/31 12:29 + */ +@Configuration +public class AuthStateConfiguration { + @Bean + public AuthStateCache authStateCache() { + return new MyAuthStateCache(); + } +} +``` ## 附录 +### 1. 配置 + `justauth` 配置列表 | 属性名 | 类型 | 默认值 | 可选项 | 描述 | | ------------------ | ------------------------------------------------------------ | ------ | ---------- | ----------------- | | `justauth.enabled` | `boolean` | true | true/false | 是否启用 JustAuth | | `justauth.type` | `java.util.Map` | 无 | | JustAuth 配置 | +| `justauth.cache` | `com.xkcoding.justauth.properties.CacheProperties` | | | JustAuth缓存配置 | `justauth.type` 配置列表 @@ -219,3 +259,37 @@ public class TestController { | `justauth.type.keys` | `justauth.type` 是 `Map` 格式的,key 的取值请参考 [`AuthSource`](https://github.com/zhangyd-c/JustAuth/blob/master/src/main/java/me/zhyd/oauth/config/AuthSource.java) | | `justauth.type.keys.values` | `justauth.type` 是 `Map` 格式的,value 的取值请参考 [`AuthConfig`](https://github.com/zhangyd-c/JustAuth/blob/master/src/main/java/me/zhyd/oauth/config/AuthConfig.java) | +`justauth.cache` 配置列表 + +| 属性名 | 类型 | 默认值 | 可选项 | 描述 | +| ------------------------ | ------------------------------------------------------------ | ----------------- | -------------------- | ------------------------------------------------------------ | +| `justauth.cache.type` | `com.xkcoding.justauth.properties.CacheProperties.CacheType` | default | default/redis/custom | 缓存类型,default使用JustAuth默认的缓存实现,redis使用默认的redis缓存实现,custom用户自定义缓存实现 | +| `justauth.cache.prefix` | `string` | JUSTAUTH::STATE:: | | 缓存前缀,目前只对redis缓存生效,默认 JUSTAUTH::STATE:: | +| `justauth.cache.timeout` | `java.time.Duration` | 3分钟 | | 超时时长,目前只对redis缓存生效,默认3分钟 | + +### 2. 私服 + +如果想体验快照版本,需要在 `pom.xml` 文件里添加如下配置 + +```xml + + + + aliyun + aliyun + http://maven.aliyun.com/nexus/content/groups/public + + + + xkcoding-nexus + xkcoding nexus + https://nexus.xkcoding.com/repository/maven-public/ + + true + + + true + + + +``` \ No newline at end of file