Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
提交反馈
为 GitLab 提交贡献
登录
切换导航
I
ibzuaa
项目
项目
详情
动态
版本
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
ibiz4jteam
ibzuaa
提交
4779be16
提交
4779be16
编写于
9月 10, 2020
作者:
nancy
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修改access_token存储逻辑,按照appkey分别存储多个应用的access_token,以适配多个钉钉应用接入情况
上级
34658fa5
变更
1
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
94 行增加
和
22 行删除
+94
-22
UserDingtalkRegisterService.java
...e/uaa/extensions/service/UserDingtalkRegisterService.java
+94
-22
未找到文件。
ibzuaa-core/src/main/java/cn/ibizlab/core/uaa/extensions/service/UserDingtalkRegisterService.java
浏览文件 @
4779be16
...
...
@@ -28,6 +28,7 @@ import org.apache.http.impl.client.HttpClients;
import
org.apache.http.util.EntityUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.util.CollectionUtils
;
import
org.springframework.util.StringUtils
;
import
javax.crypto.Mac
;
...
...
@@ -38,7 +39,7 @@ import java.security.InvalidKeyException;
import
java.security.NoSuchAlgorithmException
;
import
java.sql.Timestamp
;
import
java.sql.Wrapper
;
import
java.util.
Date
;
import
java.util.
*
;
/**
* 实体[IBZUSER] 钉钉用户注册接口实现
...
...
@@ -57,24 +58,22 @@ public class UserDingtalkRegisterService {
private
ISysUserAuthService
sysUserAuthService
;
private
long
lastRefreshTime
=
System
.
currentTimeMillis
()-
7200001
;
private
String
accessToken
=
""
;
public
boolean
isExpire
()
{
if
(
System
.
currentTimeMillis
()<(
lastRefreshTime
+
7200000
))
{
System
.
currentTimeMillis
();
return
false
;
}
return
true
;
}
// private long lastRefreshTime=System.currentTimeMillis()-7200001;
// private String accessToken="";
// public boolean isExpire()
// {
// if(System.currentTimeMillis()<(lastRefreshTime+7200000))
// {
// System.currentTimeMillis();
// return false;
// }
// return true;
// }
public
synchronized
String
getAccessToken
(
String
appKey
,
String
appSecret
)
{
if
(
isExpire
(
))
{
if
(
isExpire
(
appKey
))
{
DefaultDingTalkClient
client
=
new
DefaultDingTalkClient
(
"https://oapi.dingtalk.com/gettoken"
);
OapiGettokenRequest
request
=
new
OapiGettokenRequest
();
request
.
setAppkey
(
appKey
);
...
...
@@ -82,16 +81,17 @@ public class UserDingtalkRegisterService {
request
.
setHttpMethod
(
"GET"
);
try
{
OapiGettokenResponse
response
=
client
.
execute
(
request
);
if
(
response
.
getErrcode
()!=
0
||
StringUtils
.
isEmpty
(
response
.
getAccessToken
()))
throw
new
BadRequestAlertException
(
"获取access_token失败"
,
"UserDingtalkRegisterService"
,
response
.
getErrmsg
());
lastRefreshTime
=
System
.
currentTimeMillis
();
accessToken
=
response
.
getAccessToken
();
if
(
response
.
getErrcode
()
!=
0
||
StringUtils
.
isEmpty
(
response
.
getAccessToken
()))
throw
new
BadRequestAlertException
(
"获取access_token失败"
,
"UserDingtalkRegisterService"
,
response
.
getErrmsg
());
// lastRefreshTime = System.currentTimeMillis();
// accessToken = response.getAccessToken();
setTokenInfo
(
appKey
,
response
.
getAccessToken
(),
System
.
currentTimeMillis
());
}
catch
(
ApiException
e
)
{
e
.
printStackTrace
();
throw
new
InternalServerErrorException
(
"获取access_token失败"
);
}
}
return
accessToken
;
return
getToken
(
appKey
)
;
}
@Autowired
...
...
@@ -101,7 +101,8 @@ public class UserDingtalkRegisterService {
{
return
getOpenAccess
(
id
,
true
);
}
public
SysOpenAccess
getOpenAccess
(
String
id
,
boolean
throwEx
)
public
SysOpenAccess
getOpenAccess
(
String
id
,
boolean
throwEx
)
{
final
String
accessid
=
StringUtils
.
isEmpty
(
id
)?
"dingtalk"
:
id
;
SysOpenAccess
sysOpenAccess
=
sysOpenAccessService
.
getOne
(
Wrappers
.<
SysOpenAccess
>
lambdaQuery
().
eq
(
SysOpenAccess:
:
getOpenType
,
"dingtalk"
).
...
...
@@ -115,7 +116,7 @@ public class UserDingtalkRegisterService {
if
(!
accessToken
.
equals
(
sysOpenAccess
.
getAccessToken
()))
{
sysOpenAccess
.
setAccessToken
(
accessToken
);
sysOpenAccess
.
setExpiresTime
(
new
Timestamp
(
lastRefreshTime
));
sysOpenAccess
.
setExpiresTime
(
new
Timestamp
(
getLastRefreshTime
(
sysOpenAccess
.
getAccessKey
())
));
sysOpenAccessService
.
update
(
sysOpenAccess
);
}
}
catch
(
Exception
e
)
{
...
...
@@ -222,5 +223,76 @@ public class UserDingtalkRegisterService {
return
returnObj
;
}
private
static
Map
<
String
,
List
<
String
>>
tokenMapping
=
new
HashMap
<>();
/**
* 适配当UAA接入多个钉钉应用的情况。
*
* @param appkey 钉钉appkey
* @return
*/
public
boolean
isExpire
(
String
appkey
)
{
if
(!
tokenMapping
.
containsKey
(
appkey
))
return
true
;
List
<
String
>
accessTokenInfo
=
tokenMapping
.
get
(
appkey
);
if
(
CollectionUtils
.
isEmpty
(
accessTokenInfo
)
||
accessTokenInfo
.
size
()
!=
2
)
return
true
;
String
accessToken
=
accessTokenInfo
.
get
(
0
);
String
refreshtime
=
accessTokenInfo
.
get
(
1
);
if
(
StringUtils
.
isEmpty
(
accessToken
)
||
StringUtils
.
isEmpty
(
refreshtime
))
return
true
;
if
(
System
.
currentTimeMillis
()
<
(
Long
.
valueOf
(
refreshtime
)
+
7100000
))
{
System
.
currentTimeMillis
();
return
false
;
}
return
true
;
}
private
void
setTokenInfo
(
String
appkey
,
String
token
,
Long
refreshTime
)
{
if
(
StringUtils
.
isEmpty
(
token
)
||
StringUtils
.
isEmpty
(
refreshTime
))
return
;
if
(!
tokenMapping
.
containsKey
(
appkey
))
{
List
<
String
>
tokenInfo
=
new
ArrayList
<>(
2
);
tokenMapping
.
put
(
appkey
,
tokenInfo
);
}
List
<
String
>
tokenInfo
=
tokenMapping
.
get
(
appkey
);
tokenInfo
.
set
(
0
,
token
);
tokenInfo
.
set
(
1
,
String
.
valueOf
(
refreshTime
));
}
private
String
getToken
(
String
appkey
){
if
(!
StringUtils
.
isEmpty
(
appkey
)){
List
<
String
>
tokenInfo
=
tokenMapping
.
get
(
appkey
);
if
(!
CollectionUtils
.
isEmpty
(
tokenInfo
)
&&
tokenInfo
.
size
()==
2
)
return
tokenMapping
.
get
(
appkey
).
get
(
0
);
}
return
null
;
}
/**
* 钉钉默认超时时间为2小时(7200秒),防止程序卡点,换成7100秒(100秒预留)
*/
private
Long
getLastRefreshTime
(
String
appkey
){
if
(!
StringUtils
.
isEmpty
(
appkey
)){
List
<
String
>
tokenInfo
=
tokenMapping
.
get
(
appkey
);
if
(!
CollectionUtils
.
isEmpty
(
tokenInfo
)
&&
tokenInfo
.
size
()==
2
)
{
String
refreshtime
=
tokenMapping
.
get
(
appkey
).
get
(
1
);
try
{
return
Long
.
valueOf
(
refreshtime
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
return
System
.
currentTimeMillis
()-
7100001
;
}
}
\ No newline at end of file
编辑
预览
Markdown
格式
0%
请重试
or
添加新附件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录