提交 74ee17b2 编写于 作者: 智布道's avatar 智布道 👁 提交者: Gitee

!27 获取三方实例 部分 进行重构,使之可以进行更简单的获取对应的实例。

Merge pull request !27 from 陈宁/master
package me.zhyd.oauth.config; package me.zhyd.oauth.config;
import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.enums.AuthResponseStatus; import me.zhyd.oauth.enums.AuthResponseStatus;
import me.zhyd.oauth.exception.AuthException; import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.request.*;
import java.util.Arrays;
/** /**
* JustAuth内置的各api需要的url, 用枚举类分平台类型管理 * JustAuth内置的各api需要的url, 用枚举类分平台类型管理
...@@ -13,7 +17,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -13,7 +17,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* Github * Github
*/ */
GITHUB { GITHUB(AuthGithubRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://github.com/login/oauth/authorize"; return "https://github.com/login/oauth/authorize";
...@@ -32,7 +36,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -32,7 +36,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* 新浪微博 * 新浪微博
*/ */
WEIBO { WEIBO(AuthWeiboRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://api.weibo.com/oauth2/authorize"; return "https://api.weibo.com/oauth2/authorize";
...@@ -56,7 +60,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -56,7 +60,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* gitee * gitee
*/ */
GITEE { GITEE(AuthGiteeRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://gitee.com/oauth/authorize"; return "https://gitee.com/oauth/authorize";
...@@ -75,7 +79,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -75,7 +79,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* 钉钉扫码登录 * 钉钉扫码登录
*/ */
DINGTALK { DINGTALK(AuthDingTalkRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://oapi.dingtalk.com/connect/qrconnect"; return "https://oapi.dingtalk.com/connect/qrconnect";
...@@ -94,7 +98,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -94,7 +98,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* 钉钉账号登录 * 钉钉账号登录
*/ */
DINGTALK_ACCOUNT { DINGTALK_ACCOUNT(AuthDingTalkAccountRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://oapi.dingtalk.com/connect/oauth2/sns_authorize"; return "https://oapi.dingtalk.com/connect/oauth2/sns_authorize";
...@@ -113,7 +117,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -113,7 +117,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* 百度 * 百度
*/ */
BAIDU { BAIDU(AuthBaiduRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://openapi.baidu.com/oauth/2.0/authorize"; return "https://openapi.baidu.com/oauth/2.0/authorize";
...@@ -142,7 +146,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -142,7 +146,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* csdn * csdn
*/ */
CSDN { CSDN(AuthCsdnRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://api.csdn.net/oauth2/authorize"; return "https://api.csdn.net/oauth2/authorize";
...@@ -164,7 +168,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -164,7 +168,7 @@ public enum AuthDefaultSource implements AuthSource {
* 参考 https://help.coding.net/docs/project/open/oauth.html#%E7%94%A8%E6%88%B7%E6%8E%88%E6%9D%83 中的说明, * 参考 https://help.coding.net/docs/project/open/oauth.html#%E7%94%A8%E6%88%B7%E6%8E%88%E6%9D%83 中的说明,
* 新版的 coding API 地址需要传入用户团队名,这儿使用动态参数,方便在 request 中使用 * 新版的 coding API 地址需要传入用户团队名,这儿使用动态参数,方便在 request 中使用
*/ */
CODING { CODING(AuthCodingRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://%s.coding.net/oauth_authorize.html"; return "https://%s.coding.net/oauth_authorize.html";
...@@ -183,7 +187,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -183,7 +187,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* oschina 开源中国 * oschina 开源中国
*/ */
OSCHINA { OSCHINA(AuthOschinaRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://www.oschina.net/action/oauth2/authorize"; return "https://www.oschina.net/action/oauth2/authorize";
...@@ -202,7 +206,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -202,7 +206,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* 支付宝 * 支付宝
*/ */
ALIPAY { ALIPAY(AuthAlipayRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm"; return "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm";
...@@ -221,7 +225,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -221,7 +225,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* QQ * QQ
*/ */
QQ { QQ(AuthQqRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://graph.qq.com/oauth2.0/authorize"; return "https://graph.qq.com/oauth2.0/authorize";
...@@ -245,7 +249,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -245,7 +249,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* 微信开放平台 * 微信开放平台
*/ */
WECHAT_OPEN { WECHAT_OPEN(AuthWeChatOpenRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://open.weixin.qq.com/connect/qrconnect"; return "https://open.weixin.qq.com/connect/qrconnect";
...@@ -269,7 +273,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -269,7 +273,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* 微信公众平台 * 微信公众平台
*/ */
WECHAT_MP { WECHAT_MP(AuthWeChatMpRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://open.weixin.qq.com/connect/oauth2/authorize"; return "https://open.weixin.qq.com/connect/oauth2/authorize";
...@@ -293,7 +297,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -293,7 +297,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* 淘宝 * 淘宝
*/ */
TAOBAO { TAOBAO(AuthTaobaoRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://oauth.taobao.com/authorize"; return "https://oauth.taobao.com/authorize";
...@@ -312,7 +316,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -312,7 +316,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* Google * Google
*/ */
GOOGLE { GOOGLE(AuthGoogleRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://accounts.google.com/o/oauth2/v2/auth"; return "https://accounts.google.com/o/oauth2/v2/auth";
...@@ -331,7 +335,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -331,7 +335,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* Facebook * Facebook
*/ */
FACEBOOK { FACEBOOK(AuthFacebookRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://www.facebook.com/v10.0/dialog/oauth"; return "https://www.facebook.com/v10.0/dialog/oauth";
...@@ -350,7 +354,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -350,7 +354,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* 抖音 * 抖音
*/ */
DOUYIN { DOUYIN(AuthDouyinRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://open.douyin.com/platform/oauth/connect"; return "https://open.douyin.com/platform/oauth/connect";
...@@ -374,7 +378,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -374,7 +378,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* 领英 * 领英
*/ */
LINKEDIN { LINKEDIN(AuthLinkedinRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://www.linkedin.com/oauth/v2/authorization"; return "https://www.linkedin.com/oauth/v2/authorization";
...@@ -398,7 +402,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -398,7 +402,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* 微软 * 微软
*/ */
MICROSOFT { MICROSOFT(AuthMicrosoftRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"; return "https://login.microsoftonline.com/common/oauth2/v2.0/authorize";
...@@ -422,7 +426,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -422,7 +426,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* 小米 * 小米
*/ */
MI { MI(AuthMiRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://account.xiaomi.com/oauth2/authorize"; return "https://account.xiaomi.com/oauth2/authorize";
...@@ -446,7 +450,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -446,7 +450,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* 今日头条 * 今日头条
*/ */
TOUTIAO { TOUTIAO(AuthToutiaoRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://open.snssdk.com/auth/authorize"; return "https://open.snssdk.com/auth/authorize";
...@@ -465,7 +469,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -465,7 +469,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* Teambition * Teambition
*/ */
TEAMBITION { TEAMBITION(AuthTeambitionRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://account.teambition.com/oauth2/authorize"; return "https://account.teambition.com/oauth2/authorize";
...@@ -490,7 +494,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -490,7 +494,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* 人人网 * 人人网
*/ */
RENREN { RENREN(AuthRenrenRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://graph.renren.com/oauth/authorize"; return "https://graph.renren.com/oauth/authorize";
...@@ -515,7 +519,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -515,7 +519,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* Pinterest * Pinterest
*/ */
PINTEREST { PINTEREST(AuthPinterestRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://api.pinterest.com/oauth"; return "https://api.pinterest.com/oauth";
...@@ -535,7 +539,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -535,7 +539,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* Stack Overflow * Stack Overflow
*/ */
STACK_OVERFLOW { STACK_OVERFLOW(AuthStackOverflowRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://stackoverflow.com/oauth"; return "https://stackoverflow.com/oauth";
...@@ -557,7 +561,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -557,7 +561,7 @@ public enum AuthDefaultSource implements AuthSource {
* *
* @since 1.10.0 * @since 1.10.0
*/ */
HUAWEI { HUAWEI(AuthHuaweiRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://oauth-login.cloud.huawei.com/oauth2/v2/authorize"; return "https://oauth-login.cloud.huawei.com/oauth2/v2/authorize";
...@@ -584,7 +588,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -584,7 +588,7 @@ public enum AuthDefaultSource implements AuthSource {
* *
* @since 1.10.0 * @since 1.10.0
*/ */
WECHAT_ENTERPRISE { WECHAT_ENTERPRISE(AuthWeChatEnterpriseQrcodeRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://open.work.weixin.qq.com/wwopen/sso/qrConnect"; return "https://open.work.weixin.qq.com/wwopen/sso/qrConnect";
...@@ -604,7 +608,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -604,7 +608,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* 企业微信网页登录 * 企业微信网页登录
*/ */
WECHAT_ENTERPRISE_WEB { WECHAT_ENTERPRISE_WEB(AuthWeChatEnterpriseWebRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://open.weixin.qq.com/connect/oauth2/authorize"; return "https://open.weixin.qq.com/connect/oauth2/authorize";
...@@ -626,7 +630,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -626,7 +630,7 @@ public enum AuthDefaultSource implements AuthSource {
* *
* @since 1.11.0 * @since 1.11.0
*/ */
KUJIALE { KUJIALE(AuthKujialeRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://oauth.kujiale.com/oauth2/show"; return "https://oauth.kujiale.com/oauth2/show";
...@@ -653,7 +657,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -653,7 +657,7 @@ public enum AuthDefaultSource implements AuthSource {
* *
* @since 1.11.0 * @since 1.11.0
*/ */
GITLAB { GITLAB(AuthGitlabRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://gitlab.com/oauth/authorize"; return "https://gitlab.com/oauth/authorize";
...@@ -675,7 +679,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -675,7 +679,7 @@ public enum AuthDefaultSource implements AuthSource {
* *
* @since 1.12.0 * @since 1.12.0
*/ */
MEITUAN { MEITUAN(AuthMeituanRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://openapi.waimai.meituan.com/oauth/authorize"; return "https://openapi.waimai.meituan.com/oauth/authorize";
...@@ -704,7 +708,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -704,7 +708,7 @@ public enum AuthDefaultSource implements AuthSource {
* *
* @since 1.12.0 * @since 1.12.0
*/ */
ELEME { ELEME(AuthElemeRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://open-api.shop.ele.me/authorize"; return "https://open-api.shop.ele.me/authorize";
...@@ -731,7 +735,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -731,7 +735,7 @@ public enum AuthDefaultSource implements AuthSource {
* *
* @since 1.13.0 * @since 1.13.0
*/ */
TWITTER { TWITTER(AuthTwitterRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://api.twitter.com/oauth/authenticate"; return "https://api.twitter.com/oauth/authenticate";
...@@ -755,7 +759,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -755,7 +759,7 @@ public enum AuthDefaultSource implements AuthSource {
* *
* @since 1.15.9 * @since 1.15.9
*/ */
FEISHU { FEISHU(AuthFeishuRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://open.feishu.cn/open-apis/authen/v1/index"; return "https://open.feishu.cn/open-apis/authen/v1/index";
...@@ -781,7 +785,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -781,7 +785,7 @@ public enum AuthDefaultSource implements AuthSource {
* *
* @since 1.15.0 * @since 1.15.0
*/ */
JD { JD(AuthJdRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://open-oauth.jd.com/oauth2/to_login"; return "https://open-oauth.jd.com/oauth2/to_login";
...@@ -806,7 +810,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -806,7 +810,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* 阿里云 * 阿里云
*/ */
ALIYUN { ALIYUN(AuthAliyunRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://signin.aliyun.com/oauth2/v1/auth"; return "https://signin.aliyun.com/oauth2/v1/auth";
...@@ -831,7 +835,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -831,7 +835,7 @@ public enum AuthDefaultSource implements AuthSource {
/** /**
* 喜马拉雅 * 喜马拉雅
*/ */
XMLY { XMLY(AuthXmlyRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://api.ximalaya.com/oauth2/js/authorize"; return "https://api.ximalaya.com/oauth2/js/authorize";
...@@ -858,7 +862,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -858,7 +862,7 @@ public enum AuthDefaultSource implements AuthSource {
* *
* @since 1.16.0 * @since 1.16.0
*/ */
AMAZON { AMAZON(AuthAmazonRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://www.amazon.com/ap/oa"; return "https://www.amazon.com/ap/oa";
...@@ -884,7 +888,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -884,7 +888,7 @@ public enum AuthDefaultSource implements AuthSource {
* *
* @since 1.16.0 * @since 1.16.0
*/ */
SLACK { SLACK(AuthSlackRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://slack.com/oauth/v2/authorize"; return "https://slack.com/oauth/v2/authorize";
...@@ -917,7 +921,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -917,7 +921,7 @@ public enum AuthDefaultSource implements AuthSource {
* *
* @since 1.16.0 * @since 1.16.0
*/ */
LINE { LINE(AuthLineRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://access.line.me/oauth2/v2.1/authorize"; return "https://access.line.me/oauth2/v2.1/authorize";
...@@ -950,7 +954,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -950,7 +954,7 @@ public enum AuthDefaultSource implements AuthSource {
* *
* @since 1.16.0 * @since 1.16.0
*/ */
OKTA { OKTA(AuthOktaRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://%s.okta.com/oauth2/%s/v1/authorize"; return "https://%s.okta.com/oauth2/%s/v1/authorize";
...@@ -981,7 +985,7 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -981,7 +985,7 @@ public enum AuthDefaultSource implements AuthSource {
* *
* @since 1.16.2 * @since 1.16.2
*/ */
PROGINN { PROGINN(AuthProginnRequest.class) {
@Override @Override
public String authorize() { public String authorize() {
return "https://www.proginn.com/oauth2/authorize"; return "https://www.proginn.com/oauth2/authorize";
...@@ -996,5 +1000,49 @@ public enum AuthDefaultSource implements AuthSource { ...@@ -996,5 +1000,49 @@ public enum AuthDefaultSource implements AuthSource {
public String userInfo() { public String userInfo() {
return "https://www.proginn.com/openapi/user/basic_info"; return "https://www.proginn.com/openapi/user/basic_info";
} }
}, };
/** 对应的实现类 */
private Class<? extends AuthDefaultRequest> targetClass;
AuthDefaultSource(Class<? extends AuthDefaultRequest> targetClass) {
this.targetClass = targetClass;
}
/**
* 根据source获取对应的AuthSource枚举
* @param source 枚举名
* @return AuthDefaultSource
*/
public static AuthDefaultSource getAuthSource(String source){
return Arrays.stream(AuthDefaultSource.values()).filter(authSource -> authSource.name().equalsIgnoreCase(source)).findAny().orElseThrow(()->new AuthException("未获取到有效的AuthSource配置"));
}
/**
* 根据配置获取 AuthRequest 实例
* @param authConfig 配置
* @return AuthRequest
*/
public AuthRequest getAuthRequestInstance(AuthConfig authConfig) {
return getAuthRequestInstance(authConfig,null);
}
/**
* 利用反射 生成 AuthRequest 实例
* @param authConfig 配置
* @param authStateCache 缓存配置
* @return AuthRequest
*/
public AuthRequest getAuthRequestInstance(AuthConfig authConfig, AuthStateCache authStateCache) {
try {
if(authStateCache==null){
return this.targetClass.getDeclaredConstructor(AuthConfig.class).newInstance(authConfig);
}else{
return this.targetClass.getDeclaredConstructor(AuthConfig.class, AuthStateCache.class).newInstance(authConfig, authStateCache);
}
} catch (Exception e) {
throw new AuthException("未获取到有效的Auth配置");
}
}
} }
...@@ -21,6 +21,8 @@ import me.zhyd.oauth.model.AuthUser; ...@@ -21,6 +21,8 @@ import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.utils.StringUtils; import me.zhyd.oauth.utils.StringUtils;
import me.zhyd.oauth.utils.UrlBuilder; import me.zhyd.oauth.utils.UrlBuilder;
import java.net.InetSocketAddress;
/** /**
* 支付宝登录 * 支付宝登录
* *
...@@ -39,8 +41,15 @@ public class AuthAlipayRequest extends AuthDefaultRequest { ...@@ -39,8 +41,15 @@ public class AuthAlipayRequest extends AuthDefaultRequest {
public AuthAlipayRequest(AuthConfig config, AuthStateCache authStateCache) { public AuthAlipayRequest(AuthConfig config, AuthStateCache authStateCache) {
super(config, AuthDefaultSource.ALIPAY, authStateCache); super(config, AuthDefaultSource.ALIPAY, authStateCache);
this.alipayClient = new DefaultAlipayClient(AuthDefaultSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(), "json", "UTF-8", config if (config.getHttpConfig() != null && config.getHttpConfig().getProxy() != null
.getAlipayPublicKey(), "RSA2"); && config.getHttpConfig().getProxy().address() instanceof InetSocketAddress) {
InetSocketAddress address = (InetSocketAddress) config.getHttpConfig().getProxy().address();
this.alipayClient = new DefaultAlipayClient(AuthDefaultSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(),
"json", "UTF-8", config.getAlipayPublicKey(), "RSA2", address.getHostName(), address.getPort());
} else {
this.alipayClient = new DefaultAlipayClient(AuthDefaultSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(),
"json", "UTF-8", config.getAlipayPublicKey(), "RSA2");
}
} }
public AuthAlipayRequest(AuthConfig config, AuthStateCache authStateCache, String proxyHost, Integer proxyPort) { public AuthAlipayRequest(AuthConfig config, AuthStateCache authStateCache, String proxyHost, Integer proxyPort) {
...@@ -54,7 +63,7 @@ public class AuthAlipayRequest extends AuthDefaultRequest { ...@@ -54,7 +63,7 @@ public class AuthAlipayRequest extends AuthDefaultRequest {
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest(); AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
request.setGrantType("authorization_code"); request.setGrantType("authorization_code");
request.setCode(authCallback.getAuth_code()); request.setCode(authCallback.getAuth_code());
AlipaySystemOauthTokenResponse response = null; AlipaySystemOauthTokenResponse response;
try { try {
response = this.alipayClient.execute(request); response = this.alipayClient.execute(request);
} catch (Exception e) { } catch (Exception e) {
......
package me.zhyd.oauth.config; package me.zhyd.oauth.config;
import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.request.AuthDefaultRequest;
import me.zhyd.oauth.request.AuthExtendRequest;
import me.zhyd.oauth.request.AuthRequest;
/** /**
* 测试自定义实现{@link AuthSource}接口后的枚举类 * 测试自定义实现{@link AuthSource}接口后的枚举类
* *
...@@ -9,7 +15,7 @@ package me.zhyd.oauth.config; ...@@ -9,7 +15,7 @@ package me.zhyd.oauth.config;
*/ */
public enum AuthExtendSource implements AuthSource { public enum AuthExtendSource implements AuthSource {
OTHER { OTHER (AuthExtendRequest.class){
/** /**
* 授权的api * 授权的api
* *
...@@ -59,5 +65,28 @@ public enum AuthExtendSource implements AuthSource { ...@@ -59,5 +65,28 @@ public enum AuthExtendSource implements AuthSource {
public String refresh() { public String refresh() {
return null; return null;
} }
};
private Class<? extends AuthDefaultRequest> targetClass;
AuthExtendSource(Class<? extends AuthDefaultRequest> targetClass) {
this.targetClass = targetClass;
}
public AuthRequest getAuthRequestInstance(AuthConfig authConfig) {
return this.getAuthRequestInstance(authConfig,null);
} }
public AuthRequest getAuthRequestInstance(AuthConfig authConfig, AuthStateCache authStateCache) {
try {
if(authStateCache==null){
return this.targetClass.getDeclaredConstructor(AuthConfig.class).newInstance(authConfig);
}else{
return this.targetClass.getDeclaredConstructor(AuthConfig.class, AuthStateCache.class).newInstance(authConfig, authStateCache);
}
} catch (Exception e) {
throw new AuthException("未获取到有效的Auth配置");
}
}
} }
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource;
import me.zhyd.oauth.utils.AuthStateUtils; import me.zhyd.oauth.utils.AuthStateUtils;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
public class AuthWeChatMpRequestTest { public class AuthWeChatMpRequestTest {
...@@ -16,4 +18,15 @@ public class AuthWeChatMpRequestTest { ...@@ -16,4 +18,15 @@ public class AuthWeChatMpRequestTest {
.build()); .build());
System.out.println(request.authorize(AuthStateUtils.createState())); System.out.println(request.authorize(AuthStateUtils.createState()));
} }
@Test
public void authorize1() {
AuthRequest request = AuthDefaultSource.getAuthSource("wechat_mp").getAuthRequestInstance(AuthConfig.builder()
.clientId("a")
.clientSecret("a")
.redirectUri("https://www.justauth.cn")
.build());
Assert.assertTrue(request instanceof AuthWeChatMpRequest);
System.out.println(request.authorize(AuthStateUtils.createState()));
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册