Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
提交反馈
为 GitLab 提交贡献
登录
切换导航
I
ibzuaa
项目
项目
详情
动态
版本
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
ibiz4jteam
ibzuaa
提交
2decea9b
提交
2decea9b
编写于
6月 22, 2020
作者:
laizhilong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
添加一些判断
上级
a8a4746c
变更
3
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
59 行增加
和
33 行删除
+59
-33
UserDingtalkRegisterService.java
...e/uaa/extensions/service/UserDingtalkRegisterService.java
+13
-8
UserWechatRegisterService.java
...ore/uaa/extensions/service/UserWechatRegisterService.java
+30
-15
UserWechatRegisterResource.java
...izlab/api/rest/extensions/UserWechatRegisterResource.java
+16
-10
未找到文件。
ibzuaa-core/src/main/java/cn/ibizlab/core/uaa/extensions/service/UserDingtalkRegisterService.java
浏览文件 @
2decea9b
...
@@ -15,6 +15,7 @@ import org.apache.http.impl.client.HttpClients;
...
@@ -15,6 +15,7 @@ import org.apache.http.impl.client.HttpClients;
import
org.apache.http.util.EntityUtils
;
import
org.apache.http.util.EntityUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
org.springframework.util.StringUtils
;
import
javax.crypto.Mac
;
import
javax.crypto.Mac
;
import
javax.crypto.spec.SecretKeySpec
;
import
javax.crypto.spec.SecretKeySpec
;
...
@@ -98,19 +99,23 @@ public class UserDingtalkRegisterService {
...
@@ -98,19 +99,23 @@ public class UserDingtalkRegisterService {
// 获取结果实体
// 获取结果实体
HttpEntity
entity
=
response
.
getEntity
();
HttpEntity
entity
=
response
.
getEntity
();
JSONObject
entityJson
=
null
;
JSONObject
entityJson
=
null
;
if
(
entity
!=
null
)
{
if
(
StringUtils
.
isEmpty
(
entity
))
{
throw
new
BadRequestAlertException
(
"钉钉服务端返回结果为空"
,
"UserDingtalkRegisterService"
,
""
);
}
else
{
//按指定编码转换结果实体为String类型
//按指定编码转换结果实体为String类型
entityJson
=
JSONObject
.
parseObject
(
EntityUtils
.
toString
(
entity
,
"UTF-8"
));
entityJson
=
JSONObject
.
parseObject
(
EntityUtils
.
toString
(
entity
,
"UTF-8"
));
// 是否获取钉钉用户信息成功
if
(
entityJson
.
containsKey
(
"user_info"
))
{
returnObj
=
entityJson
.
getJSONObject
(
"user_info"
);
}
else
{
throw
new
BadRequestAlertException
(
"获取钉钉授权用户个人信息失败"
,
"UserDingtalkRegisterService"
,
""
);
}
}
}
// 是否获取钉钉用户信息成功
}
catch
(
NoSuchAlgorithmException
|
InvalidKeyException
e
)
{
if
(
entityJson
.
containsKey
(
"user_info"
))
{
returnObj
=
entityJson
.
getJSONObject
(
"user_info"
);
}
else
{
throw
new
BadRequestAlertException
(
"code获取钉钉用户信息失败"
,
"UserDingtalkRegisterService"
,
""
);
}
}
catch
(
NoSuchAlgorithmException
|
InvalidKeyException
|
IOException
e
)
{
throw
new
BadRequestAlertException
(
"钉钉生成安全签名失败"
,
"UserDingtalkRegisterService"
,
""
);
throw
new
BadRequestAlertException
(
"钉钉生成安全签名失败"
,
"UserDingtalkRegisterService"
,
""
);
}
catch
(
IOException
e
)
{
throw
new
BadRequestAlertException
(
"连接钉钉服务端失败"
,
"UserDingtalkRegisterService"
,
""
);
}
}
return
returnObj
;
return
returnObj
;
...
...
ibzuaa-core/src/main/java/cn/ibizlab/core/uaa/extensions/service/UserWechatRegisterService.java
浏览文件 @
2decea9b
...
@@ -49,43 +49,58 @@ public class UserWechatRegisterService {
...
@@ -49,43 +49,58 @@ public class UserWechatRegisterService {
public
JSONObject
requestWechatUserByCode
(
String
code
,
String
state
,
String
wechatAppId
,
String
wechatappsecret
)
{
public
JSONObject
requestWechatUserByCode
(
String
code
,
String
state
,
String
wechatAppId
,
String
wechatappsecret
)
{
JSONObject
returnObj
=
null
;
JSONObject
returnObj
=
null
;
try
{
try
{
// 1.根据code获取access_token
// 1.根据code获取access_token、openid、refresh_token
String
getAccessTokenUrl
=
"https://api.weixin.qq.com/sns/oauth2/access_token?appid="
+
wechatAppId
+
"&secret="
+
wechatappsecret
+
"&code="
+
code
+
"&grant_type=authorization_code"
;
String
getAccessTokenUrl
=
"https://api.weixin.qq.com/sns/oauth2/access_token?"
+
"appid="
+
wechatAppId
+
"&secret="
+
wechatappsecret
+
"&code="
+
code
+
"&grant_type=authorization_code"
;
JSONObject
responseObj
=
JSONObject
.
parseObject
(
HttpUtils
.
get
(
getAccessTokenUrl
,
null
,
null
));
JSONObject
responseObj
=
JSONObject
.
parseObject
(
HttpUtils
.
get
(
getAccessTokenUrl
,
null
,
null
));
if
(!
responseObj
.
containsKey
(
"access_token"
)
&&
!
responseObj
.
containsKey
(
"openid"
))
{
if
(!
responseObj
.
containsKey
(
"access_token"
)
&&
!
responseObj
.
containsKey
(
"openid"
))
{
throw
new
BadRequestAlertException
(
"获取access_token失败!"
,
"UserWechatRegisterService"
,
""
);
throw
new
BadRequestAlertException
(
"
微信服务端
获取access_token失败!"
,
"UserWechatRegisterService"
,
""
);
}
}
String
access_token
=
responseObj
.
getString
(
"access_token"
);
String
access_token
=
responseObj
.
getString
(
"access_token"
);
String
openid
=
responseObj
.
getString
(
"openid"
);
String
openid
=
responseObj
.
getString
(
"openid"
);
String
refresh_token
=
responseObj
.
getString
(
"refresh_token"
);
String
refresh_token
=
responseObj
.
getString
(
"refresh_token"
);
// 2.检验授权凭证(access_token)是否有效
// 2.检验授权凭证(access_token)是否有效,无效则需要刷新access_token
String
checkAccessTokenUrl
=
"https://api.weixin.qq.com/sns/auth?access_token="
+
access_token
+
"&openid="
+
responseObj
.
get
(
"openid"
);
String
checkAccessTokenUrl
=
"https://api.weixin.qq.com/sns/auth?"
+
"access_token="
+
access_token
+
"&openid="
+
openid
;
JSONObject
responseObj2
=
JSONObject
.
parseObject
(
HttpUtils
.
get
(
checkAccessTokenUrl
,
null
,
null
));
JSONObject
responseObj2
=
JSONObject
.
parseObject
(
HttpUtils
.
get
(
checkAccessTokenUrl
,
null
,
null
));
if
(
responseObj2
.
getInteger
(
"errcode"
)
!=
0
)
{
if
(
responseObj2
.
getInteger
(
"errcode"
)
!=
0
)
{
// access_token已失效,使用refresh_token进行刷新
// access_token已失效,使用refresh_token刷新access_token
String
refreshAccess_token
=
"https://api.weixin.qq.com/sns/oauth2/refresh_token?appid="
+
wechatAppId
+
"&grant_type=refresh_token&refresh_token="
+
refresh_token
;
String
refreshAccess_token
=
"https://api.weixin.qq.com/sns/oauth2/refresh_token?"
+
"appid="
+
wechatAppId
+
"&grant_type=refresh_token"
+
"&refresh_token="
+
refresh_token
;
JSONObject
responseObj3
=
JSONObject
.
parseObject
(
HttpUtils
.
get
(
refreshAccess_token
,
null
,
null
));
JSONObject
responseObj3
=
JSONObject
.
parseObject
(
HttpUtils
.
get
(
refreshAccess_token
,
null
,
null
));
if
(!
responseObj3
.
containsKey
(
"access_token"
)
||
!
responseObj3
.
containsKey
(
"openid"
))
{
if
(!
responseObj3
.
containsKey
(
"access_token"
)
||
!
responseObj3
.
containsKey
(
"openid"
))
{
throw
new
BadRequestAlertException
(
"
重新获取
access_token失败!"
,
"UserWechatRegisterService"
,
""
);
throw
new
BadRequestAlertException
(
"
微信服务端刷新
access_token失败!"
,
"UserWechatRegisterService"
,
""
);
}
}
openid
=
responseObj3
.
getString
(
"openid"
);
// 重新拿到access_token、openid、refresh_token
access_token
=
responseObj3
.
getString
(
"access_token"
);
access_token
=
responseObj3
.
getString
(
"access_token"
);
openid
=
responseObj3
.
getString
(
"openid"
);
refresh_token
=
responseObj3
.
getString
(
"refresh_token"
);
}
}
// 3.access_token有效,拉取用户信息(需scope为 snsapi_userinfo)
// 3.access_token有效,拉取用户信息
String
getweChatUserInfoUrl
=
"https://api.weixin.qq.com/sns/userinfo?access_token="
+
access_token
+
"&openid="
+
openid
+
"&lang=zh_CN"
;
String
getweChatUserInfoUrl
=
"https://api.weixin.qq.com/sns/userinfo?"
+
"access_token="
+
access_token
+
"&openid="
+
openid
+
"&lang=zh_CN"
;
returnObj
=
JSONObject
.
parseObject
(
HttpUtils
.
get
(
getweChatUserInfoUrl
,
null
,
null
));
returnObj
=
JSONObject
.
parseObject
(
HttpUtils
.
get
(
getweChatUserInfoUrl
,
null
,
null
));
if
(
StringUtils
.
isEmpty
(
returnObj
)
||
returnObj
.
containsKey
(
"errcode"
))
{
if
(
StringUtils
.
isEmpty
(
returnObj
))
{
throw
new
BadRequestAlertException
(
"拉取微信用户信息失败!"
,
"UserWechatRegisterService"
,
""
);
throw
new
BadRequestAlertException
(
"微信服务端返回结果为空!"
,
"UserWechatRegisterService"
,
""
);
}
else
if
(!
returnObj
.
containsKey
(
"openid"
))
{
throw
new
BadRequestAlertException
(
"获取微信用户信息失败!"
,
"UserWechatRegisterService"
,
""
);
}
}
}
catch
(
UnsupportedEncodingException
e
)
{
}
catch
(
UnsupportedEncodingException
e
)
{
throw
new
BadRequestAlertException
(
"
获取微信授权用户相关信息
失败!"
,
"UserWechatRegisterService"
,
""
);
throw
new
BadRequestAlertException
(
"
连接微信服务端
失败!"
,
"UserWechatRegisterService"
,
""
);
}
}
return
returnObj
;
return
returnObj
;
}
}
}
}
\ No newline at end of file
ibzuaa-provider/ibzuaa-provider-api/src/main/java/cn/ibizlab/api/rest/extensions/UserWechatRegisterResource.java
浏览文件 @
2decea9b
...
@@ -218,16 +218,22 @@ public class UserWechatRegisterResource {
...
@@ -218,16 +218,22 @@ public class UserWechatRegisterResource {
if
(
StringUtils
.
isEmpty
(
openid
))
{
if
(
StringUtils
.
isEmpty
(
openid
))
{
// 通过code获取微信用户信息
// 通过code获取微信用户信息
JSONObject
returnObj
=
userWechatRegisterService
.
requestWechatUserByCode
(
code
,
state
,
WechatAppid
,
WechatAppsecret
);
JSONObject
returnObj
=
userWechatRegisterService
.
requestWechatUserByCode
(
code
,
state
,
WechatAppid
,
WechatAppsecret
);
if
(!
StringUtils
.
isEmpty
(
returnObj
)
&&
!
returnObj
.
containsKey
(
"errcode"
))
{
if
(!
StringUtils
.
isEmpty
(
returnObj
))
{
// 微信注册
if
(
returnObj
.
containsKey
(
"openid"
))
{
String
uuid
=
UUID
.
randomUUID
().
toString
();
// 微信注册
ibzuser
.
setPassword
(
password
);
String
uuid
=
UUID
.
randomUUID
().
toString
();
ibzuser
.
setLoginname
(
loginname
);
ibzuser
.
setPassword
(
password
);
ibzuser
.
setUserid
(
"wechat-"
+
uuid
);
ibzuser
.
setLoginname
(
loginname
);
ibzuser
.
setPersonname
(
returnObj
.
getString
(
"nickname"
));
ibzuser
.
setUserid
(
"wechat-"
+
uuid
);
ibzuser
.
setNickname
(
returnObj
.
getString
(
"nickname"
));
ibzuser
.
setPersonname
(
returnObj
.
getString
(
"nickname"
));
ibzuser
.
setAvatar
(
returnObj
.
getString
(
"openid"
));
ibzuser
.
setNickname
(
returnObj
.
getString
(
"nickname"
));
userWechatRegisterService
.
toRegister
(
ibzuser
);
ibzuser
.
setAvatar
(
returnObj
.
getString
(
"openid"
));
userWechatRegisterService
.
toRegister
(
ibzuser
);
}
else
{
throw
new
BadRequestAlertException
(
"获取微信授权用户相关信息失败!"
,
"UserWechatRegisterResource"
,
""
);
}
}
else
{
throw
new
BadRequestAlertException
(
"获取微信授权用户相关信息失败!"
,
"UserWechatRegisterResource"
,
""
);
}
}
}
else
{
}
else
{
// 已经有微信用户信息直接注册
// 已经有微信用户信息直接注册
...
...
编辑
预览
Markdown
格式
0%
请重试
or
添加新附件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录