From 8eceb0f1cd654a66753d2cd5e67a887cf33c8ce0 Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Wed, 9 Oct 2019 15:29:18 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20`=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E6=89=A9=E5=B1=95=E7=AC=AC=E4=B8=89=E6=96=B9=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xkcoding/justauth/AuthRequestFactory.java | 109 +++++++++++++++++- .../justauth/properties/ExtendProperties.java | 56 +++++++++ .../properties/JustAuthProperties.java | 14 ++- 3 files changed, 170 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/xkcoding/justauth/properties/ExtendProperties.java diff --git a/src/main/java/com/xkcoding/justauth/AuthRequestFactory.java b/src/main/java/com/xkcoding/justauth/AuthRequestFactory.java index ee4176c..43083d1 100644 --- a/src/main/java/com/xkcoding/justauth/AuthRequestFactory.java +++ b/src/main/java/com/xkcoding/justauth/AuthRequestFactory.java @@ -17,16 +17,25 @@ package com.xkcoding.justauth; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.EnumUtil; +import cn.hutool.core.util.ReflectUtil; +import cn.hutool.core.util.StrUtil; +import com.xkcoding.justauth.properties.ExtendProperties; import com.xkcoding.justauth.properties.JustAuthProperties; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.config.AuthDefaultSource; import me.zhyd.oauth.config.AuthSource; import me.zhyd.oauth.enums.AuthResponseStatus; import me.zhyd.oauth.exception.AuthException; import me.zhyd.oauth.request.*; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; /** @@ -37,6 +46,7 @@ import java.util.stream.Collectors; * @author yangkai.shen * @date Created in 2019-07-22 14:21 */ +@Slf4j @RequiredArgsConstructor public class AuthRequestFactory { private final JustAuthProperties properties; @@ -47,8 +57,17 @@ public class AuthRequestFactory { * * @return Oauth列表 */ + @SuppressWarnings("unchecked") public List oauthList() { - return properties.getType().keySet().stream().map(Enum::name).collect(Collectors.toList()); + // 默认列表 + List defaultList = properties.getType().keySet().stream().map(Enum::name).collect(Collectors.toList()); + + Class enumClass = properties.getExtend().getEnumClass(); + List names = EnumUtil.getNames(enumClass); + // 扩展列表 + List extendList = properties.getExtend().getConfig().keySet().stream().filter(x -> names.contains(x.toUpperCase())).map(String::toUpperCase).collect(Collectors.toList()); + // 合并 + return (List) CollUtil.addAll(defaultList, extendList); } /** @@ -57,9 +76,85 @@ public class AuthRequestFactory { * @param source {@link AuthSource} * @return {@link AuthRequest} */ - public AuthRequest get(AuthSource source) { - AuthConfig config = properties.getType().get(source); - switch (source) { + public AuthRequest get(String source) { + if (StrUtil.isBlank(source)) { + throw new AuthException(AuthResponseStatus.NO_AUTH_SOURCE); + } + + // 获取 JustAuth 中已存在的 + AuthRequest authRequest = getDefaultRequest(source); + + // 如果获取不到则尝试取自定义的 + if (authRequest == null) { + authRequest = getExtendRequest(properties.getExtend().getEnumClass(), source); + } + + if (authRequest == null) { + throw new AuthException(AuthResponseStatus.UNSUPPORTED); + } + + return authRequest; + } + + /** + * 获取自定义的 request + * + * @param clazz 枚举类 {@link AuthSource} + * @param source {@link AuthSource} + * @return {@link AuthRequest} + */ + @SuppressWarnings("unchecked") + private AuthRequest getExtendRequest(Class clazz, String source) { + try { + EnumUtil.fromString(clazz, source.toUpperCase()); + } catch (IllegalArgumentException e) { + // 无自定义匹配 + return null; + } + + Map extendConfig = properties.getExtend().getConfig(); + + // key 转大写 + Map upperConfig = new HashMap<>(6); + extendConfig.forEach((k, v) -> upperConfig.put(k.toUpperCase(), v)); + + ExtendProperties.ExtendRequestConfig extendRequestConfig = upperConfig.get(source.toUpperCase()); + if (extendRequestConfig != null) { + Class requestClass = extendRequestConfig.getRequestClass(); + + if (requestClass != null) { + // 反射获取 Request 对象,所以必须实现 2 个参数的构造方法 + return ReflectUtil.newInstance(requestClass, (AuthConfig) extendRequestConfig, authStateCache); + } + } + + return null; + } + + + /** + * 获取默认的 Request + * + * @param source {@link AuthSource} + * @return {@link AuthRequest} + */ + private AuthRequest getDefaultRequest(String source) { + AuthDefaultSource authDefaultSource; + + try { + authDefaultSource = EnumUtil.fromString(AuthDefaultSource.class, source.toUpperCase()); + } catch (IllegalArgumentException e) { + // 无自定义匹配 + return null; + } + + AuthConfig config = properties.getType().get(authDefaultSource); + // 找不到对应关系,直接返回空 + if (config == null) { + return null; + } + + switch (authDefaultSource) { case GITHUB: return new AuthGithubRequest(config, authStateCache); case WEIBO: @@ -116,8 +211,12 @@ public class AuthRequestFactory { return new AuthGitlabRequest(config, authStateCache); case KUJIALE: return new AuthKujialeRequest(config, authStateCache); + case ELEME: + return new AuthElemeRequest(config, authStateCache); + case MEITUAN: + return new AuthMeituanRequest(config, authStateCache); default: - throw new AuthException(AuthResponseStatus.UNSUPPORTED); + return null; } } } diff --git a/src/main/java/com/xkcoding/justauth/properties/ExtendProperties.java b/src/main/java/com/xkcoding/justauth/properties/ExtendProperties.java new file mode 100644 index 0000000..30a912d --- /dev/null +++ b/src/main/java/com/xkcoding/justauth/properties/ExtendProperties.java @@ -0,0 +1,56 @@ +/* + * 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 me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.config.AuthSource; +import me.zhyd.oauth.request.AuthRequest; + +import java.util.HashMap; +import java.util.Map; + +/** + *

+ * 扩展配置 + *

+ * + * @author yangkai.shen + * @date Created in 2019/10/9 11:21 + */ +@Getter +@Setter +public class ExtendProperties { + /** + * 枚举类全路径 + */ + private Class enumClass; + + private Map config = new HashMap<>(); + + @Getter + @Setter + public static class ExtendRequestConfig extends AuthConfig{ + /** + * 请求对应全路径 + */ + private Class requestClass; + } + +} diff --git a/src/main/java/com/xkcoding/justauth/properties/JustAuthProperties.java b/src/main/java/com/xkcoding/justauth/properties/JustAuthProperties.java index d1afa94..93fd932 100644 --- a/src/main/java/com/xkcoding/justauth/properties/JustAuthProperties.java +++ b/src/main/java/com/xkcoding/justauth/properties/JustAuthProperties.java @@ -20,7 +20,7 @@ package com.xkcoding.justauth.properties; import lombok.Getter; import lombok.Setter; import me.zhyd.oauth.config.AuthConfig; -import me.zhyd.oauth.config.AuthSource; +import me.zhyd.oauth.config.AuthDefaultSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; @@ -45,14 +45,20 @@ public class JustAuthProperties { private boolean enabled; /** - * JustAuth 配置 + * JustAuth 默认配置 */ - private Map type = new HashMap<>(); + private Map type = new HashMap<>(); + + /** + * JustAuth 自定义配置 + */ + @NestedConfigurationProperty + private ExtendProperties extend; /** * 缓存配置类 */ @NestedConfigurationProperty - private CacheProperties cache; + private CacheProperties cache = new CacheProperties(); }