diff --git a/src/main/java/me/zhyd/oauth/authorization/AlipayAuthorization.java b/src/main/java/me/zhyd/oauth/authorization/AlipayAuthorization.java
new file mode 100644
index 0000000000000000000000000000000000000000..c5655fe35034aebf998913a1ceeb1bd80dbd5c5b
--- /dev/null
+++ b/src/main/java/me/zhyd/oauth/authorization/AlipayAuthorization.java
@@ -0,0 +1,19 @@
+package me.zhyd.oauth.authorization;
+
+import me.zhyd.oauth.config.AuthConfig;
+import me.zhyd.oauth.utils.UrlBuilder;
+
+/**
+ * 支付宝授权
+ *
+ * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
+ * @version 1.0
+ * @since 1.8
+ */
+public class AlipayAuthorization implements Authorization {
+
+ @Override
+ public String getAuthorizeUrl(AuthConfig config) {
+ return UrlBuilder.getAlipayAuthorizeUrl(config.getClientId(), config.getRedirectUri());
+ }
+}
diff --git a/src/main/java/me/zhyd/oauth/authorization/Authorization.java b/src/main/java/me/zhyd/oauth/authorization/Authorization.java
new file mode 100644
index 0000000000000000000000000000000000000000..0db3cb9d380ef0e9460c945d53fb2711fbe3851f
--- /dev/null
+++ b/src/main/java/me/zhyd/oauth/authorization/Authorization.java
@@ -0,0 +1,15 @@
+package me.zhyd.oauth.authorization;
+
+import me.zhyd.oauth.config.AuthConfig;
+
+/**
+ * 授权接口,用来获取具体第三方平台的授权地址
+ *
+ * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
+ * @version 1.0
+ * @since 1.8
+ */
+public interface Authorization {
+
+ String getAuthorizeUrl(AuthConfig config);
+}
diff --git a/src/main/java/me/zhyd/oauth/authorization/AuthorizationFactory.java b/src/main/java/me/zhyd/oauth/authorization/AuthorizationFactory.java
new file mode 100644
index 0000000000000000000000000000000000000000..7d2c65cd01147ac855258ff6c04f5a0cf6557c6f
--- /dev/null
+++ b/src/main/java/me/zhyd/oauth/authorization/AuthorizationFactory.java
@@ -0,0 +1,74 @@
+package me.zhyd.oauth.authorization;
+
+import me.zhyd.oauth.exception.AuthException;
+import me.zhyd.oauth.model.AuthSource;
+import me.zhyd.oauth.request.ResponseStatus;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 授权工厂类,负责创建指定平台的授权类获取授权地址
+ *
+ * 使用策略模式 + 工厂模式 避免大量的if else(swatch)操作
+ *
+ * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
+ * @version 1.0
+ * @since 1.8
+ */
+public class AuthorizationFactory {
+
+ private static Map authorizationMap = new HashMap<>();
+ private static boolean loader = false;
+
+ private AuthorizationFactory() {
+
+ }
+
+ /**
+ * 根据第三方平台,获取具体的授权工具
+ *
+ * @param source 平台
+ * @return 具体的Authorization
+ */
+ public static Authorization getAuthorize(AuthSource source) {
+ if (null == source) {
+ throw new AuthException(ResponseStatus.NO_AUTH_SOURCE);
+ }
+ registerAllAuthorize();
+
+ Authorization authorization = authorizationMap.get(source.toString());
+ if (null == authorization) {
+ throw new AuthException(ResponseStatus.UNIDENTIFIED_PLATFORM);
+ }
+ return authorization;
+ }
+
+ /**
+ * 将所有Authorize的实现类注册到authorizeMap中,
+ * 每次增加新的平台都需要在这儿添加注册代码
+ */
+ private static void registerAllAuthorize() {
+ if (loader) {
+ return;
+ }
+ AuthorizationFactory.register(AuthSource.ALIPAY, new AlipayAuthorization());
+ AuthorizationFactory.register(AuthSource.BAIDU, new BaiduAuthorization());
+ AuthorizationFactory.register(AuthSource.CODING, new CodingAuthorization());
+ AuthorizationFactory.register(AuthSource.CSDN, new CsdnAuthorization());
+ AuthorizationFactory.register(AuthSource.DINGTALK, new DingTalkAuthorization());
+ AuthorizationFactory.register(AuthSource.GITEE, new GiteeAuthorization());
+ AuthorizationFactory.register(AuthSource.GITHUB, new GithubAuthorization());
+ AuthorizationFactory.register(AuthSource.OSCHINA, new OschinaAuthorization());
+ AuthorizationFactory.register(AuthSource.QQ, new QqAuthorization());
+ AuthorizationFactory.register(AuthSource.TAOBAO, new TaobaoAuthorization());
+ AuthorizationFactory.register(AuthSource.TENCEN_CLOUD, new TencentCloudAuthorization());
+ AuthorizationFactory.register(AuthSource.WECHAT, new WeChatAuthorization());
+ AuthorizationFactory.register(AuthSource.WEIBO, new WeiboAuthorization());
+ loader = true;
+ }
+
+ private static void register(AuthSource authSource, Authorization authorization) {
+ authorizationMap.put(authSource.toString(), authorization);
+ }
+}
diff --git a/src/main/java/me/zhyd/oauth/authorization/BaiduAuthorization.java b/src/main/java/me/zhyd/oauth/authorization/BaiduAuthorization.java
new file mode 100644
index 0000000000000000000000000000000000000000..c7e346965b80cca3b7b5f9cbbe511176fdc79712
--- /dev/null
+++ b/src/main/java/me/zhyd/oauth/authorization/BaiduAuthorization.java
@@ -0,0 +1,19 @@
+package me.zhyd.oauth.authorization;
+
+import me.zhyd.oauth.config.AuthConfig;
+import me.zhyd.oauth.utils.UrlBuilder;
+
+/**
+ * 百度授权
+ *
+ * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
+ * @version 1.0
+ * @since 1.8
+ */
+public class BaiduAuthorization implements Authorization {
+
+ @Override
+ public String getAuthorizeUrl(AuthConfig config) {
+ return UrlBuilder.getBaiduAuthorizeUrl(config.getClientId(), config.getRedirectUri());
+ }
+}
diff --git a/src/main/java/me/zhyd/oauth/authorization/CodingAuthorization.java b/src/main/java/me/zhyd/oauth/authorization/CodingAuthorization.java
new file mode 100644
index 0000000000000000000000000000000000000000..dd0128649410e9a5e1e735cc0e00b4871dbfc362
--- /dev/null
+++ b/src/main/java/me/zhyd/oauth/authorization/CodingAuthorization.java
@@ -0,0 +1,19 @@
+package me.zhyd.oauth.authorization;
+
+import me.zhyd.oauth.config.AuthConfig;
+import me.zhyd.oauth.utils.UrlBuilder;
+
+/**
+ * Coding授权
+ *
+ * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
+ * @version 1.0
+ * @since 1.8
+ */
+public class CodingAuthorization implements Authorization {
+
+ @Override
+ public String getAuthorizeUrl(AuthConfig config) {
+ return UrlBuilder.getCodingAuthorizeUrl(config.getClientId(), config.getRedirectUri());
+ }
+}
diff --git a/src/main/java/me/zhyd/oauth/authorization/CsdnAuthorization.java b/src/main/java/me/zhyd/oauth/authorization/CsdnAuthorization.java
new file mode 100644
index 0000000000000000000000000000000000000000..cc6474cfec990e0aba07d986459b9a22ce98fdd5
--- /dev/null
+++ b/src/main/java/me/zhyd/oauth/authorization/CsdnAuthorization.java
@@ -0,0 +1,19 @@
+package me.zhyd.oauth.authorization;
+
+import me.zhyd.oauth.config.AuthConfig;
+import me.zhyd.oauth.utils.UrlBuilder;
+
+/**
+ * CSDN授权
+ *
+ * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
+ * @version 1.0
+ * @since 1.8
+ */
+public class CsdnAuthorization implements Authorization {
+
+ @Override
+ public String getAuthorizeUrl(AuthConfig config) {
+ return UrlBuilder.getCsdnAuthorizeUrl(config.getClientId(), config.getRedirectUri());
+ }
+}
diff --git a/src/main/java/me/zhyd/oauth/authorization/DingTalkAuthorization.java b/src/main/java/me/zhyd/oauth/authorization/DingTalkAuthorization.java
new file mode 100644
index 0000000000000000000000000000000000000000..4bda11e456ba4301222553256b79aed05a66065b
--- /dev/null
+++ b/src/main/java/me/zhyd/oauth/authorization/DingTalkAuthorization.java
@@ -0,0 +1,19 @@
+package me.zhyd.oauth.authorization;
+
+import me.zhyd.oauth.config.AuthConfig;
+import me.zhyd.oauth.utils.UrlBuilder;
+
+/**
+ * 钉钉授权
+ *
+ * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
+ * @version 1.0
+ * @since 1.8
+ */
+public class DingTalkAuthorization implements Authorization {
+
+ @Override
+ public String getAuthorizeUrl(AuthConfig config) {
+ return UrlBuilder.getDingTalkQrConnectUrl(config.getClientId(), config.getRedirectUri());
+ }
+}
diff --git a/src/main/java/me/zhyd/oauth/authorization/GiteeAuthorization.java b/src/main/java/me/zhyd/oauth/authorization/GiteeAuthorization.java
new file mode 100644
index 0000000000000000000000000000000000000000..e02a476a8103050da891b87d6acc2e7e34972325
--- /dev/null
+++ b/src/main/java/me/zhyd/oauth/authorization/GiteeAuthorization.java
@@ -0,0 +1,19 @@
+package me.zhyd.oauth.authorization;
+
+import me.zhyd.oauth.config.AuthConfig;
+import me.zhyd.oauth.utils.UrlBuilder;
+
+/**
+ * 码云授权
+ *
+ * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
+ * @version 1.0
+ * @since 1.8
+ */
+public class GiteeAuthorization implements Authorization {
+
+ @Override
+ public String getAuthorizeUrl(AuthConfig config) {
+ return UrlBuilder.getGiteeAuthorizeUrl(config.getClientId(), config.getRedirectUri());
+ }
+}
diff --git a/src/main/java/me/zhyd/oauth/authorization/GithubAuthorization.java b/src/main/java/me/zhyd/oauth/authorization/GithubAuthorization.java
new file mode 100644
index 0000000000000000000000000000000000000000..f77ceb419c64886073ec861e4dd7d4a48746a6bb
--- /dev/null
+++ b/src/main/java/me/zhyd/oauth/authorization/GithubAuthorization.java
@@ -0,0 +1,19 @@
+package me.zhyd.oauth.authorization;
+
+import me.zhyd.oauth.config.AuthConfig;
+import me.zhyd.oauth.utils.UrlBuilder;
+
+/**
+ * Github授权
+ *
+ * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
+ * @version 1.0
+ * @since 1.8
+ */
+public class GithubAuthorization implements Authorization {
+
+ @Override
+ public String getAuthorizeUrl(AuthConfig config) {
+ return UrlBuilder.getGithubAuthorizeUrl(config.getClientId(), config.getRedirectUri());
+ }
+}
diff --git a/src/main/java/me/zhyd/oauth/authorization/OschinaAuthorization.java b/src/main/java/me/zhyd/oauth/authorization/OschinaAuthorization.java
new file mode 100644
index 0000000000000000000000000000000000000000..fa70aaf696b78e25d986864532edc4d7f1c65373
--- /dev/null
+++ b/src/main/java/me/zhyd/oauth/authorization/OschinaAuthorization.java
@@ -0,0 +1,19 @@
+package me.zhyd.oauth.authorization;
+
+import me.zhyd.oauth.config.AuthConfig;
+import me.zhyd.oauth.utils.UrlBuilder;
+
+/**
+ * 开源中国授权
+ *
+ * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
+ * @version 1.0
+ * @since 1.8
+ */
+public class OschinaAuthorization implements Authorization {
+
+ @Override
+ public String getAuthorizeUrl(AuthConfig config) {
+ return UrlBuilder.getOschinaAuthorizeUrl(config.getClientId(), config.getRedirectUri());
+ }
+}
diff --git a/src/main/java/me/zhyd/oauth/authorization/QqAuthorization.java b/src/main/java/me/zhyd/oauth/authorization/QqAuthorization.java
new file mode 100644
index 0000000000000000000000000000000000000000..f9002365b9cf79985b69eb58f6050670409d1b0e
--- /dev/null
+++ b/src/main/java/me/zhyd/oauth/authorization/QqAuthorization.java
@@ -0,0 +1,19 @@
+package me.zhyd.oauth.authorization;
+
+import me.zhyd.oauth.config.AuthConfig;
+import me.zhyd.oauth.utils.UrlBuilder;
+
+/**
+ * QQ授权
+ *
+ * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
+ * @version 1.0
+ * @since 1.8
+ */
+public class QqAuthorization implements Authorization {
+
+ @Override
+ public String getAuthorizeUrl(AuthConfig config) {
+ return UrlBuilder.getQqAuthorizeUrl(config.getClientId(), config.getRedirectUri());
+ }
+}
diff --git a/src/main/java/me/zhyd/oauth/authorization/TaobaoAuthorization.java b/src/main/java/me/zhyd/oauth/authorization/TaobaoAuthorization.java
new file mode 100644
index 0000000000000000000000000000000000000000..cdc8370b48b61eca6573b6621439ec970f805254
--- /dev/null
+++ b/src/main/java/me/zhyd/oauth/authorization/TaobaoAuthorization.java
@@ -0,0 +1,19 @@
+package me.zhyd.oauth.authorization;
+
+import me.zhyd.oauth.config.AuthConfig;
+import me.zhyd.oauth.utils.UrlBuilder;
+
+/**
+ * 淘宝授权
+ *
+ * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
+ * @version 1.0
+ * @since 1.8
+ */
+public class TaobaoAuthorization implements Authorization {
+
+ @Override
+ public String getAuthorizeUrl(AuthConfig config) {
+ return UrlBuilder.getTaobaoAuthorizeUrl(config.getClientId(), config.getRedirectUri());
+ }
+}
diff --git a/src/main/java/me/zhyd/oauth/authorization/TencentCloudAuthorization.java b/src/main/java/me/zhyd/oauth/authorization/TencentCloudAuthorization.java
new file mode 100644
index 0000000000000000000000000000000000000000..dcb94fe5210a82e4fbd0d7a9c3530f3d129028a9
--- /dev/null
+++ b/src/main/java/me/zhyd/oauth/authorization/TencentCloudAuthorization.java
@@ -0,0 +1,19 @@
+package me.zhyd.oauth.authorization;
+
+import me.zhyd.oauth.config.AuthConfig;
+import me.zhyd.oauth.utils.UrlBuilder;
+
+/**
+ * 腾讯云授权
+ *
+ * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
+ * @version 1.0
+ * @since 1.8
+ */
+public class TencentCloudAuthorization implements Authorization {
+
+ @Override
+ public String getAuthorizeUrl(AuthConfig config) {
+ return UrlBuilder.getTencentCloudAuthorizeUrl(config.getClientId(), config.getRedirectUri());
+ }
+}
diff --git a/src/main/java/me/zhyd/oauth/authorization/WeChatAuthorization.java b/src/main/java/me/zhyd/oauth/authorization/WeChatAuthorization.java
new file mode 100644
index 0000000000000000000000000000000000000000..a7f208a25e6c8a35b3299c334288f1a2c593c419
--- /dev/null
+++ b/src/main/java/me/zhyd/oauth/authorization/WeChatAuthorization.java
@@ -0,0 +1,19 @@
+package me.zhyd.oauth.authorization;
+
+import me.zhyd.oauth.config.AuthConfig;
+import me.zhyd.oauth.utils.UrlBuilder;
+
+/**
+ * 微信授权
+ *
+ * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
+ * @version 1.0
+ * @since 1.8
+ */
+public class WeChatAuthorization implements Authorization {
+
+ @Override
+ public String getAuthorizeUrl(AuthConfig config) {
+ return UrlBuilder.getWeChatAuthorizeUrl(config.getClientId(), config.getRedirectUri());
+ }
+}
diff --git a/src/main/java/me/zhyd/oauth/authorization/WeiboAuthorization.java b/src/main/java/me/zhyd/oauth/authorization/WeiboAuthorization.java
new file mode 100644
index 0000000000000000000000000000000000000000..2083233af1301d845d61fc4cc3c46d8921a40bc1
--- /dev/null
+++ b/src/main/java/me/zhyd/oauth/authorization/WeiboAuthorization.java
@@ -0,0 +1,19 @@
+package me.zhyd.oauth.authorization;
+
+import me.zhyd.oauth.config.AuthConfig;
+import me.zhyd.oauth.utils.UrlBuilder;
+
+/**
+ * 微博授权
+ *
+ * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
+ * @version 1.0
+ * @since 1.8
+ */
+public class WeiboAuthorization implements Authorization {
+
+ @Override
+ public String getAuthorizeUrl(AuthConfig config) {
+ return UrlBuilder.getWeiboAuthorizeUrl(config.getClientId(), config.getRedirectUri());
+ }
+}
diff --git a/src/main/java/me/zhyd/oauth/model/AuthSource.java b/src/main/java/me/zhyd/oauth/model/AuthSource.java
index 7eac69de68d49da6964245e4013fc7fb66edbdb2..55631b687452a2fb6eb0f12bed611c717d101fba 100644
--- a/src/main/java/me/zhyd/oauth/model/AuthSource.java
+++ b/src/main/java/me/zhyd/oauth/model/AuthSource.java
@@ -1,6 +1,8 @@
package me.zhyd.oauth.model;
/**
+ * 授权来源(平台)
+ *
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
diff --git a/src/main/java/me/zhyd/oauth/request/BaseAuthRequest.java b/src/main/java/me/zhyd/oauth/request/BaseAuthRequest.java
index afc839c28c0a6649d6d895f177712c24cd6b6e65..86254ef941fa6760f0c5a93b02faf98bef71c7cf 100644
--- a/src/main/java/me/zhyd/oauth/request/BaseAuthRequest.java
+++ b/src/main/java/me/zhyd/oauth/request/BaseAuthRequest.java
@@ -1,6 +1,7 @@
package me.zhyd.oauth.request;
import lombok.Data;
+import me.zhyd.oauth.authorization.AuthorizationFactory;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthResponse;
@@ -8,7 +9,6 @@ import me.zhyd.oauth.model.AuthSource;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.utils.AuthConfigChecker;
-import me.zhyd.oauth.utils.UrlBuilder;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
@@ -44,52 +44,6 @@ public abstract class BaseAuthRequest implements AuthRequest {
@Override
public String authorize() {
- String authorizeUrl = null;
- switch (source) {
- case WEIBO:
- authorizeUrl = UrlBuilder.getWeiboAuthorizeUrl(config.getClientId(), config.getRedirectUri());
- break;
- case BAIDU:
- authorizeUrl = UrlBuilder.getBaiduAuthorizeUrl(config.getClientId(), config.getRedirectUri());
- break;
- case DINGTALK:
- authorizeUrl = UrlBuilder.getDingTalkQrConnectUrl(config.getClientId(), config.getRedirectUri());
- break;
- case GITEE:
- authorizeUrl = UrlBuilder.getGiteeAuthorizeUrl(config.getClientId(), config.getRedirectUri());
- break;
- case GITHUB:
- authorizeUrl = UrlBuilder.getGithubAuthorizeUrl(config.getClientId(), config.getRedirectUri());
- break;
- case CSDN:
- authorizeUrl = UrlBuilder.getCsdnAuthorizeUrl(config.getClientId(), config.getRedirectUri());
- break;
- case CODING:
- authorizeUrl = UrlBuilder.getCodingAuthorizeUrl(config.getClientId(), config.getRedirectUri());
- break;
- case TENCEN_CLOUD:
- authorizeUrl = UrlBuilder.getTencentCloudAuthorizeUrl(config.getClientId(), config.getRedirectUri());
- break;
- case OSCHINA:
- authorizeUrl = UrlBuilder.getOschinaAuthorizeUrl(config.getClientId(), config.getRedirectUri());
- break;
- case ALIPAY:
- authorizeUrl = UrlBuilder.getAlipayAuthorizeUrl(config.getClientId(), config.getRedirectUri());
- break;
- case QQ:
- authorizeUrl = UrlBuilder.getQqAuthorizeUrl(config.getClientId(), config.getRedirectUri());
- break;
- case WECHAT:
- authorizeUrl = UrlBuilder.getWeChatAuthorizeUrl(config.getClientId(), config.getRedirectUri());
- break;
- case TAOBAO:
- authorizeUrl = UrlBuilder.getTaobaoAuthorizeUrl(config.getClientId(), config.getRedirectUri());
- break;
- case GOOGLE:
- break;
- default:
- break;
- }
- return authorizeUrl;
+ return AuthorizationFactory.getAuthorize(source).getAuthorizeUrl(config);
}
}
diff --git a/src/main/java/me/zhyd/oauth/request/ResponseStatus.java b/src/main/java/me/zhyd/oauth/request/ResponseStatus.java
index 73c4add9262e2fb77a4c3c0be8d5c7e6e87218be..378669b52ba9a0aeeaa27d966e20f4cfbd38ba1c 100644
--- a/src/main/java/me/zhyd/oauth/request/ResponseStatus.java
+++ b/src/main/java/me/zhyd/oauth/request/ResponseStatus.java
@@ -11,6 +11,8 @@ public enum ResponseStatus {
NOT_IMPLEMENTED(5001, "Not Implemented"),
PARAMETER_INCOMPLETE(5002, "Parameter incomplete"),
UNSUPPORTED(5003, "Unsupported operation"),
+ NO_AUTH_SOURCE(5004, "AuthSource cannot be null"),
+ UNIDENTIFIED_PLATFORM(5005, "Unidentified platform"),
;
private int code;