Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
提交反馈
为 GitLab 提交贡献
登录
切换导航
I
ibzuaa
项目
项目
详情
动态
版本
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
ibiz4jteam
ibzuaa
提交
bbbfdc6b
提交
bbbfdc6b
编写于
9月 07, 2020
作者:
nancy
1
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
钉钉认证。
上级
2564b22d
变更
1
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
57 行增加
和
91 行删除
+57
-91
UserDingtalkRegisterService.java
...e/uaa/extensions/service/UserDingtalkRegisterService.java
+57
-91
未找到文件。
ibzuaa-core/src/main/java/cn/ibizlab/core/uaa/extensions/service/UserDingtalkRegisterService.java
浏览文件 @
bbbfdc6b
...
...
@@ -11,34 +11,22 @@ import cn.ibizlab.util.security.AuthenticationUser;
import
cn.ibizlab.util.service.AuthenticationUserService
;
import
cn.ibizlab.util.service.IBZUSERService
;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.nacos.client.identify.Base64
;
import
com.baomidou.mybatisplus.core.toolkit.Wrappers
;
import
com.dingtalk.api.DefaultDingTalkClient
;
import
com.dingtalk.api.DingTalkClient
;
import
com.dingtalk.api.request.*
;
import
com.dingtalk.api.response.*
;
import
com.dingtalk.api.request.OapiGettokenRequest
;
import
com.dingtalk.api.request.OapiSnsGetuserinfoBycodeRequest
;
import
com.dingtalk.api.request.OapiUserGetuserinfoRequest
;
import
com.dingtalk.api.response.OapiGettokenResponse
;
import
com.dingtalk.api.response.OapiSnsGetuserinfoBycodeResponse
;
import
com.dingtalk.api.response.OapiUserGetuserinfoResponse
;
import
com.taobao.api.ApiException
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.entity.StringEntity
;
import
org.apache.http.impl.client.CloseableHttpClient
;
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.StringUtils
;
import
javax.crypto.Mac
;
import
javax.crypto.spec.SecretKeySpec
;
import
java.io.IOException
;
import
java.net.URLEncoder
;
import
java.security.InvalidKeyException
;
import
java.security.NoSuchAlgorithmException
;
import
java.sql.Timestamp
;
import
java.sql.Wrapper
;
import
java.util.Date
;
/**
* 实体[IBZUSER] 钉钉用户注册接口实现
...
...
@@ -49,83 +37,63 @@ public class UserDingtalkRegisterService {
@Autowired
private
IBZUSERService
ibzuserService
;
@Autowired
private
AuthenticationUserService
authenticationUserService
;
@Autowired
private
ISysUserAuthService
sysUserAuthService
;
@Autowired
private
ISysOpenAccessService
sysOpenAccessService
;
public
synchronized
String
getAccessToken
(
SysOpenAccess
sysOpenAccess
)
{
private
long
lastRefreshTime
=
System
.
currentTimeMillis
()-
7200001
;
private
String
accessToken
=
""
;
public
boolean
isExpire
()
{
if
(
System
.
currentTimeMillis
()<(
lastRefreshTime
+
7200000
))
{
System
.
currentTimeMillis
();
return
false
;
if
(
sysOpenAccess
==
null
)
{
log
.
error
(
"无法获取对应的第三方认证信息,数据信息[{}],请检查数据库信息。"
,
sysOpenAccess
);
throw
new
BadRequestAlertException
(
"无法获取对应的第三方认证信息,数据信息[{}],请检查数据库信息。"
,
null
,
null
);
}
return
true
;
}
public
synchronized
String
getAccessToken
(
String
appKey
,
String
appSecret
)
{
if
(
isExpire
())
{
if
(
sysOpenAccess
.
getExpiresTime
()
==
null
||
System
.
currentTimeMillis
()
<
(
sysOpenAccess
.
getExpiresTime
().
getTime
()
+
7200000
))
{
DefaultDingTalkClient
client
=
new
DefaultDingTalkClient
(
"https://oapi.dingtalk.com/gettoken"
);
OapiGettokenRequest
request
=
new
OapiGettokenRequest
();
request
.
setAppkey
(
"appKey"
);
request
.
setAppsecret
(
"appSecret"
);
request
.
setAppkey
(
sysOpenAccess
.
getAccessKey
()
);
request
.
setAppsecret
(
sysOpenAccess
.
getSecretKey
()
);
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
());
sysOpenAccess
.
setExpiresTime
(
new
Timestamp
(
System
.
currentTimeMillis
())
);
sysOpenAccess
.
setAccessToken
(
response
.
getAccessToken
()
);
}
catch
(
ApiException
e
)
{
e
.
printStackTrace
();
throw
new
InternalServerErrorException
(
"获取access_token失败"
);
}
}
return
accessToken
;
sysOpenAccessService
.
update
(
sysOpenAccess
);
return
sysOpenAccess
.
getAccessToken
();
}
@Autowired
private
ISysOpenAccessService
sysOpenAccessService
;
public
SysOpenAccess
getOpenAccess
(
String
id
)
{
return
getOpenAccess
(
id
,
true
);
}
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"
).
and
(
wrapper
->
wrapper
.
eq
(
SysOpenAccess:
:
getAccessKey
,
accessid
).
or
().
eq
(
SysOpenAccess:
:
getId
,
accessid
)),
false
);
if
((
sysOpenAccess
==
null
||
(
sysOpenAccess
.
getDisabled
()!=
null
&&
sysOpenAccess
.
getDisabled
()==
1
))&&
throwEx
)
throw
new
BadRequestAlertException
(
"获取接入配置失败"
,
"UserDingtalkRegisterService"
,
""
);
String
accessToken
=
getAccessToken
(
sysOpenAccess
.
getAccessKey
(),
sysOpenAccess
.
getSecretKey
());
if
(!
accessToken
.
equals
(
sysOpenAccess
.
getAccessToken
()))
{
sysOpenAccess
.
setAccessToken
(
accessToken
);
sysOpenAccess
.
setExpiresTime
(
new
Timestamp
(
lastRefreshTime
));
sysOpenAccessService
.
update
(
sysOpenAccess
);
public
SysOpenAccess
getOpenAccess
(
String
id
)
{
return
getOpenAccess
(
id
,
true
);
}
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"
).
and
(
wrapper
->
wrapper
.
eq
(
SysOpenAccess:
:
getAccessKey
,
accessid
).
or
().
eq
(
SysOpenAccess:
:
getId
,
accessid
)),
false
);
if
((
sysOpenAccess
==
null
||
(
sysOpenAccess
.
getDisabled
()
!=
null
&&
sysOpenAccess
.
getDisabled
()
==
1
))
&&
throwEx
)
throw
new
BadRequestAlertException
(
"获取接入配置失败"
,
"UserDingtalkRegisterService"
,
""
);
String
accessToken
=
getAccessToken
(
sysOpenAccess
);
return
sysOpenAccess
;
}
public
AuthenticationUser
getUserByToken
(
String
id
,
String
requestAuthCode
)
{
public
AuthenticationUser
getUserByToken
(
String
id
,
String
requestAuthCode
)
{
SysOpenAccess
openAccess
=
getOpenAccess
(
id
);
if
(
openAccess
==
null
||
(
openAccess
.
getDisabled
()!=
null
&&
openAccess
.
getDisabled
()==
1
))
if
(
openAccess
==
null
||
(
openAccess
.
getDisabled
()
!=
null
&&
openAccess
.
getDisabled
()
==
1
))
throw
new
BadRequestAlertException
(
"未找到配置"
,
"UserDingtalkRegisterService"
,
""
);
DingTalkClient
client
=
new
DefaultDingTalkClient
(
"https://oapi.dingtalk.com/user/getuserinfo"
);
...
...
@@ -135,8 +103,8 @@ public class UserDingtalkRegisterService {
OapiUserGetuserinfoResponse
response
=
null
;
try
{
response
=
client
.
execute
(
request
,
openAccess
.
getAccessToken
());
if
(
response
.
getErrcode
()!=
0
||
StringUtils
.
isEmpty
(
response
.
getUserid
()))
throw
new
BadRequestAlertException
(
"获取user失败"
,
"UserDingtalkRegisterService"
,
response
.
getErrmsg
());
if
(
response
.
getErrcode
()
!=
0
||
StringUtils
.
isEmpty
(
response
.
getUserid
()))
throw
new
BadRequestAlertException
(
"获取user失败"
,
"UserDingtalkRegisterService"
,
response
.
getErrmsg
());
}
catch
(
ApiException
e
)
{
e
.
printStackTrace
();
throw
new
InternalServerErrorException
(
"获取user失败"
);
...
...
@@ -144,19 +112,18 @@ public class UserDingtalkRegisterService {
String
userId
=
response
.
getUserid
();
//先按userid或者username查
IBZUSER
user
=
ibzuserService
.
getOne
(
Wrappers
.<
IBZUSER
>
lambdaQuery
().
eq
(
IBZUSER:
:
getUserid
,
userId
).
or
().
eq
(
IBZUSER:
:
getUsername
,
userId
),
false
);
IBZUSER
user
=
ibzuserService
.
getOne
(
Wrappers
.<
IBZUSER
>
lambdaQuery
().
eq
(
IBZUSER:
:
getUserid
,
userId
).
or
().
eq
(
IBZUSER:
:
getUsername
,
userId
),
false
);
if
(
user
==
null
)
{
if
(
user
==
null
)
{
//查不到情况下到auth表查真实userId
SysUserAuth
userAuth
=
sysUserAuthService
.
getOne
(
Wrappers
.<
SysUserAuth
>
lambdaQuery
().
eq
(
SysUserAuth:
:
getIdentityType
,
"dingtalk"
).
eq
(
SysUserAuth:
:
getIdentifier
,
userId
),
false
);
SysUserAuth
userAuth
=
sysUserAuthService
.
getOne
(
Wrappers
.<
SysUserAuth
>
lambdaQuery
().
eq
(
SysUserAuth:
:
getIdentityType
,
"dingtalk"
).
eq
(
SysUserAuth:
:
getIdentifier
,
userId
),
false
);
// 该钉钉用户注册过账号,登录系统
if
(
userAuth
!=
null
)
{
if
(
userAuth
!=
null
)
{
user
=
ibzuserService
.
getById
(
userAuth
.
getUserid
());
if
(
user
==
null
)
throw
new
BadRequestAlertException
(
"未找到"
+
userId
+
"对应系统用户"
,
"UserDingtalkRegisterService"
,
""
);
if
(
user
==
null
)
throw
new
BadRequestAlertException
(
"未找到"
+
userId
+
"对应系统用户"
,
"UserDingtalkRegisterService"
,
""
);
AuthenticationUser
curUser
=
authenticationUserService
.
loadUserByUsername
(
user
.
getLoginname
()
+(
StringUtils
.
isEmpty
(
user
.
getDomains
())?
""
:(
"|"
+
user
.
getDomains
())));
AuthenticationUser
curUser
=
authenticationUserService
.
loadUserByUsername
(
user
.
getLoginname
()
+
(
StringUtils
.
isEmpty
(
user
.
getDomains
())
?
""
:
(
"|"
+
user
.
getDomains
())));
return
curUser
;
}
}
...
...
@@ -170,41 +137,41 @@ public class UserDingtalkRegisterService {
*
* @return
*/
public
JSONObject
getUserBySnsToken
(
String
id
,
String
requestAuthCode
)
{
public
JSONObject
getUserBySnsToken
(
String
id
,
String
requestAuthCode
)
{
JSONObject
returnObj
=
null
;
SysOpenAccess
openAccess
=
getOpenAccess
(
id
);
if
(
openAccess
==
null
||
(
openAccess
.
getDisabled
()!=
null
&&
openAccess
.
getDisabled
()==
1
))
if
(
openAccess
==
null
||
(
openAccess
.
getDisabled
()
!=
null
&&
openAccess
.
getDisabled
()
==
1
))
throw
new
BadRequestAlertException
(
"未找到配置"
,
"UserDingtalkRegisterService"
,
""
);
DefaultDingTalkClient
client
=
new
DefaultDingTalkClient
(
"https://oapi.dingtalk.com/sns/getuserinfo_bycode"
);
OapiSnsGetuserinfoBycodeRequest
req
=
new
OapiSnsGetuserinfoBycodeRequest
();
req
.
setTmpAuthCode
(
requestAuthCode
);
try
{
OapiSnsGetuserinfoBycodeResponse
response
=
client
.
execute
(
req
,
openAccess
.
getAccessKey
(),
openAccess
.
getSecretKey
());
OapiSnsGetuserinfoBycodeResponse
response
=
client
.
execute
(
req
,
openAccess
.
getAccessKey
(),
openAccess
.
getSecretKey
());
if
(
response
.
getErrcode
()!=
0
)
{
if
(
response
.
getErrcode
()
!=
0
)
{
throw
new
BadRequestAlertException
(
"获取user失败"
,
"UserDingtalkRegisterService"
,
response
.
getErrmsg
());
}
returnObj
.
put
(
"openid"
,
response
.
getUserInfo
().
getOpenid
());
returnObj
.
put
(
"nickname"
,
response
.
getUserInfo
().
getNick
());
returnObj
.
put
(
"unionid"
,
response
.
getUserInfo
().
getUnionid
());
SysUserAuth
userAuth
=
sysUserAuthService
.
getOne
(
Wrappers
.<
SysUserAuth
>
lambdaQuery
().
eq
(
SysUserAuth:
:
getIdentityType
,
"dingtalk"
)
SysUserAuth
userAuth
=
sysUserAuthService
.
getOne
(
Wrappers
.<
SysUserAuth
>
lambdaQuery
().
eq
(
SysUserAuth:
:
getIdentityType
,
"dingtalk"
)
.
and
(
wrapper
->
wrapper
.
eq
(
SysUserAuth:
:
getIdentifier
,
response
.
getUserInfo
().
getOpenid
()).
or
().
eq
(
SysUserAuth:
:
getIdentifier
,
response
.
getUserInfo
().
getUnionid
())
),
false
);
),
false
);
IBZUSER
user
=
null
;
// 该钉钉用户注册过账号,登录系统
if
(
userAuth
!=
null
)
{
if
(
userAuth
!=
null
)
{
user
=
ibzuserService
.
getById
(
userAuth
.
getUserid
());
if
(
user
==
null
)
user
=
ibzuserService
.
getOne
(
Wrappers
.<
IBZUSER
>
lambdaQuery
().
eq
(
IBZUSER:
:
getUserid
,
response
.
getUserInfo
().
getOpenid
()).
or
().
eq
(
IBZUSER:
:
getUsername
,
response
.
getUserInfo
().
getOpenid
()),
false
);
user
=
ibzuserService
.
getOne
(
Wrappers
.<
IBZUSER
>
lambdaQuery
().
eq
(
IBZUSER:
:
getUserid
,
response
.
getUserInfo
().
getOpenid
()).
or
().
eq
(
IBZUSER:
:
getUsername
,
response
.
getUserInfo
().
getOpenid
()),
false
);
if
(
user
!=
null
)
{
returnObj
.
put
(
"username"
,
user
.
getLoginname
()+(
StringUtils
.
isEmpty
(
user
.
getDomains
())?
""
:(
"|"
+
user
.
getDomains
())));
if
(
user
!=
null
)
{
returnObj
.
put
(
"username"
,
user
.
getLoginname
()
+
(
StringUtils
.
isEmpty
(
user
.
getDomains
())
?
""
:
(
"|"
+
user
.
getDomains
())));
}
}
...
...
@@ -217,5 +184,4 @@ public class UserDingtalkRegisterService {
return
returnObj
;
}
}
\ No newline at end of file
nancy_iBiz
@nancy_iBiz
被提及 commit
c966ea0d
·
9月 08, 2020
被提及 commit
c966ea0d
被提及 commit c966ea0d058d0acfc539836b75c1fca20c55a1fe
切换提交列表
编辑
预览
Markdown
格式
0%
请重试
or
添加新附件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录