From 6971ae0387d80d4f3ea48efc037716e4fea9228d Mon Sep 17 00:00:00 2001 From: "yadong.zhang" Date: Thu, 25 Jun 2020 13:37:19 +0800 Subject: [PATCH] =?UTF-8?q?:arrow=5Fup:=20=E5=8D=87=E7=BA=A7=20`JustAuth`?= =?UTF-8?q?=20=E7=89=88=E6=9C=AC=EF=BC=9A1.15.5=EF=BC=8C=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=20http=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 + README.md | 76 ++++++++++++++----- pom.xml | 9 ++- .../xkcoding/justauth/AuthRequestFactory.java | 48 ++++++++++-- .../autoconfigure/CacheProperties.java | 2 +- .../autoconfigure/ExtendProperties.java | 2 +- .../autoconfigure/JustAuthProperties.java | 25 +++++- ...itional-spring-configuration-metadata.json | 52 +++++++++++-- 8 files changed, 178 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0cc3ad..6951a35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## 版本更新记录 +### 【1.3.5】2020-06-25 + +- 升级 `JustAuth` 版本:1.15.5,支持自定义 http config + ### 【1.3.4】2020-06-10 - 升级 `JustAuth` 版本:1.15.4-alpha diff --git a/README.md b/README.md index b56e96c..a684da0 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ https://github.com/xkcoding/justauth-spring-boot-starter-demo com.xkcoding.justauth justauth-spring-boot-starter - 1.3.4 + 1.3.5 ``` @@ -75,10 +75,7 @@ justauth: client-id: 10**********6 client-secret: 1f7d08**********5b7**********29e redirect-uri: http://oauth.xkcoding.com/demo/oauth/coding/callback - TENCENT_CLOUD: - client-id: 10**********6 - client-secret: 1f7d08**********5b7**********29e - redirect-uri: http://oauth.xkcoding.com/demo/oauth/tencent_cloud/callback + coding-group-name: xx OSCHINA: client-id: 10**********6 client-secret: 1f7d08**********5b7**********29e @@ -522,39 +519,78 @@ justauth: redirect-uri: http://oauth.xkcoding.com/demo/oauth/test/callback ``` -## 4. 附录 +## 4. http 代理配置 -### 4.1. 基础配置 +修改配置文件,增加如下配置: -#### 4.1.1. `justauth` 配置列表 +```yaml +justauth: + http-config: + timeout: 30000 + proxy: + GOOGLE: + type: HTTP + hostname: 127.0.0.1 + port: 10080 +``` + +注:当项目中使用了自定义的第三方登录,并且需要使用代理时,也要在 `http-config` 节点下添加相关配置,格式参考上面示例 + +## 5. 附录 + +### 5.1. `justauth` 配置列表 | 属性名 | 类型 | 默认值 | 可选项 | 描述 | | ------------------ | ------------------------------------------------------------ | ------ | ---------- | ---------------------- | | `justauth.enabled` | `boolean` | true | true/false | 是否启用 JustAuth | | `justauth.type` | `java.util.Map` | 无 | | JustAuth 配置 | +| `justauth.httpConfig` | `java.util.Map` | 无 | | http 相关配置 | | `justauth.cache` | `com.xkcoding.justauth.autoconfigure.CacheProperties` | | | JustAuth缓存配置 | | `justauth.extend` | `com.xkcoding.justauth.autoconfigure.ExtendProperties` | 无 | | JustAuth第三方平台配置 | -##### 4.1.1.1. `justauth.type` 配置列表 +#### `justauth.type` 配置列表 | 属性名 | 描述 | | --------------------------- | ------------------------------------------------------------ | | `justauth.type.keys` | `justauth.type` 是 `Map` 格式的,key 的取值请参考 [`AuthDefaultSource`](https://github.com/zhangyd-c/JustAuth/blob/master/src/main/java/me/zhyd/oauth/config/AuthDefaultSource.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) | -###### 4.1.1.1.1 `justauth.type.keys.values` 所有可选配置如下: +##### `justauth.type.keys.values` 所有可选配置如下: | 属性名 | 描述 | 备注 | | --------------------------- | ----------------------------------------------------------- | ------------------------------- | -| `client-id` | 客户端id,对应各平台的appKey | 必填 | -| `client-secret` | 客户端Secret,对应各平台的appSecret | 必填 | -| `redirect-uri` | 登录成功后的回调地址 | 必填 | -| `alipay-public-key` | 支付宝公钥 | 当使用支付宝登录时,该值必填,对应“RSA2(SHA256)密钥”中的“支付宝公钥” | +| `client-id` | 客户端id,对应各平台的appKey | **必填** | +| `client-secret` | 客户端Secret,对应各平台的appSecret | **必填** | +| `redirect-uri` | 登录成功后的回调地址 | **必填** | +| `alipay-public-key` | 支付宝公钥 | 当使用支付宝登录时, **该值必填**,对应“RSA2(SHA256)密钥”中的“支付宝公钥” | | `union-id` | 是否需要申请unionid | 当使用QQ登录时,该值**选填**,如果置为`true`则qq开发者应用必须具备相应权限,参考链接:[查看详情](http://wiki.connect.qq.com/unionid%E4%BB%8B%E7%BB%8D) | -| `stack-overflow-key` | Stack Overflow Key | 当使用Stack Overflow登录时,该值必填 | -| `agent-id` | 企业微信,授权方的网页应用ID | 当使用企业微信登录时,该值必填 | +| `stack-overflow-key` | Stack Overflow Key | 当使用Stack Overflow登录时, **该值必填** | +| `agent-id` | 企业微信,授权方的网页应用ID | 当使用企业微信登录时, **该值必填** | +| `coding-group-name` | 团队域名前缀 | 使用 Coding 登录时, **该值必填** | -##### 4.1.1.2.`justauth.cache` 配置列表 +#### `justauth.httpConfig` 配置列表 + +| 属性名 | 描述 | +| --------------------------- | ------------------------------------------------------------ | +| `justauth.httpConfig.keys` | `justauth.type` 是 `Map` 格式的,key 的取值请参考 [`AuthDefaultSource`](https://github.com/zhangyd-c/JustAuth/blob/master/src/main/java/me/zhyd/oauth/config/AuthDefaultSource.java) | +| `justauth.httpConfig.keys.values` | `justauth.type` 是 `Map` 格式的,value 的取值请参考 `JustAuthProperties.JustAuthHttpConfig` | + +##### `justauth.httpConfig.keys.values` 所有可选配置如下: + +| 属性名 | 描述 | 备注 | +| --------------------------- | ----------------------------------------------------------- | ------------------------------- | +| `timeout` | 请求超时时间 | | +| `proxy` | 代理的相关配置,针对国外平台,需要配置代理 | **必填** | + +##### `justauth.httpConfig.proxy` 所有可选配置如下: + +| 属性名 | 描述 | 备注 | +| --------------------------- | ----------------------------------------------------------- | ------------------------------- | +| `type` | 代理类型,可选值:`HTTP`、`DIRECT`、`SOCKS`,默认为 `HTTP` | | +| `hostname` | 代理 IP 地址 | | +| `port` | 代理端口 | | + +#### `justauth.cache` 配置列表 | 属性名 | 类型 | 默认值 | 可选项 | 描述 | | ------------------------ | ------------------------------------------------------------ | ----------------- | -------------------- | ------------------------------------------------------------ | @@ -562,21 +598,21 @@ justauth: | `justauth.cache.prefix` | `java.lang.String` | JUSTAUTH::STATE:: | | 缓存前缀,目前只对redis缓存生效,默认 `JUSTAUTH::STATE::` | | `justauth.cache.timeout` | `java.time.Duration` | 3分钟 | | 超时时长,目前只对redis缓存生效,默认`3分钟` | -##### 4.1.1.3.`justauth.extend` 配置列表 +#### `justauth.extend` 配置列表 | 属性名 | 类型 | 默认值 | 可选项 | 描述 | | ---------------------------- | -------------------------------------------- | ------ | ------ | ------------ | | `justauth.extend.enum-class` | `Class` | 无 | | 枚举类全路径 | | `justauth.extend.config` | `java.util.Map` | 无 | | 对应配置信息 | -###### 4.1.1.3.1.`justauth.extend.config` 配置列表 +##### `justauth.extend.config` 配置列表 | 属性名 | 类型 | 默认值 | 可选项 | 描述 | | ------------------------------- | ------------------------------------------------------------ | ------ | ------ | ------------------------------------------------------------ | | `justauth.extend.config.keys` | `java.lang.String` | 无 | | key 必须在 `justauth.extend.enum-class` 配置的枚举类中声明 | | `justauth.extend.config.values` | `com.xkcoding.justauth.autoconfigure.ExtendProperties.ExtendRequestConfig` | 无 | | value 就是 `AuthConfig` 的子类,增加了一个 `request-class` 属性配置请求的全类名,具体参考类[`ExtendProperties.ExtendRequestConfig`](https://github.com/justauth/justauth-spring-boot-starter/blob/master/src/main/java/com/xkcoding/justauth/autoconfigure/ExtendProperties.java#L49-L54) | -### 4.2. SNAPSHOT版本 +### 5.2. SNAPSHOT版本 ![https://img.shields.io/badge/snapshots-1.4.0--SNAPSHOT-green](https://img.shields.io/badge/snapshots-1.4.0--SNAPSHOT-green)如果需要体验快照版本,可以在你的 `pom.xml`进行如下配置: diff --git a/pom.xml b/pom.xml index f0164d4..3da5450 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ com.xkcoding.justauth justauth-spring-boot-starter - 1.3.4 + 1.3.5 justauth-spring-boot-starter https://github.com/xkcoding/justauth-spring-boot-starter @@ -43,6 +43,11 @@ Asia/Shanghai + + Yadong.Zhang + yadong.zhang0415@gmail.com + https://www.zhyd.me + @@ -67,7 +72,7 @@ 2.1.8.RELEASE - 1.15.4-alpha + 1.15.5 5.1.0 diff --git a/src/main/java/com/xkcoding/justauth/AuthRequestFactory.java b/src/main/java/com/xkcoding/justauth/AuthRequestFactory.java index 2b56ddb..c1d304d 100644 --- a/src/main/java/com/xkcoding/justauth/AuthRequestFactory.java +++ b/src/main/java/com/xkcoding/justauth/AuthRequestFactory.java @@ -21,6 +21,7 @@ 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.http.config.HttpConfig; import com.xkcoding.justauth.autoconfigure.ExtendProperties; import com.xkcoding.justauth.autoconfigure.JustAuthProperties; import lombok.RequiredArgsConstructor; @@ -32,7 +33,10 @@ import me.zhyd.oauth.config.AuthSource; import me.zhyd.oauth.enums.AuthResponseStatus; import me.zhyd.oauth.exception.AuthException; import me.zhyd.oauth.request.*; +import org.springframework.util.CollectionUtils; +import java.net.InetSocketAddress; +import java.net.Proxy; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -61,7 +65,7 @@ public class AuthRequestFactory { @SuppressWarnings("unchecked") public List oauthList() { // 默认列表 - List defaultList = properties.getType().keySet().stream().map(Enum::name).collect(Collectors.toList()); + List defaultList = new ArrayList<>(properties.getType().keySet()); // 扩展列表 List extendList = new ArrayList<>(); ExtendProperties extend = properties.getExtend(); @@ -116,8 +120,9 @@ public class AuthRequestFactory { */ @SuppressWarnings("unchecked") private AuthRequest getExtendRequest(Class clazz, String source) { + String upperSource = source.toUpperCase(); try { - EnumUtil.fromString(clazz, source.toUpperCase()); + EnumUtil.fromString(clazz, upperSource); } catch (IllegalArgumentException e) { // 无自定义匹配 return null; @@ -129,8 +134,12 @@ public class AuthRequestFactory { Map upperConfig = new HashMap<>(6); extendConfig.forEach((k, v) -> upperConfig.put(k.toUpperCase(), v)); - ExtendProperties.ExtendRequestConfig extendRequestConfig = upperConfig.get(source.toUpperCase()); + ExtendProperties.ExtendRequestConfig extendRequestConfig = upperConfig.get(upperSource); if (extendRequestConfig != null) { + + // 配置 http config + configureHttpConfig(upperSource, extendRequestConfig, properties.getHttpConfig()); + Class requestClass = extendRequestConfig.getRequestClass(); if (requestClass != null) { @@ -159,12 +168,15 @@ public class AuthRequestFactory { return null; } - AuthConfig config = properties.getType().get(authDefaultSource); + AuthConfig config = properties.getType().get(authDefaultSource.name()); // 找不到对应关系,直接返回空 if (config == null) { return null; } + // 配置 http config + configureHttpConfig(authDefaultSource.name(), config, properties.getHttpConfig()); + switch (authDefaultSource) { case GITHUB: return new AuthGithubRequest(config, authStateCache); @@ -180,8 +192,6 @@ public class AuthRequestFactory { return new AuthCsdnRequest(config, authStateCache); case CODING: return new AuthCodingRequest(config, authStateCache); - case TENCENT_CLOUD: - return new AuthTencentCloudRequest(config, authStateCache); case OSCHINA: return new AuthOschinaRequest(config, authStateCache); case ALIPAY: @@ -234,4 +244,30 @@ public class AuthRequestFactory { return null; } } + + /** + * 配置 http 相关的配置 + * + * @param authSource {@link AuthSource} + * @param authConfig {@link AuthConfig} + */ + private void configureHttpConfig(String authSource, AuthConfig authConfig, JustAuthProperties.JustAuthHttpConfig httpConfig) { + if (null == httpConfig) { + return; + } + Map proxyConfigMap = httpConfig.getProxy(); + if (CollectionUtils.isEmpty(proxyConfigMap)) { + return; + } + JustAuthProperties.JustAuthProxyConfig proxyConfig = proxyConfigMap.get(authSource); + + if (null == proxyConfig) { + return; + } + + authConfig.setHttpConfig(HttpConfig.builder() + .timeout(httpConfig.getTimeout()) + .proxy(new Proxy(Proxy.Type.valueOf(proxyConfig.getType()), new InetSocketAddress(proxyConfig.getHostname(), proxyConfig.getPort()))) + .build()); + } } diff --git a/src/main/java/com/xkcoding/justauth/autoconfigure/CacheProperties.java b/src/main/java/com/xkcoding/justauth/autoconfigure/CacheProperties.java index 67b5441..37a7f88 100644 --- a/src/main/java/com/xkcoding/justauth/autoconfigure/CacheProperties.java +++ b/src/main/java/com/xkcoding/justauth/autoconfigure/CacheProperties.java @@ -66,6 +66,6 @@ public class CacheProperties { /** * 自定义缓存 */ - CUSTOM; + CUSTOM } } diff --git a/src/main/java/com/xkcoding/justauth/autoconfigure/ExtendProperties.java b/src/main/java/com/xkcoding/justauth/autoconfigure/ExtendProperties.java index 303a795..2232827 100644 --- a/src/main/java/com/xkcoding/justauth/autoconfigure/ExtendProperties.java +++ b/src/main/java/com/xkcoding/justauth/autoconfigure/ExtendProperties.java @@ -46,7 +46,7 @@ public class ExtendProperties { @Getter @Setter - public static class ExtendRequestConfig extends AuthConfig{ + public static class ExtendRequestConfig extends AuthConfig { /** * 请求对应全路径 */ diff --git a/src/main/java/com/xkcoding/justauth/autoconfigure/JustAuthProperties.java b/src/main/java/com/xkcoding/justauth/autoconfigure/JustAuthProperties.java index 213c9b8..74b32a5 100644 --- a/src/main/java/com/xkcoding/justauth/autoconfigure/JustAuthProperties.java +++ b/src/main/java/com/xkcoding/justauth/autoconfigure/JustAuthProperties.java @@ -20,10 +20,10 @@ package com.xkcoding.justauth.autoconfigure; import lombok.Getter; import lombok.Setter; import me.zhyd.oauth.config.AuthConfig; -import me.zhyd.oauth.config.AuthDefaultSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; +import java.net.Proxy; import java.util.HashMap; import java.util.Map; @@ -47,7 +47,13 @@ public class JustAuthProperties { /** * JustAuth 默认配置 */ - private Map type = new HashMap<>(); + private Map type = new HashMap<>(); + + + /** + * http 相关的配置,可设置请求超时时间和代理配置 + */ + private JustAuthHttpConfig httpConfig; /** * JustAuth 自定义配置 @@ -61,4 +67,19 @@ public class JustAuthProperties { @NestedConfigurationProperty private CacheProperties cache = new CacheProperties(); + @Getter + @Setter + public static class JustAuthProxyConfig { + private String type = Proxy.Type.HTTP.name(); + private String hostname; + private int port; + } + + @Getter + @Setter + public static class JustAuthHttpConfig { + private int timeout; + private Map proxy; + } + } diff --git a/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/src/main/resources/META-INF/additional-spring-configuration-metadata.json index b241307..091e435 100644 --- a/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -42,10 +42,6 @@ "value": "CODING", "description": "CODING." }, - { - "value": "TENCENT_CLOUD", - "description": "TENCENT_CLOUD." - }, { "value": "OSCHINA", "description": "OSCHINA." @@ -58,10 +54,6 @@ "value": "QQ", "description": "QQ." }, - { - "value": "WECHAT", - "description": "WECHAT." - }, { "value": "TAOBAO", "description": "TAOBAO." @@ -109,6 +101,50 @@ { "value": "STACK_OVERFLOW", "description": "STACK_OVERFLOW." + }, + { + "value": "HUAWEI", + "description": "HUAWEI." + }, + { + "value": "WECHAT_OPEN", + "description": "WeChat open platform." + }, + { + "value": "WECHAT_ENTERPRISE", + "description": "WeChat enterprise platform." + }, + { + "value": "WECHAT_MP", + "description": "WeChat Official Platform." + }, + { + "value": "KUJIALE", + "description": "KUJIALE." + }, + { + "value": "GITLAB", + "description": "GITLAB." + }, + { + "value": "MEITUAN", + "description": "MEITUAN." + }, + { + "value": "ELEME", + "description": "ELEME." + }, + { + "value": "GITLAB", + "description": "GITLAB." + }, + { + "value": "TWITTER", + "description": "TWITTER." + }, + { + "value": "ALIYUN", + "description": "ALIYUN." } ], "parameters": {