From 75176571b77ecf38b4f50e0d2e9556040f9cd82f Mon Sep 17 00:00:00 2001 From: "yadong.zhang" Date: Wed, 27 Mar 2019 18:34:53 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E9=9B=86=E6=88=90=E6=94=AF?= =?UTF-8?q?=E4=BB=98=E5=AE=9D=E6=8E=88=E6=9D=83=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 35 +++++---- pom.xml | 12 ++- .../java/me/zhyd/oauth/config/AuthConfig.java | 9 +++ .../java/me/zhyd/oauth/consts/ApiUrl.java | 29 +++++++ .../java/me/zhyd/oauth/model/AuthSource.java | 1 + .../me/zhyd/oauth/model/AuthUserGender.java | 2 +- .../zhyd/oauth/request/AuthAlipayRequest.java | 75 +++++++++++++++++++ .../zhyd/oauth/request/BaseAuthRequest.java | 12 ++- .../java/me/zhyd/oauth/utils/UrlBuilder.java | 13 ++++ 9 files changed, 167 insertions(+), 21 deletions(-) create mode 100644 src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java diff --git a/README.md b/README.md index f462fd2..1d05105 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 898ff41..4a2cdc8 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 6cd25da..efe6873 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 d2b7635..ff420b2 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 ceee08b..c887e73 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 10c7cfa..28d63e9 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 0000000..e0f50d3 --- /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 b138e03..ebbd213 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 49160df..0ff1128 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); + } } -- GitLab