1
0
mirror of synced 2025-12-17 03:57:17 +08:00

自定义扩展第三方平台

This commit is contained in:
Yangkai.Shen
2019-10-09 15:29:18 +08:00
parent 71e3a09565
commit 8eceb0f1cd
3 changed files with 170 additions and 9 deletions

View File

@@ -17,16 +17,25 @@
package com.xkcoding.justauth; 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 com.xkcoding.justauth.properties.JustAuthProperties;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource;
import me.zhyd.oauth.config.AuthSource; import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.enums.AuthResponseStatus; import me.zhyd.oauth.enums.AuthResponseStatus;
import me.zhyd.oauth.exception.AuthException; import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.request.*; import me.zhyd.oauth.request.*;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@@ -37,6 +46,7 @@ import java.util.stream.Collectors;
* @author yangkai.shen * @author yangkai.shen
* @date Created in 2019-07-22 14:21 * @date Created in 2019-07-22 14:21
*/ */
@Slf4j
@RequiredArgsConstructor @RequiredArgsConstructor
public class AuthRequestFactory { public class AuthRequestFactory {
private final JustAuthProperties properties; private final JustAuthProperties properties;
@@ -47,8 +57,17 @@ public class AuthRequestFactory {
* *
* @return Oauth列表 * @return Oauth列表
*/ */
@SuppressWarnings("unchecked")
public List<String> oauthList() { public List<String> oauthList() {
return properties.getType().keySet().stream().map(Enum::name).collect(Collectors.toList()); // 默认列表
List<String> defaultList = properties.getType().keySet().stream().map(Enum::name).collect(Collectors.toList());
Class enumClass = properties.getExtend().getEnumClass();
List<String> names = EnumUtil.getNames(enumClass);
// 扩展列表
List<String> extendList = properties.getExtend().getConfig().keySet().stream().filter(x -> names.contains(x.toUpperCase())).map(String::toUpperCase).collect(Collectors.toList());
// 合并
return (List<String>) CollUtil.addAll(defaultList, extendList);
} }
/** /**
@@ -57,9 +76,85 @@ public class AuthRequestFactory {
* @param source {@link AuthSource} * @param source {@link AuthSource}
* @return {@link AuthRequest} * @return {@link AuthRequest}
*/ */
public AuthRequest get(AuthSource source) { public AuthRequest get(String source) {
AuthConfig config = properties.getType().get(source); if (StrUtil.isBlank(source)) {
switch (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<String, ExtendProperties.ExtendRequestConfig> extendConfig = properties.getExtend().getConfig();
// key 转大写
Map<String, ExtendProperties.ExtendRequestConfig> upperConfig = new HashMap<>(6);
extendConfig.forEach((k, v) -> upperConfig.put(k.toUpperCase(), v));
ExtendProperties.ExtendRequestConfig extendRequestConfig = upperConfig.get(source.toUpperCase());
if (extendRequestConfig != null) {
Class<? extends AuthRequest> 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: case GITHUB:
return new AuthGithubRequest(config, authStateCache); return new AuthGithubRequest(config, authStateCache);
case WEIBO: case WEIBO:
@@ -116,8 +211,12 @@ public class AuthRequestFactory {
return new AuthGitlabRequest(config, authStateCache); return new AuthGitlabRequest(config, authStateCache);
case KUJIALE: case KUJIALE:
return new AuthKujialeRequest(config, authStateCache); return new AuthKujialeRequest(config, authStateCache);
case ELEME:
return new AuthElemeRequest(config, authStateCache);
case MEITUAN:
return new AuthMeituanRequest(config, authStateCache);
default: default:
throw new AuthException(AuthResponseStatus.UNSUPPORTED); return null;
} }
} }
} }

View File

@@ -0,0 +1,56 @@
/*
* Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com).
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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;
/**
* <p>
* 扩展配置
* </p>
*
* @author yangkai.shen
* @date Created in 2019/10/9 11:21
*/
@Getter
@Setter
public class ExtendProperties {
/**
* 枚举类全路径
*/
private Class<? extends AuthSource> enumClass;
private Map<String, ExtendRequestConfig> config = new HashMap<>();
@Getter
@Setter
public static class ExtendRequestConfig extends AuthConfig{
/**
* 请求对应全路径
*/
private Class<? extends AuthRequest> requestClass;
}
}

View File

@@ -20,7 +20,7 @@ package com.xkcoding.justauth.properties;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import me.zhyd.oauth.config.AuthConfig; 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.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty; import org.springframework.boot.context.properties.NestedConfigurationProperty;
@@ -45,14 +45,20 @@ public class JustAuthProperties {
private boolean enabled; private boolean enabled;
/** /**
* JustAuth 配置 * JustAuth 默认配置
*/ */
private Map<AuthSource, AuthConfig> type = new HashMap<>(); private Map<AuthDefaultSource, AuthConfig> type = new HashMap<>();
/**
* JustAuth 自定义配置
*/
@NestedConfigurationProperty
private ExtendProperties extend;
/** /**
* 缓存配置类 * 缓存配置类
*/ */
@NestedConfigurationProperty @NestedConfigurationProperty
private CacheProperties cache; private CacheProperties cache = new CacheProperties();
} }