Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Sajor_
JustAuth
提交
af723a8b
J
JustAuth
项目概览
Sajor_
/
JustAuth
与 Fork 源项目一致
Fork自
justauth / JustAuth
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
JustAuth
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
af723a8b
编写于
7月 16, 2019
作者:
智布道
👁
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
♻
从UrlBuilder中拆分微信相关的URL
上级
1360db60
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
73 addition
and
57 deletion
+73
-57
src/main/java/me/zhyd/oauth/request/AuthWeChatRequest.java
src/main/java/me/zhyd/oauth/request/AuthWeChatRequest.java
+21
-6
src/main/java/me/zhyd/oauth/url/WechatUrlBuilder.java
src/main/java/me/zhyd/oauth/url/WechatUrlBuilder.java
+49
-0
src/main/java/me/zhyd/oauth/url/entity/AuthRefreshTokenEntity.java
...java/me/zhyd/oauth/url/entity/AuthRefreshTokenEntity.java
+3
-0
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
+0
-51
未找到文件。
src/main/java/me/zhyd/oauth/request/AuthWeChatRequest.java
浏览文件 @
af723a8b
...
...
@@ -7,7 +7,11 @@ import me.zhyd.oauth.config.AuthConfig;
import
me.zhyd.oauth.config.AuthSource
;
import
me.zhyd.oauth.exception.AuthException
;
import
me.zhyd.oauth.model.*
;
import
me.zhyd.oauth.utils.UrlBuilder
;
import
me.zhyd.oauth.url.WechatUrlBuilder
;
import
me.zhyd.oauth.url.entity.AuthAccessTokenEntity
;
import
me.zhyd.oauth.url.entity.AuthAuthorizeEntity
;
import
me.zhyd.oauth.url.entity.AuthRefreshTokenEntity
;
import
me.zhyd.oauth.url.entity.AuthUserInfoEntity
;
/**
* 微信登录
...
...
@@ -18,7 +22,7 @@ import me.zhyd.oauth.utils.UrlBuilder;
*/
public
class
AuthWeChatRequest
extends
BaseAuthRequest
{
public
AuthWeChatRequest
(
AuthConfig
config
)
{
super
(
config
,
AuthSource
.
WECHAT
);
super
(
config
,
AuthSource
.
WECHAT
,
new
WechatUrlBuilder
()
);
}
/**
...
...
@@ -29,7 +33,10 @@ public class AuthWeChatRequest extends BaseAuthRequest {
*/
@Override
protected
AuthToken
getAccessToken
(
AuthCallback
authCallback
)
{
String
accessTokenUrl
=
UrlBuilder
.
getWeChatAccessTokenUrl
(
config
.
getClientId
(),
config
.
getClientSecret
(),
authCallback
.
getCode
());
String
accessTokenUrl
=
this
.
urlBuilder
.
getAccessTokenUrl
(
AuthAccessTokenEntity
.
builder
()
.
config
(
config
)
.
code
(
authCallback
.
getCode
())
.
build
());
return
this
.
getToken
(
accessTokenUrl
);
}
...
...
@@ -38,7 +45,10 @@ public class AuthWeChatRequest extends BaseAuthRequest {
String
accessToken
=
authToken
.
getAccessToken
();
String
openId
=
authToken
.
getOpenId
();
HttpResponse
response
=
HttpRequest
.
get
(
UrlBuilder
.
getWeChatUserInfoUrl
(
accessToken
,
openId
)).
execute
();
HttpResponse
response
=
HttpRequest
.
get
(
this
.
urlBuilder
.
getUserInfoUrl
(
AuthUserInfoEntity
.
builder
()
.
accessToken
(
accessToken
)
.
openId
(
openId
)
.
build
())).
execute
();
JSONObject
object
=
JSONObject
.
parseObject
(
response
.
body
());
this
.
checkResponse
(
object
);
...
...
@@ -63,12 +73,17 @@ public class AuthWeChatRequest extends BaseAuthRequest {
*/
@Override
public
String
authorize
()
{
return
UrlBuilder
.
getWeChatAuthorizeUrl
(
config
.
getClientId
(),
config
.
getRedirectUri
(),
config
.
getState
());
return
this
.
urlBuilder
.
getAuthorizeUrl
(
AuthAuthorizeEntity
.
builder
()
.
config
(
config
)
.
build
());
}
@Override
public
AuthResponse
refresh
(
AuthToken
oldToken
)
{
String
refreshTokenUrl
=
UrlBuilder
.
getWeChatRefreshUrl
(
config
.
getClientId
(),
oldToken
.
getRefreshToken
());
String
refreshTokenUrl
=
this
.
urlBuilder
.
getRefreshUrl
(
AuthRefreshTokenEntity
.
builder
()
.
clientId
(
config
.
getClientId
())
.
refreshToken
(
oldToken
.
getRefreshToken
())
.
build
());
return
AuthResponse
.
builder
()
.
code
(
ResponseStatus
.
SUCCESS
.
getCode
())
.
data
(
this
.
getToken
(
refreshTokenUrl
))
...
...
src/main/java/me/zhyd/oauth/url/WechatUrlBuilder.java
0 → 100644
浏览文件 @
af723a8b
package
me.zhyd.oauth.url
;
import
me.zhyd.oauth.config.AuthConfig
;
import
me.zhyd.oauth.config.AuthSource
;
import
me.zhyd.oauth.url.entity.*
;
import
java.text.MessageFormat
;
/**
* 微信相关的URL构建类
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public
class
WechatUrlBuilder
extends
AbstractUrlBuilder
{
private
static
final
String
WECHAT_AUTHORIZE_PATTERN
=
"{0}?appid={1}&redirect_uri={2}&response_type=code&scope=snsapi_login&state={3}#wechat_redirect"
;
private
static
final
String
WECHAT_ACCESS_TOKEN_PATTERN
=
"{0}?appid={1}&secret={2}&code={3}&grant_type=authorization_code"
;
private
static
final
String
WECHAT_REFRESH_TOKEN_PATTERN
=
"{0}?appid={1}&grant_type=refresh_token&refresh_token={2}"
;
private
static
final
String
WECHAT_USER_INFO_PATTERN
=
"{0}?access_token={1}&openid={2}&lang=zh_CN"
;
@Override
public
String
getAccessTokenUrl
(
AuthAccessTokenEntity
accessTokenEntity
)
{
AuthConfig
config
=
accessTokenEntity
.
getConfig
();
return
MessageFormat
.
format
(
WECHAT_ACCESS_TOKEN_PATTERN
,
AuthSource
.
WECHAT
.
accessToken
(),
config
.
getClientId
(),
config
.
getClientSecret
(),
accessTokenEntity
.
getCode
());
}
@Override
public
String
getUserInfoUrl
(
AuthUserInfoEntity
userInfoEntity
)
{
return
MessageFormat
.
format
(
WECHAT_USER_INFO_PATTERN
,
AuthSource
.
WECHAT
.
userInfo
(),
userInfoEntity
.
getAccessToken
(),
userInfoEntity
.
getOpenId
());
}
@Override
public
String
getAuthorizeUrl
(
AuthAuthorizeEntity
authorizeEntity
)
{
AuthConfig
config
=
authorizeEntity
.
getConfig
();
return
MessageFormat
.
format
(
WECHAT_AUTHORIZE_PATTERN
,
AuthSource
.
WECHAT
.
authorize
(),
config
.
getClientId
(),
config
.
getRedirectUri
(),
this
.
getRealState
(
config
.
getState
()));
}
@Override
public
String
getRefreshUrl
(
AuthRefreshTokenEntity
refreshTokenEntity
)
{
return
MessageFormat
.
format
(
WECHAT_REFRESH_TOKEN_PATTERN
,
AuthSource
.
WECHAT
.
refresh
(),
refreshTokenEntity
.
getClientId
(),
refreshTokenEntity
.
getRefreshToken
());
}
@Override
public
String
getRevokeUrl
(
AuthRevokeEntity
revokeEntity
)
{
return
null
;
}
}
src/main/java/me/zhyd/oauth/url/entity/AuthRefreshTokenEntity.java
浏览文件 @
af723a8b
...
...
@@ -11,4 +11,7 @@ import lombok.Getter;
@Getter
@Builder
public
class
AuthRefreshTokenEntity
{
private
String
clientId
;
private
String
refreshToken
;
}
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
浏览文件 @
af723a8b
...
...
@@ -57,11 +57,6 @@ public class UrlBuilder {
private
static
final
String
QQ_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&response_type=code&redirect_uri={2}&state={3}"
;
private
static
final
String
QQ_OPENID_PATTERN
=
"{0}?access_token={1}&unionid={2}"
;
private
static
final
String
WECHAT_AUTHORIZE_PATTERN
=
"{0}?appid={1}&redirect_uri={2}&response_type=code&scope=snsapi_login&state={3}#wechat_redirect"
;
private
static
final
String
WECHAT_ACCESS_TOKEN_PATTERN
=
"{0}?appid={1}&secret={2}&code={3}&grant_type=authorization_code"
;
private
static
final
String
WECHAT_REFRESH_TOKEN_PATTERN
=
"{0}?appid={1}&grant_type=refresh_token&refresh_token={2}"
;
private
static
final
String
WECHAT_USER_INFO_PATTERN
=
"{0}?access_token={1}&openid={2}&lang=zh_CN"
;
private
static
final
String
TAOBAO_AUTHORIZE_PATTERN
=
"{0}?response_type=code&client_id={1}&redirect_uri={2}&state={3}&view=web"
;
private
static
final
String
TAOBAO_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&code={3}&redirect_uri={4}&grant_type=authorization_code"
;
...
...
@@ -440,52 +435,6 @@ public class UrlBuilder {
return
MessageFormat
.
format
(
ALIPAY_AUTHORIZE_PATTERN
,
AuthSource
.
ALIPAY
.
authorize
(),
clientId
,
redirectUrl
,
getState
(
state
));
}
/**
* 获取微信 授权地址
*
* @param clientId 微信 应用的appid
* @param redirectUrl 微信 应用授权成功后的回调地址
* @param state 随机字符串,用于保持会话状态,防止CSRF攻击
* @return full url
*/
public
static
String
getWeChatAuthorizeUrl
(
String
clientId
,
String
redirectUrl
,
String
state
)
{
return
MessageFormat
.
format
(
WECHAT_AUTHORIZE_PATTERN
,
AuthSource
.
WECHAT
.
authorize
(),
clientId
,
redirectUrl
,
getState
(
state
));
}
/**
* 获取微信 token的接口地址
*
* @param clientId 微信 应用的appid
* @param clientSecret 微信 应用的secret
* @param code 微信 授权前的code,用来换token
* @return full url
*/
public
static
String
getWeChatAccessTokenUrl
(
String
clientId
,
String
clientSecret
,
String
code
)
{
return
MessageFormat
.
format
(
WECHAT_ACCESS_TOKEN_PATTERN
,
AuthSource
.
WECHAT
.
accessToken
(),
clientId
,
clientSecret
,
code
);
}
/**
* 获取微信 用户详情的接口地址
*
* @param token 微信 应用返回的 access token
* @param openId 微信 应用返回的openId
* @return full url
*/
public
static
String
getWeChatUserInfoUrl
(
String
token
,
String
openId
)
{
return
MessageFormat
.
format
(
WECHAT_USER_INFO_PATTERN
,
AuthSource
.
WECHAT
.
userInfo
(),
token
,
openId
);
}
/**
* 获取微信 刷新令牌 地址
*
* @param clientId 微信 应用的appid
* @param refreshToken 微信 应用返回的刷新token
* @return full url
*/
public
static
String
getWeChatRefreshUrl
(
String
clientId
,
String
refreshToken
)
{
return
MessageFormat
.
format
(
WECHAT_REFRESH_TOKEN_PATTERN
,
AuthSource
.
WECHAT
.
refresh
(),
clientId
,
refreshToken
);
}
/**
* 获取Taobao token的接口地址: 淘宝的授权登录,在这一步就会返回用户信息
*
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录