diff --git a/README.md b/README.md
index f462fd23182e1bea73a298dd13f90313559274b1..1d051054c3504cd8b5c7c7f87b852c5c166f690f 100644
--- a/README.md
+++ b/README.md
@@ -29,6 +29,7 @@
|
|
|
+ |
|
|
@@ -42,8 +43,9 @@
百度 |
CSDN |
Coding |
- 腾讯云开发者平台 |
+ 腾讯云 |
OSChina |
+ 支付宝 |
QQ |
微信 |
@@ -57,13 +59,15 @@
JustAuth,如你所见,它仅仅是一个**第三方授权登录**的**工具类库**,它可以让我们脱离繁琐的第三方登录SDK,让登录变得**So easy!**
+项目开源地址:[gitee](https://gitee.com/yadong.zhang/JustAuth) | [github](https://github.com/zhangyd-c/JustAuth)
+
## 快速开始
- 引入依赖
```xml
me.zhyd.oauth
JustAuth
- 1.0.0
+ 1.0.1
```
- 调用api
@@ -80,7 +84,10 @@ authRequest.authorize();
authRequest.login("code");
```
-具体的例子可以参考:[实现Gitee授权登录](http://t.cn/ExDKxQs)
+具体的例子可以参考:
+
+- [实现Gitee授权登录](http://t.cn/ExDKxQs)
+- [实现Github授权登录](http://t.cn/EJ0Fxqo)
#### API列表
| :computer: 平台 | :coffee: API类 | :page_facing_up: SDK |
@@ -90,10 +97,11 @@ authRequest.login("code");
| | [AuthWeiboRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java) | 参考文档 |
| | [AuthDingTalkRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthDingTalkRequest.java) | 参考文档 |
| | [AuthBaiduRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthBaiduRequest.java) | 参考文档 |
-| | [AuthCsdnRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthCsdnRequest.java) | 待续 |
| | [AuthCodingRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthCodingRequest.java) | 参考文档 |
| | [AuthTencentCloudRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthTencentCloudRequest.java) | 参考文档 |
| | [AuthOschinaRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthOschinaRequest.java) | 参考文档 |
+| | [AuthAlipayRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java) | 参考文档 |
+| | [AuthCsdnRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthCsdnRequest.java) | 待续 |
| | AuthQqRequest | 参考文档 |
| | AuthWechatRequest | 待续 |
@@ -103,13 +111,6 @@ authRequest.login("code");
另外,期待您和我一起完善这个项目!
-
-## 本项目同步开源到Gitee和Github
-
-[gitee](https://gitee.com/yadong.zhang/JustAuth)
-
-[github](https://github.com/zhangyd-c/JustAuth)
-
## 致谢
在项目立项初期,也对当前开源圈的一些相同类型的项目作过调研,同时本项目也参考过这些项目,再次感谢开源圈内的朋友。
@@ -139,10 +140,6 @@ authRequest.login("code");
![百度授权登录](https://images.gitee.com/uploads/images/2019/0221/140607_ebf1dcb6_784199.png "百度授权登录")
-#### 授权csdn
-
-待续
-
#### 授权coding
![Coding授权登录](https://images.gitee.com/uploads/images/2019/0224/192106_fd53b3d7_784199.png "Coding授权登录")
@@ -155,6 +152,14 @@ authRequest.login("code");
![授权oschina登录](https://images.gitee.com/uploads/images/2019/0322/230652_05b4fd8a_784199.png "授权oschina")
+#### 授权支付宝
+
+待续
+
+#### 授权csdn
+
+待续
+
#### 授权qq
待续
diff --git a/pom.xml b/pom.xml
index 898ff41a0d5836b5f1465e1a1f87ae4d096cd999..4a2cdc83092a945e6108074bc833282b1f47be1f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,12 +1,12 @@
-
4.0.0
me.zhyd.oauth
JustAuth
- 1.0.0
+ 1.0.1
JustAuth
https://gitee.com/yadong.zhang/JustAuth
@@ -45,6 +45,7 @@
4.11
1.2.44
1.28.0
+ 3.7.4.ALL
@@ -74,6 +75,13 @@
google-api-client
${google-api-version}
-->
+
+
+ com.alipay.sdk
+ alipay-sdk-java
+ ${alipay-sdk-version}
+ compile
+
diff --git a/src/main/java/me/zhyd/oauth/config/AuthConfig.java b/src/main/java/me/zhyd/oauth/config/AuthConfig.java
index 6cd25daa07096b116a1dce8667136f088f862c16..efe6873d193ce3303fd024ccdea81617c0a1a359 100644
--- a/src/main/java/me/zhyd/oauth/config/AuthConfig.java
+++ b/src/main/java/me/zhyd/oauth/config/AuthConfig.java
@@ -1,7 +1,9 @@
package me.zhyd.oauth.config;
+import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
+import lombok.NoArgsConstructor;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
@@ -12,6 +14,8 @@ import lombok.Getter;
*/
@Getter
@Builder
+@NoArgsConstructor
+@AllArgsConstructor
public class AuthConfig {
private String clientId;
private String clientSecret;
@@ -19,4 +23,9 @@ public class AuthConfig {
* 登录成功后的回调地址
*/
private String redirectUri;
+
+ /**
+ * 支付宝公钥:当选择支付宝登录时,该值可用
+ */
+ private String alipayPublicKey;
}
diff --git a/src/main/java/me/zhyd/oauth/consts/ApiUrl.java b/src/main/java/me/zhyd/oauth/consts/ApiUrl.java
index d2b76358edd3723400ddca5c27a6adbaa5cf2cad..ff420b295117218afdcd7e67c0b19b5ce9fa3ec7 100644
--- a/src/main/java/me/zhyd/oauth/consts/ApiUrl.java
+++ b/src/main/java/me/zhyd/oauth/consts/ApiUrl.java
@@ -269,6 +269,35 @@ public enum ApiUrl {
throw new AuthException(ResponseStatus.UNSUPPORTED);
}
+ @Override
+ public String refresh() {
+ throw new AuthException(ResponseStatus.UNSUPPORTED);
+ }
+ },
+ /**
+ * 支付宝
+ */
+ ALIPAY {
+ @Override
+ public String authorize() {
+ return "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm";
+ }
+
+ @Override
+ public String accessToken() {
+ return "https://openapi.alipay.com/gateway.do";
+ }
+
+ @Override
+ public String userInfo() {
+ return "https://openapi.alipay.com/gateway.do";
+ }
+
+ @Override
+ public String revoke() {
+ throw new AuthException(ResponseStatus.UNSUPPORTED);
+ }
+
@Override
public String refresh() {
throw new AuthException(ResponseStatus.UNSUPPORTED);
diff --git a/src/main/java/me/zhyd/oauth/model/AuthSource.java b/src/main/java/me/zhyd/oauth/model/AuthSource.java
index ceee08b718ed740a4e80ed4777d45fddd422f453..c887e73b8c389b0909b9aacacf7752166a367f8d 100644
--- a/src/main/java/me/zhyd/oauth/model/AuthSource.java
+++ b/src/main/java/me/zhyd/oauth/model/AuthSource.java
@@ -17,6 +17,7 @@ public enum AuthSource {
CODING,
OSCHINA,
TENCEN_CLOUD,
+ ALIPAY,
QQ,
WECHAT,
diff --git a/src/main/java/me/zhyd/oauth/model/AuthUserGender.java b/src/main/java/me/zhyd/oauth/model/AuthUserGender.java
index 10c7cfa65f21eeb67f29cdcee8ba9cca89b09fbc..28d63e9ecf565f73a05f74112f77427f2264be4a 100644
--- a/src/main/java/me/zhyd/oauth/model/AuthUserGender.java
+++ b/src/main/java/me/zhyd/oauth/model/AuthUserGender.java
@@ -21,7 +21,7 @@ public enum AuthUserGender {
if (code == null) {
return UNKNOW;
}
- if ("m".equals(code) || "男".equals(code) || "1".equals(code) || "male".equalsIgnoreCase(code)) {
+ if ("m".equals(code) || "男".equals(code) || "1".equals(code) || "male".equalsIgnoreCase(code) || "F".equalsIgnoreCase(code)) {
return MALE;
}
if ("f".equals(code) || "女".equals(code) || "0".equals(code) || "female".equalsIgnoreCase(code)) {
diff --git a/src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java b/src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java
new file mode 100644
index 0000000000000000000000000000000000000000..e0f50d3ab1058421843bacbf6754c0a3fb571c98
--- /dev/null
+++ b/src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java
@@ -0,0 +1,75 @@
+package me.zhyd.oauth.request;
+
+import com.alipay.api.AlipayApiException;
+import com.alipay.api.AlipayClient;
+import com.alipay.api.DefaultAlipayClient;
+import com.alipay.api.request.AlipaySystemOauthTokenRequest;
+import com.alipay.api.request.AlipayUserInfoShareRequest;
+import com.alipay.api.response.AlipaySystemOauthTokenResponse;
+import com.alipay.api.response.AlipayUserInfoShareResponse;
+import me.zhyd.oauth.config.AuthConfig;
+import me.zhyd.oauth.consts.ApiUrl;
+import me.zhyd.oauth.exception.AuthException;
+import me.zhyd.oauth.model.AuthSource;
+import me.zhyd.oauth.model.AuthUser;
+import me.zhyd.oauth.model.AuthUserGender;
+import me.zhyd.oauth.utils.StringUtils;
+
+/**
+ * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
+ * @version 1.0
+ * @website https://www.zhyd.me
+ * @date 2019/3/25 22:48
+ * @since 1.8
+ */
+public class AuthAlipayRequest extends BaseAuthRequest {
+
+ private AlipayClient alipayClient;
+
+ public AuthAlipayRequest(AuthConfig config) {
+ super(config, AuthSource.ALIPAY);
+ this.alipayClient = new DefaultAlipayClient(ApiUrl.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(), "json", "UTF-8", config.getAlipayPublicKey(), "RSA2");
+ }
+
+ @Override
+ protected String getAccessToken(String code) {
+ AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
+ request.setGrantType("authorization_code");
+ request.setCode(code);
+ AlipaySystemOauthTokenResponse response = null;
+ try {
+ response = this.alipayClient.execute(request);
+ } catch (Exception e) {
+ throw new AuthException("Unable to get token from alipay using code [" + code + "]", e);
+ }
+ if (!response.isSuccess()) {
+ throw new AuthException(response.getSubMsg());
+ }
+ return response.getAccessToken();
+ }
+
+ @Override
+ protected AuthUser getUserInfo(String accessToken) {
+ AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
+ AlipayUserInfoShareResponse response = null;
+ try {
+ response = this.alipayClient.execute(request, accessToken);
+ } catch (AlipayApiException e) {
+ throw new AuthException(e.getErrMsg(), e);
+ }
+ if (!response.isSuccess()) {
+ throw new AuthException(response.getSubMsg());
+ }
+ String province = response.getProvince(),
+ city = response.getCity();
+ return AuthUser.builder()
+ .username(response.getUserName())
+ .nickname(response.getNickName())
+ .avatar(response.getAvatar())
+ .location(String.format("%s %s", StringUtils.isEmpty(province) ? "" : province, StringUtils.isEmpty(city) ? "" : city))
+ .gender(AuthUserGender.getRealGender(response.getGender()))
+ .accessToken(accessToken)
+ .source(AuthSource.ALIPAY)
+ .build();
+ }
+}
diff --git a/src/main/java/me/zhyd/oauth/request/BaseAuthRequest.java b/src/main/java/me/zhyd/oauth/request/BaseAuthRequest.java
index b138e038bc6ab46f870a46eb67e65dac1e5bf7b6..ebbd2132fe1eb2a19e34555b917347a2623ec072 100644
--- a/src/main/java/me/zhyd/oauth/request/BaseAuthRequest.java
+++ b/src/main/java/me/zhyd/oauth/request/BaseAuthRequest.java
@@ -35,9 +35,12 @@ public abstract class BaseAuthRequest implements AuthRequest {
@Override
public AuthResponse login(String code) {
- return AuthResponse.builder()
- .data(this.getUserInfo(this.getAccessToken(code)))
- .build();
+ try {
+ AuthUser user = this.getUserInfo(this.getAccessToken(code));
+ return AuthResponse.builder().data(user).build();
+ } catch (Exception e) {
+ return AuthResponse.builder().code(500).msg(e.getMessage()).build();
+ }
}
@Override
@@ -71,6 +74,9 @@ public abstract class BaseAuthRequest implements AuthRequest {
case OSCHINA:
authorizeUrl = UrlBuilder.getOschinaAuthorizeUrl(config.getClientId(), config.getRedirectUri());
break;
+ case ALIPAY:
+ authorizeUrl = UrlBuilder.getAlipayAuthorizeUrl(config.getClientId(), config.getRedirectUri());
+ break;
case QQ:
break;
case WECHAT:
diff --git a/src/main/java/me/zhyd/oauth/utils/UrlBuilder.java b/src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
index 49160dfbe2168ec4b4af1bb0a6bc2a2b5c0f669c..0ff11283257fef1ce4c9782ff9fb221f034dfa54 100644
--- a/src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
+++ b/src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
@@ -51,6 +51,8 @@ public class UrlBuilder {
private static final String OSCHINA_USER_INFO_PATTERN = "{0}?access_token={1}&dataType=json";
private static final String OSCHINA_AUTHORIZE_PATTERN = "{0}?client_id={1}&response_type=code&redirect_uri={2}";
+ private static final String ALIPAY_AUTHORIZE_PATTERN = "{0}?app_id={1}&scope=auth_user&redirect_uri={2}&state=init";
+
/**
* 获取githubtoken的接口地址
*
@@ -353,4 +355,15 @@ public class UrlBuilder {
public static String getOschinaAuthorizeUrl(String clientId, String redirectUrl) {
return MessageFormat.format(OSCHINA_AUTHORIZE_PATTERN, ApiUrl.OSCHINA.authorize(), clientId, redirectUrl);
}
+
+ /**
+ * 获取alipay授权地址
+ *
+ * @param clientId alipay 应用的Client ID
+ * @param redirectUrl alipay 应用授权成功后的回调地址
+ * @return full url
+ */
+ public static String getAlipayAuthorizeUrl(String clientId, String redirectUrl) {
+ return MessageFormat.format(ALIPAY_AUTHORIZE_PATTERN, ApiUrl.ALIPAY.authorize(), clientId, redirectUrl);
+ }
}