✨ 集成支付宝授权登录
This commit is contained in:
35
README.md
35
README.md
@@ -29,6 +29,7 @@
|
||||
<td align="center" width="200"><a href="https://coding.net"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/coding.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://dev.tencent.com/"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/tencent_cloud.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://www.oschina.net"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/oschinas.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://www.alipay.com"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/alipay.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://connect.qq.com/devuser.html#/"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/qq.png" width="30"></a></td>
|
||||
<td align="center" width="200"><a href="https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN"><img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/wechats.png" width="30"></a></td>
|
||||
</tr>
|
||||
@@ -42,8 +43,9 @@
|
||||
<td align="center" width="200"><a href="#授权百度">百度</a></td>
|
||||
<td align="center" width="200"><a href="#授权csdn">CSDN</a></td>
|
||||
<td align="center" width="200"><a href="#授权coding">Coding</a></td>
|
||||
<td align="center" width="200"><a href="#授权腾讯云开发者平台" title="coding升级后就变成腾讯云开发者平台了">腾讯云开发者平台</a></td>
|
||||
<td align="center" width="200"><a href="#授权腾讯云开发者平台" title="coding升级后就变成腾讯云开发者平台了">腾讯云</a></td>
|
||||
<td align="center" width="200"><a href="#授权oschina">OSChina</a></td>
|
||||
<td align="center" width="200"><a href="#授权支付宝">支付宝</a></td>
|
||||
<td align="center" width="200"><a href="#授权qq">QQ</a></td>
|
||||
<td align="center" width="200"><a href="#授权微信">微信</a></td>
|
||||
</tr>
|
||||
@@ -57,13 +59,15 @@
|
||||
|
||||
JustAuth,如你所见,它仅仅是一个**第三方授权登录**的**工具类库**,它可以让我们脱离繁琐的第三方登录SDK,让登录变得**So easy!**
|
||||
|
||||
项目开源地址:[gitee](https://gitee.com/yadong.zhang/JustAuth) | [github](https://github.com/zhangyd-c/JustAuth)
|
||||
|
||||
## 快速开始
|
||||
- 引入依赖
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>me.zhyd.oauth</groupId>
|
||||
<artifactId>JustAuth</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.0.1</version>
|
||||
</dependency>
|
||||
```
|
||||
- 调用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");
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/weibo.png" width="20"> | [AuthWeiboRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java) | <a href="https://open.weibo.com/apps?_blank" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/dingding.png" width="20"> | [AuthDingTalkRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthDingTalkRequest.java) | <a href="https://open-doc.dingtalk.com/microapp/serverapi2/kymkv6" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/baidu.png" width="20"> | [AuthBaiduRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthBaiduRequest.java) | <a href="https://developer.baidu.com/" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/csdn.png" width="20"> | [AuthCsdnRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthCsdnRequest.java) | 待续 |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/coding.png" width="25"> | [AuthCodingRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthCodingRequest.java) | <a href="https://open.coding.net/references/oauth/" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/tencent_cloud.png" width="25"> | [AuthTencentCloudRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthTencentCloudRequest.java) | <a href="https://dev.tencent.com/help/doc/faq/b4e5b7aee786/oauth" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/oschinas.png" width="20"> | [AuthOschinaRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthOschinaRequest.java) | <a href="https://www.oschina.net/openapi/docs/openapi_user" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/alipay.png" width="20"> | [AuthAlipayRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java) | <a href="https://alipay.open.taobao.com/docs/doc.htm?spm=a219a.7629140.0.0.336d4b70GUKXOl&treeId=193&articleId=105809&docType=1" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/csdn.png" width="20"> | [AuthCsdnRequest](https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthCsdnRequest.java) | 待续 |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/qq.png" width="20"> | AuthQqRequest | <a href="https://connect.qq.com/" target="_blank">参考文档</a> |
|
||||
| <img src="https://gitee.com/yadong.zhang/static/raw/master/JustAuth/wechats.png" width="20"> | 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");
|
||||
|
||||

|
||||
|
||||
#### 授权csdn
|
||||
|
||||
待续
|
||||
|
||||
#### 授权coding
|
||||
|
||||

|
||||
@@ -155,6 +152,14 @@ authRequest.login("code");
|
||||
|
||||

|
||||
|
||||
#### 授权支付宝
|
||||
|
||||
待续
|
||||
|
||||
#### 授权csdn
|
||||
|
||||
待续
|
||||
|
||||
#### 授权qq
|
||||
|
||||
待续
|
||||
|
||||
12
pom.xml
12
pom.xml
@@ -1,12 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>me.zhyd.oauth</groupId>
|
||||
<artifactId>JustAuth</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.0.1</version>
|
||||
|
||||
<name>JustAuth</name>
|
||||
<url>https://gitee.com/yadong.zhang/JustAuth</url>
|
||||
@@ -45,6 +45,7 @@
|
||||
<junit-version>4.11</junit-version>
|
||||
<fastjson-version>1.2.44</fastjson-version>
|
||||
<google-api-version>1.28.0</google-api-version>
|
||||
<alipay-sdk-version>3.7.4.ALL</alipay-sdk-version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -74,6 +75,13 @@
|
||||
<artifactId>google-api-client</artifactId>
|
||||
<version>${google-api-version}</version>
|
||||
</dependency>-->
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alipay.sdk</groupId>
|
||||
<artifactId>alipay-sdk-java</artifactId>
|
||||
<version>${alipay-sdk-version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -17,6 +17,7 @@ public enum AuthSource {
|
||||
CODING,
|
||||
OSCHINA,
|
||||
TENCEN_CLOUD,
|
||||
ALIPAY,
|
||||
|
||||
QQ,
|
||||
WECHAT,
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
75
src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java
Normal file
75
src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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:
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user