Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
justauth
JustAuth
提交
51087658
J
JustAuth
项目概览
justauth
/
JustAuth
1 年多 前同步成功
通知
394
Star
15212
Fork
2708
代码
文件
提交
分支
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看板
提交
51087658
编写于
9月 11, 2019
作者:
智布道
👁
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
🍻
完善饿了么授权登录
上级
6567f175
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
31 addition
and
41 deletion
+31
-41
.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md
.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md
+5
-5
src/main/java/me/zhyd/oauth/config/AuthSource.java
src/main/java/me/zhyd/oauth/config/AuthSource.java
+2
-3
src/main/java/me/zhyd/oauth/request/AuthElemeRequest.java
src/main/java/me/zhyd/oauth/request/AuthElemeRequest.java
+24
-33
未找到文件。
.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md
浏览文件 @
51087658
[ ] 是否为解决Issue?
-
[ ] 是否为解决Issue?
### 您做了哪些更新?
### 您做了哪些更新?
####
新增
-
新增
####
修改
-
修改
####
修复
-
修复
####
其他
-
其他
### 是否做了充分测试?
### 是否做了充分测试?
...
...
src/main/java/me/zhyd/oauth/config/AuthSource.java
浏览文件 @
51087658
...
@@ -14,13 +14,12 @@ import me.zhyd.oauth.model.AuthCallback;
...
@@ -14,13 +14,12 @@ import me.zhyd.oauth.model.AuthCallback;
* <p>
* <p>
* 注:
* 注:
* ①、如需通过JustAuth扩展实现第三方授权,请参考{@link AuthDefaultSource}自行创建对应的枚举类并实现{@link AuthSource}接口
* ①、如需通过JustAuth扩展实现第三方授权,请参考{@link AuthDefaultSource}自行创建对应的枚举类并实现{@link AuthSource}接口
* ②、如果不是使用的枚举类,那么在授权成功后获取用户信息时,需要单独处理
{@link me.zhyd.oauth.model.AuthUser#setSource}
字段的赋值
* ②、如果不是使用的枚举类,那么在授权成功后获取用户信息时,需要单独处理
sourcec
字段的赋值
* ③、如果扩展了对应枚举类时,在{@link me.zhyd.oauth.request.AuthRequest#login(AuthCallback)}中可以通过{@code xx.toString()}获取对应的source
* ③、如果扩展了对应枚举类时,在{@link me.zhyd.oauth.request.AuthRequest#login(AuthCallback)}中可以通过{@code xx.toString()}获取对应的source
*
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @version 1.0
* @date 2019/9/6 21:17
* @since 1.12.0
* @since 1.8
*/
*/
public
interface
AuthSource
{
public
interface
AuthSource
{
...
...
src/main/java/me/zhyd/oauth/request/AuthElemeRequest.java
浏览文件 @
51087658
...
@@ -57,7 +57,6 @@ public class AuthElemeRequest extends AuthDefaultRequest {
...
@@ -57,7 +57,6 @@ public class AuthElemeRequest extends AuthDefaultRequest {
this
.
checkResponse
(
object
);
this
.
checkResponse
(
object
);
return
AuthToken
.
builder
()
return
AuthToken
.
builder
()
.
openId
(
this
.
getOpenId
(
authCallback
.
getCode
()))
.
accessToken
(
object
.
getString
(
"access_token"
))
.
accessToken
(
object
.
getString
(
"access_token"
))
.
refreshToken
(
object
.
getString
(
"refresh_token"
))
.
refreshToken
(
object
.
getString
(
"refresh_token"
))
.
tokenType
(
object
.
getString
(
"token_type"
))
.
tokenType
(
object
.
getString
(
"token_type"
))
...
@@ -73,29 +72,38 @@ public class AuthElemeRequest extends AuthDefaultRequest {
...
@@ -73,29 +72,38 @@ public class AuthElemeRequest extends AuthDefaultRequest {
// 时间戳,单位秒。API服务端允许客户端请求最大时间误差为正负5分钟。
// 时间戳,单位秒。API服务端允许客户端请求最大时间误差为正负5分钟。
final
long
timestamp
=
System
.
currentTimeMillis
();
final
long
timestamp
=
System
.
currentTimeMillis
();
// 公共参数
// 公共参数
Map
<
String
,
Object
>
metasHashMap
=
new
HashMap
<
String
,
Object
>();
Map
<
String
,
Object
>
metasHashMap
=
new
HashMap
<>();
metasHashMap
.
put
(
"app_key"
,
config
.
getClientId
());
metasHashMap
.
put
(
"app_key"
,
config
.
getClientId
());
metasHashMap
.
put
(
"timestamp"
,
timestamp
);
metasHashMap
.
put
(
"timestamp"
,
timestamp
);
String
signature
=
GlobalAuthUtil
.
generateElemeSignature
(
config
.
getClientId
(),
config
.
getClientSecret
(),
timestamp
,
action
,
authToken
.
getAccessToken
(),
parameters
);
String
signature
=
GlobalAuthUtil
.
generateElemeSignature
(
config
.
getClientId
(),
config
.
getClientSecret
(),
timestamp
,
action
,
authToken
.
getAccessToken
(),
parameters
);
String
requestId
=
this
.
getRequestId
();
Map
<
String
,
Object
>
paramsMap
=
new
HashMap
<>();
paramsMap
.
put
(
"nop"
,
"1.0.0"
);
paramsMap
.
put
(
"id"
,
requestId
);
paramsMap
.
put
(
"action"
,
action
);
paramsMap
.
put
(
"token"
,
authToken
.
getAccessToken
());
paramsMap
.
put
(
"metas"
,
metasHashMap
);
paramsMap
.
put
(
"params"
,
parameters
);
paramsMap
.
put
(
"signature"
,
signature
);
HttpRequest
request
=
HttpRequest
.
post
(
source
.
userInfo
())
HttpRequest
request
=
HttpRequest
.
post
(
source
.
userInfo
())
.
form
(
"nop"
,
"1.0.0"
)
.
body
(
JSONObject
.
toJSONBytes
(
paramsMap
));
.
form
(
"id"
,
this
.
getRequestId
())
.
form
(
"metas"
,
metasHashMap
)
.
form
(
"action"
,
action
)
.
form
(
"token"
,
authToken
.
getAccessToken
())
.
form
(
"params"
,
parameters
)
.
form
(
"signature"
,
signature
);
// 设置header
// 设置header
this
.
setHeader
(
request
,
"application/json; charset=utf-8"
);
this
.
setHeader
(
request
,
"application/json; charset=utf-8"
,
requestId
);
HttpResponse
response
=
request
.
execute
();
HttpResponse
response
=
request
.
execute
();
JSONObject
object
=
JSONObject
.
parseObject
(
response
.
body
());
JSONObject
object
=
JSONObject
.
parseObject
(
response
.
body
());
// 校验请求
// 校验请求
if
(
object
.
containsKey
(
"error"
))
{
if
(
object
.
containsKey
(
"name"
))
{
throw
new
AuthException
(
object
.
getString
(
"message"
));
}
if
(
object
.
containsKey
(
"error"
)
&&
null
!=
object
.
get
(
"error"
))
{
throw
new
AuthException
(
object
.
getJSONObject
(
"error"
).
getString
(
"message"
));
throw
new
AuthException
(
object
.
getJSONObject
(
"error"
).
getString
(
"message"
));
}
}
...
@@ -143,23 +151,6 @@ public class AuthElemeRequest extends AuthDefaultRequest {
...
@@ -143,23 +151,6 @@ public class AuthElemeRequest extends AuthDefaultRequest {
.
build
();
.
build
();
}
}
private
String
getOpenId
(
String
code
)
{
HttpRequest
request
=
HttpRequest
.
post
(
"https://open-api.shop.ele.me/identity"
)
.
form
(
"grant_type"
,
"authorization_code"
)
.
form
(
"code"
,
code
)
.
form
(
"redirect_uri"
,
config
.
getRedirectUri
())
.
form
(
"client_id"
,
config
.
getClientId
());
// 设置header
this
.
setHeader
(
request
);
HttpResponse
response
=
request
.
execute
();
JSONObject
object
=
JSONObject
.
parseObject
(
response
.
body
());
this
.
checkResponse
(
object
);
return
object
.
getString
(
"openId"
);
}
private
String
getBasic
(
String
appKey
,
String
appSecret
)
{
private
String
getBasic
(
String
appKey
,
String
appSecret
)
{
StringBuilder
sb
=
new
StringBuilder
();
StringBuilder
sb
=
new
StringBuilder
();
String
encodeToString
=
Base64
.
encode
((
appKey
+
":"
+
appSecret
).
getBytes
());
String
encodeToString
=
Base64
.
encode
((
appKey
+
":"
+
appSecret
).
getBytes
());
...
@@ -168,20 +159,20 @@ public class AuthElemeRequest extends AuthDefaultRequest {
...
@@ -168,20 +159,20 @@ public class AuthElemeRequest extends AuthDefaultRequest {
}
}
private
void
setHeader
(
HttpRequest
request
)
{
private
void
setHeader
(
HttpRequest
request
)
{
setHeader
(
request
,
"application/x-www-form-urlencoded;charset=UTF-8"
);
setHeader
(
request
,
"application/x-www-form-urlencoded;charset=UTF-8"
,
getRequestId
());
request
.
header
(
"Authorization"
,
this
.
getBasic
(
config
.
getClientId
(),
config
.
getClientSecret
()));
}
}
private
void
setHeader
(
HttpRequest
request
,
String
contentType
)
{
private
void
setHeader
(
HttpRequest
request
,
String
contentType
,
String
requestId
)
{
request
.
header
(
"Accept"
,
"text/xml,text/javascript,text/html"
)
request
.
header
(
"Accept"
,
"text/xml,text/javascript,text/html"
)
.
header
(
"Content-Type"
,
contentType
)
.
header
(
"Content-Type"
,
contentType
)
.
header
(
"Accept-Encoding"
,
"gzip"
)
.
header
(
"Accept-Encoding"
,
"gzip"
)
.
header
(
"User-Agent"
,
"eleme-openapi-java-sdk"
)
.
header
(
"User-Agent"
,
"eleme-openapi-java-sdk"
)
.
header
(
"x-eleme-requestid"
,
getRequestId
())
.
header
(
"x-eleme-requestid"
,
requestId
);
.
header
(
"Authorization"
,
this
.
getBasic
(
config
.
getClientId
(),
config
.
getClientSecret
()));
}
}
private
String
getRequestId
()
{
private
String
getRequestId
()
{
return
UuidUtils
.
getUUID
()
+
"|"
+
System
.
currentTimeMillis
();
return
(
UuidUtils
.
getUUID
()
+
"|"
+
System
.
currentTimeMillis
()).
toUpperCase
();
}
}
private
void
checkResponse
(
JSONObject
object
)
{
private
void
checkResponse
(
JSONObject
object
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录