✨ 集成支付宝授权登录
This commit is contained in:
@@ -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