From 23e5d79615c43f4797dbe1baba520270f3bdd3ed Mon Sep 17 00:00:00 2001 From: Charles7c Date: Sat, 25 Nov 2023 18:16:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20JustAuth=20?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E9=85=8D=E7=BD=AE=EF=BC=88=E8=AE=A4=E8=AF=81?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../continew-starter-auth-justauth/pom.xml | 36 ++++++++ .../JustAuthAutoConfiguration.java | 51 +++++++++++ .../impl/JustAuthStateCacheRedisImpl.java | 88 +++++++++++++++++++ ...ot.autoconfigure.AutoConfiguration.imports | 2 + continew-starter-auth/pom.xml | 1 + continew-starter-dependencies/pom.xml | 30 +++++++ 6 files changed, 208 insertions(+) create mode 100644 continew-starter-auth/continew-starter-auth-justauth/pom.xml create mode 100644 continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/charles7c/continew/starter/auth/justauth/autoconfigure/JustAuthAutoConfiguration.java create mode 100644 continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/charles7c/continew/starter/auth/justauth/impl/JustAuthStateCacheRedisImpl.java create mode 100644 continew-starter-auth/continew-starter-auth-justauth/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports diff --git a/continew-starter-auth/continew-starter-auth-justauth/pom.xml b/continew-starter-auth/continew-starter-auth-justauth/pom.xml new file mode 100644 index 00000000..6554e264 --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-justauth/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + top.charles7c.continew + continew-starter-auth + ${revision} + + + continew-starter-auth-justauth + jar + + ${project.artifactId} + ContiNew Starter 认证模块 - JustAuth + + + + + me.zhyd.oauth + JustAuth + + + + com.xkcoding.justauth + justauth-spring-boot-starter + + + + + top.charles7c.continew + continew-starter-cache-redisson + + + \ No newline at end of file diff --git a/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/charles7c/continew/starter/auth/justauth/autoconfigure/JustAuthAutoConfiguration.java b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/charles7c/continew/starter/auth/justauth/autoconfigure/JustAuthAutoConfiguration.java new file mode 100644 index 00000000..32b2050a --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/charles7c/continew/starter/auth/justauth/autoconfigure/JustAuthAutoConfiguration.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * 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 top.charles7c.continew.starter.auth.justauth.autoconfigure; + +import jakarta.annotation.PostConstruct; +import lombok.extern.slf4j.Slf4j; +import me.zhyd.oauth.cache.AuthStateCache; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import top.charles7c.continew.starter.auth.justauth.impl.JustAuthStateCacheRedisImpl; + +/** + * JustAuth 自动配置 + * + * @author Charles7c + * @since 1.0.0 + */ +@Slf4j +@AutoConfiguration(after = com.xkcoding.justauth.autoconfigure.JustAuthAutoConfiguration.class) +@ConditionalOnProperty(prefix = "justauth", value = "enabled", havingValue = "true", matchIfMissing = true) +public class JustAuthAutoConfiguration { + + /** + * 自定义 State 缓存实现 + */ + @Bean + @ConditionalOnProperty(prefix = "justauth.cache", value = "type", havingValue = "custom") + public AuthStateCache authStateCache() { + return new JustAuthStateCacheRedisImpl(); + } + + @PostConstruct + public void postConstruct() { + log.info("[ContiNew Starter] - Auto Configuration 'JustAuth' completed initialization."); + } +} \ No newline at end of file diff --git a/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/charles7c/continew/starter/auth/justauth/impl/JustAuthStateCacheRedisImpl.java b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/charles7c/continew/starter/auth/justauth/impl/JustAuthStateCacheRedisImpl.java new file mode 100644 index 00000000..f759d558 --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-justauth/src/main/java/top/charles7c/continew/starter/auth/justauth/impl/JustAuthStateCacheRedisImpl.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * 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 top.charles7c.continew.starter.auth.justauth.impl; + +import me.zhyd.oauth.cache.AuthStateCache; +import top.charles7c.continew.starter.cache.redisson.util.RedisUtils; + +import java.time.Duration; + +/** + * Just Auth 自定义 State 缓存实现(Redis) + * + * @author Charles7c + * @since 1.0.0 + */ +public class JustAuthStateCacheRedisImpl implements AuthStateCache { + + private static final String KEY_PREFIX = "SOCIAL_AUTH_STATE"; + + /** + * 存入缓存 + * + * @param key + * key + * @param value + * 内容 + */ + @Override + public void cache(String key, String value) { + // 参考:在 JustAuth 中,内置了一个基于 map 的 state 缓存器,默认缓存有效期为 3 分钟 + RedisUtils.set(RedisUtils.formatKey(KEY_PREFIX, key), value, + Duration.ofMinutes(3)); + } + + /** + * 存入缓存 + * + * @param key + * key + * @param value + * 内容 + * @param timeout + * 缓存过期时间(毫秒) + */ + @Override + public void cache(String key, String value, long timeout) { + RedisUtils.set(RedisUtils.formatKey(KEY_PREFIX, key), value, + Duration.ofMillis(timeout)); + } + + /** + * 获取缓存内容 + * + * @param key + * key + * @return 内容 + */ + @Override + public String get(String key) { + return RedisUtils.get(RedisUtils.formatKey(KEY_PREFIX, key)); + } + + /** + * 是否存在 key,如果对应 key 的 value 值已过期,也返回 false + * + * @param key + * key + * @return true:存在 key,并且 value 没过期;false:key 不存在或者已过期 + */ + @Override + public boolean containsKey(String key) { + return RedisUtils.hasKey(RedisUtils.formatKey(KEY_PREFIX, key)); + } +} \ No newline at end of file diff --git a/continew-starter-auth/continew-starter-auth-justauth/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-auth/continew-starter-auth-justauth/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..bbd7cc4f --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-justauth/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1,2 @@ +top.charles7c.continew.starter.auth.justauth.autoconfigure.JustAuthAutoConfiguration +com.xkcoding.justauth.autoconfigure.JustAuthAutoConfiguration \ No newline at end of file diff --git a/continew-starter-auth/pom.xml b/continew-starter-auth/pom.xml index 8e6bcab3..23de50c7 100644 --- a/continew-starter-auth/pom.xml +++ b/continew-starter-auth/pom.xml @@ -17,6 +17,7 @@ continew-starter-auth-satoken + continew-starter-auth-justauth diff --git a/continew-starter-dependencies/pom.xml b/continew-starter-dependencies/pom.xml index 32e89579..fc59d5bf 100644 --- a/continew-starter-dependencies/pom.xml +++ b/continew-starter-dependencies/pom.xml @@ -55,6 +55,7 @@ 1.0.0-SNAPSHOT + 1.16.5 1.37.0 3.5.4.1 4.2.0 @@ -67,6 +68,28 @@ + + + me.zhyd.oauth + JustAuth + ${just-auth.version} + + + com.xkcoding.justauth + justauth-spring-boot-starter + 1.4.0 + + + cn.hutool + hutool-core + + + me.zhyd.oauth + JustAuth + + + + cn.dev33 @@ -139,6 +162,13 @@ + + + top.charles7c.continew + continew-starter-auth-justauth + ${revision} + + top.charles7c.continew