Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
提交反馈
为 GitLab 提交贡献
登录
切换导航
I
ibzuaa
项目
项目
详情
动态
版本
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
ibiz4jteam
ibzuaa
提交
74bbe35e
提交
74bbe35e
编写于
8月 24, 2020
作者:
sq3536
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
第三方接入
上级
c3dd4508
变更
6
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
244 行增加
和
276 行删除
+244
-276
UserDingtalkRegisterService.java
...e/uaa/extensions/service/UserDingtalkRegisterService.java
+11
-8
UserQQRegisterService.java
...ab/core/uaa/extensions/service/UserQQRegisterService.java
+61
-46
UserWechatRegisterService.java
...ore/uaa/extensions/service/UserWechatRegisterService.java
+62
-67
UserDingtalkRegisterResource.java
...lab/api/rest/extensions/UserDingtalkRegisterResource.java
+1
-1
UserQQRegisterResource.java
...n/ibizlab/api/rest/extensions/UserQQRegisterResource.java
+55
-79
UserWechatRegisterResource.java
...izlab/api/rest/extensions/UserWechatRegisterResource.java
+54
-75
未找到文件。
ibzuaa-core/src/main/java/cn/ibizlab/core/uaa/extensions/service/UserDingtalkRegisterService.java
浏览文件 @
74bbe35e
...
@@ -15,12 +15,8 @@ import com.alibaba.nacos.client.identify.Base64;
...
@@ -15,12 +15,8 @@ import com.alibaba.nacos.client.identify.Base64;
import
com.baomidou.mybatisplus.core.toolkit.Wrappers
;
import
com.baomidou.mybatisplus.core.toolkit.Wrappers
;
import
com.dingtalk.api.DefaultDingTalkClient
;
import
com.dingtalk.api.DefaultDingTalkClient
;
import
com.dingtalk.api.DingTalkClient
;
import
com.dingtalk.api.DingTalkClient
;
import
com.dingtalk.api.request.OapiGettokenRequest
;
import
com.dingtalk.api.request.*
;
import
com.dingtalk.api.request.OapiSnsGetuserinfoBycodeRequest
;
import
com.dingtalk.api.response.*
;
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
com.taobao.api.ApiException
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.HttpEntity
;
...
@@ -102,11 +98,15 @@ public class UserDingtalkRegisterService {
...
@@ -102,11 +98,15 @@ public class UserDingtalkRegisterService {
private
ISysOpenAccessService
sysOpenAccessService
;
private
ISysOpenAccessService
sysOpenAccessService
;
public
SysOpenAccess
getOpenAccess
(
String
id
)
public
SysOpenAccess
getOpenAccess
(
String
id
)
{
return
getOpenAccess
(
id
,
true
);
}
public
SysOpenAccess
getOpenAccess
(
String
id
,
boolean
throwEx
)
{
{
final
String
accessid
=
StringUtils
.
isEmpty
(
id
)?
"dingtalk"
:
id
;
final
String
accessid
=
StringUtils
.
isEmpty
(
id
)?
"dingtalk"
:
id
;
SysOpenAccess
sysOpenAccess
=
sysOpenAccessService
.
getOne
(
Wrappers
.<
SysOpenAccess
>
lambdaQuery
().
eq
(
SysOpenAccess:
:
getOpenType
,
"dingtalk"
).
SysOpenAccess
sysOpenAccess
=
sysOpenAccessService
.
getOne
(
Wrappers
.<
SysOpenAccess
>
lambdaQuery
().
eq
(
SysOpenAccess:
:
getOpenType
,
"dingtalk"
).
and
(
wrapper
->
wrapper
.
eq
(
SysOpenAccess:
:
getAccessKey
,
accessid
).
or
().
eq
(
SysOpenAccess:
:
getId
,
accessid
)),
false
);
and
(
wrapper
->
wrapper
.
eq
(
SysOpenAccess:
:
getAccessKey
,
accessid
).
or
().
eq
(
SysOpenAccess:
:
getId
,
accessid
)),
false
);
if
(
sysOpenAccess
==
null
||
(
sysOpenAccess
.
getDisabled
()!=
null
&&
sysOpenAccess
.
getDisabled
()==
1
)
)
if
(
(
sysOpenAccess
==
null
||
(
sysOpenAccess
.
getDisabled
()!=
null
&&
sysOpenAccess
.
getDisabled
()==
1
))&&
throwEx
)
throw
new
BadRequestAlertException
(
"获取接入配置失败"
,
"UserDingtalkRegisterService"
,
""
);
throw
new
BadRequestAlertException
(
"获取接入配置失败"
,
"UserDingtalkRegisterService"
,
""
);
String
accessToken
=
getAccessToken
(
sysOpenAccess
.
getAccessKey
(),
sysOpenAccess
.
getSecretKey
());
String
accessToken
=
getAccessToken
(
sysOpenAccess
.
getAccessKey
(),
sysOpenAccess
.
getSecretKey
());
...
@@ -190,7 +190,10 @@ public class UserDingtalkRegisterService {
...
@@ -190,7 +190,10 @@ public class UserDingtalkRegisterService {
returnObj
.
put
(
"openid"
,
response
.
getUserInfo
().
getOpenid
());
returnObj
.
put
(
"openid"
,
response
.
getUserInfo
().
getOpenid
());
returnObj
.
put
(
"nickname"
,
response
.
getUserInfo
().
getNick
());
returnObj
.
put
(
"nickname"
,
response
.
getUserInfo
().
getNick
());
SysUserAuth
userAuth
=
sysUserAuthService
.
getOne
(
Wrappers
.<
SysUserAuth
>
lambdaQuery
().
eq
(
SysUserAuth:
:
getIdentityType
,
"dingtalk"
).
eq
(
SysUserAuth:
:
getIdentifier
,
response
.
getUserInfo
().
getOpenid
()),
false
);
returnObj
.
put
(
"unionid"
,
response
.
getUserInfo
().
getUnionid
());
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
);
IBZUSER
user
=
null
;
IBZUSER
user
=
null
;
// 该钉钉用户注册过账号,登录系统
// 该钉钉用户注册过账号,登录系统
...
...
ibzuaa-core/src/main/java/cn/ibizlab/core/uaa/extensions/service/UserQQRegisterService.java
浏览文件 @
74bbe35e
package
cn
.
ibizlab
.
core
.
uaa
.
extensions
.
service
;
package
cn
.
ibizlab
.
core
.
uaa
.
extensions
.
service
;
import
cn.ibizlab.core.uaa.domain.SysOpenAccess
;
import
cn.ibizlab.core.uaa.domain.SysUserAuth
;
import
cn.ibizlab.core.uaa.domain.SysUserAuth
;
import
cn.ibizlab.core.uaa.service.ISysOpenAccessService
;
import
cn.ibizlab.core.uaa.service.ISysUserAuthService
;
import
cn.ibizlab.core.uaa.service.ISysUserAuthService
;
import
cn.ibizlab.util.domain.IBZUSER
;
import
cn.ibizlab.util.domain.IBZUSER
;
import
cn.ibizlab.util.errors.BadRequestAlertException
;
import
cn.ibizlab.util.errors.BadRequestAlertException
;
import
cn.ibizlab.util.errors.InternalServerErrorException
;
import
cn.ibizlab.util.helper.HttpUtils
;
import
cn.ibizlab.util.helper.HttpUtils
;
import
cn.ibizlab.util.service.IBZUSERService
;
import
cn.ibizlab.util.service.IBZUSERService
;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONObject
;
import
com.baomidou.mybatisplus.core.toolkit.Wrappers
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
...
@@ -26,50 +30,51 @@ public class UserQQRegisterService {
...
@@ -26,50 +30,51 @@ public class UserQQRegisterService {
@Autowired
@Autowired
private
ISysUserAuthService
sysUserAuthService
;
private
ISysUserAuthService
sysUserAuthService
;
/**
* 注册
@Autowired
*
private
ISysOpenAccessService
sysOpenAccessService
;
* @param ibzuser
*/
public
SysOpenAccess
getOpenAccess
(
String
id
)
public
void
toRegister
(
IBZUSER
ibzuser
)
{
{
// 创建ibzuser
return
getOpenAccess
(
id
,
true
);
boolean
flag
=
ibzuserService
.
save
(
ibzuser
);
if
(!
flag
)
{
throw
new
BadRequestAlertException
(
"注册失败"
,
"UserQQRegisterService"
,
""
);
}
}
}
public
SysOpenAccess
getOpenAccess
(
String
id
,
boolean
throwEx
)
{
final
String
accessid
=
StringUtils
.
isEmpty
(
id
)?
"qq"
:
id
;
SysOpenAccess
sysOpenAccess
=
sysOpenAccessService
.
getOne
(
Wrappers
.<
SysOpenAccess
>
lambdaQuery
().
eq
(
SysOpenAccess:
:
getOpenType
,
"qq"
).
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
(
"获取接入配置失败"
,
"UserQQRegisterService"
,
""
);
/**
* 创建QQ用户授权信息
return
sysOpenAccess
;
* @param ibzuser
*/
public
void
toCreateUserAuth
(
SysUserAuth
ibzuser
)
{
// 创建用户授权信息
boolean
flag
=
sysUserAuthService
.
create
(
ibzuser
);
if
(!
flag
)
{
throw
new
BadRequestAlertException
(
"保存用户授权信息失败"
,
"UserQQRegisterService"
,
""
);
}
}
}
/**
/**
*
通过code获取QQ用户
信息
*
qq服务端通过临时授权码code获取授权用户的个人
信息
*
*
* @param code
* @param redirectUri
* @param appId
* @param appSecret
* @return
* @return
*/
*/
public
JSONObject
requestQQUserByCode
(
String
code
,
String
redirectUri
,
String
appId
,
String
appSecret
)
{
public
JSONObject
getUserBySnsToken
(
String
id
,
String
requestAuthCode
)
{
JSONObject
returnObj
=
null
;
JSONObject
returnObj
=
null
;
SysOpenAccess
openAccess
=
getOpenAccess
(
id
);
if
(
openAccess
==
null
||
(
openAccess
.
getDisabled
()!=
null
&&
openAccess
.
getDisabled
()==
1
))
throw
new
BadRequestAlertException
(
"未找到配置"
,
"UserWechatRegisterService"
,
""
);
try
{
try
{
// 1.根据code获取access_token
// 1.根据code获取access_token
String
getAccessTokenUrl
=
"https://graph.qq.com/oauth2.0/token?"
+
String
getAccessTokenUrl
=
"https://graph.qq.com/oauth2.0/token?"
+
"grant_type=authorization_code"
+
"grant_type=authorization_code"
+
"&client_id="
+
appId
+
"&client_id="
+
openAccess
.
getAccessKey
()
+
"&client_secret="
+
appSecret
+
"&client_secret="
+
openAccess
.
getSecretKey
()
+
"&code="
+
c
ode
+
"&code="
+
requestAuthC
ode
+
"&redirect_uri="
+
redirectUri
;
"&redirect_uri="
+
openAccess
.
getRedirectUri
()
;
String
responserStr
=
HttpUtils
.
get
(
getAccessTokenUrl
,
null
,
null
);
String
responserStr
=
HttpUtils
.
get
(
getAccessTokenUrl
,
null
,
null
);
JSONObject
responseObj
=
new
JSONObject
();
JSONObject
responseObj
=
new
JSONObject
();
if
(
StringUtils
.
isEmpty
(
responserStr
))
{
if
(
StringUtils
.
isEmpty
(
responserStr
))
{
...
@@ -89,6 +94,9 @@ public class UserQQRegisterService {
...
@@ -89,6 +94,9 @@ public class UserQQRegisterService {
String
access_token
=
responseObj
.
getString
(
"access_token"
);
String
access_token
=
responseObj
.
getString
(
"access_token"
);
String
refresh_token
=
responseObj
.
getString
(
"refresh_token"
);
String
refresh_token
=
responseObj
.
getString
(
"refresh_token"
);
returnObj
.
put
(
"access_token"
,
access_token
);
returnObj
.
put
(
"refresh_token"
,
refresh_token
);
// 2.使用access_token来获取用户的OpenID
// 2.使用access_token来获取用户的OpenID
String
openid
=
null
;
String
openid
=
null
;
String
getOpenIdUrl
=
"https://graph.qq.com/oauth2.0/me?access_token="
+
access_token
;
String
getOpenIdUrl
=
"https://graph.qq.com/oauth2.0/me?access_token="
+
access_token
;
...
@@ -100,26 +108,33 @@ public class UserQQRegisterService {
...
@@ -100,26 +108,33 @@ public class UserQQRegisterService {
JSONObject
responseObj2
=
JSONObject
.
parseObject
(
responseStr2
);
JSONObject
responseObj2
=
JSONObject
.
parseObject
(
responseStr2
);
if
(
responseObj2
.
containsKey
(
"openid"
))
{
if
(
responseObj2
.
containsKey
(
"openid"
))
{
openid
=
responseObj2
.
getString
(
"openid"
);
openid
=
responseObj2
.
getString
(
"openid"
);
returnObj
.
put
(
"openid"
,
openid
);
SysUserAuth
userAuth
=
sysUserAuthService
.
getOne
(
Wrappers
.<
SysUserAuth
>
lambdaQuery
().
eq
(
SysUserAuth:
:
getIdentityType
,
"qq"
).
eq
(
SysUserAuth:
:
getIdentifier
,
openid
),
false
);
IBZUSER
user
=
null
;
// 该qq用户注册过账号,登录系统
if
(
userAuth
!=
null
)
{
user
=
ibzuserService
.
getById
(
userAuth
.
getUserid
());
if
(
user
==
null
)
user
=
ibzuserService
.
getOne
(
Wrappers
.<
IBZUSER
>
lambdaQuery
().
eq
(
IBZUSER:
:
getUserid
,
openid
).
or
().
eq
(
IBZUSER:
:
getUsername
,
openid
),
false
);
if
(
user
!=
null
)
{
returnObj
.
put
(
"username"
,
user
.
getLoginname
()+(
StringUtils
.
isEmpty
(
user
.
getDomains
())?
""
:(
"|"
+
user
.
getDomains
())));
}
}
}
else
{
}
else
{
throw
new
BadRequestAlertException
(
"QQ服务端获取openid失败!"
,
"UserQQRegisterService"
,
""
);
throw
new
BadRequestAlertException
(
"QQ服务端获取openid失败!"
,
"UserQQRegisterService"
,
""
);
}
}
}
}
// 3.使用access_token以及OpenID来访问和修改用户数据
String
getQQUserInfoUrl
=
"https://graph.qq.com/user/get_user_info?"
+
"access_token="
+
access_token
+
"&oauth_consumer_key="
+
appId
+
}
catch
(
Exception
e
)
{
"&openid="
+
openid
;
e
.
printStackTrace
();
returnObj
=
JSONObject
.
parseObject
(
HttpUtils
.
get
(
getQQUserInfoUrl
,
null
,
null
));
throw
new
InternalServerErrorException
(
"获取user失败"
);
if
(
StringUtils
.
isEmpty
(
returnObj
))
{
throw
new
BadRequestAlertException
(
"QQ服务端返回结果为空!"
,
"UserQQRegisterService"
,
""
);
}
else
if
(
returnObj
.
getInteger
(
"ret"
)!=
0
)
{
throw
new
BadRequestAlertException
(
"获取QQ用户信息失败!"
,
"UserQQRegisterService"
,
""
);
}
else
{
returnObj
.
put
(
"openid"
,
openid
);
}
}
catch
(
UnsupportedEncodingException
e
)
{
throw
new
BadRequestAlertException
(
"连接QQ服务端失败!"
,
"UserQQRegisterService"
,
""
);
}
}
return
returnObj
;
return
returnObj
;
...
...
ibzuaa-core/src/main/java/cn/ibizlab/core/uaa/extensions/service/UserWechatRegisterService.java
浏览文件 @
74bbe35e
package
cn
.
ibizlab
.
core
.
uaa
.
extensions
.
service
;
package
cn
.
ibizlab
.
core
.
uaa
.
extensions
.
service
;
import
cn.ibizlab.core.uaa.domain.SysOpenAccess
;
import
cn.ibizlab.core.uaa.domain.SysUserAuth
;
import
cn.ibizlab.core.uaa.domain.SysUserAuth
;
import
cn.ibizlab.core.uaa.service.ISysOpenAccessService
;
import
cn.ibizlab.core.uaa.service.ISysUserAuthService
;
import
cn.ibizlab.core.uaa.service.ISysUserAuthService
;
import
cn.ibizlab.util.domain.IBZUSER
;
import
cn.ibizlab.util.domain.IBZUSER
;
import
cn.ibizlab.util.errors.BadRequestAlertException
;
import
cn.ibizlab.util.errors.BadRequestAlertException
;
import
cn.ibizlab.util.errors.InternalServerErrorException
;
import
cn.ibizlab.util.helper.HttpUtils
;
import
cn.ibizlab.util.helper.HttpUtils
;
import
cn.ibizlab.util.service.IBZUSERService
;
import
cn.ibizlab.util.service.IBZUSERService
;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONObject
;
import
com.baomidou.mybatisplus.core.toolkit.Wrappers
;
import
com.dingtalk.api.DefaultDingTalkClient
;
import
com.dingtalk.api.request.OapiSnsGetuserinfoBycodeRequest
;
import
com.dingtalk.api.response.OapiSnsGetuserinfoBycodeResponse
;
import
com.taobao.api.ApiException
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
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
org.springframework.util.StringUtils
;
import
java.io.UnsupportedEncodingException
;
import
java.io.UnsupportedEncodingException
;
import
java.sql.Timestamp
;
/**
/**
* 实体[IBZUSER] 微信用户注册接口实现
* 实体[IBZUSER] 微信用户注册接口实现
...
@@ -26,92 +35,78 @@ public class UserWechatRegisterService {
...
@@ -26,92 +35,78 @@ public class UserWechatRegisterService {
@Autowired
@Autowired
private
ISysUserAuthService
sysUserAuthService
;
private
ISysUserAuthService
sysUserAuthService
;
/**
* 注册
@Autowired
*
private
ISysOpenAccessService
sysOpenAccessService
;
* @param ibzuser
*/
public
SysOpenAccess
getOpenAccess
(
String
id
)
public
void
toRegister
(
IBZUSER
ibzuser
)
{
{
// 创建ibzuser
return
getOpenAccess
(
id
,
true
);
boolean
flag
=
ibzuserService
.
save
(
ibzuser
);
if
(!
flag
)
{
throw
new
BadRequestAlertException
(
"注册失败"
,
"UserWechatRegisterService"
,
""
);
}
}
}
public
SysOpenAccess
getOpenAccess
(
String
id
,
boolean
throwEx
)
{
final
String
accessid
=
StringUtils
.
isEmpty
(
id
)?
"wechat"
:
id
;
SysOpenAccess
sysOpenAccess
=
sysOpenAccessService
.
getOne
(
Wrappers
.<
SysOpenAccess
>
lambdaQuery
().
eq
(
SysOpenAccess:
:
getOpenType
,
"wechat"
).
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
(
"获取接入配置失败"
,
"UserWechatRegisterService"
,
""
);
/**
return
sysOpenAccess
;
* 创建微信用户授权信息
* @param userAuth
*/
public
void
toCreateUserAuth
(
SysUserAuth
userAuth
)
{
// 创建用户授权信息
boolean
flag
=
sysUserAuthService
.
create
(
userAuth
);
if
(!
flag
)
{
throw
new
BadRequestAlertException
(
"保存用户授权信息失败"
,
"UserWechatRegisterService"
,
""
);
}
}
}
/**
/**
*
通过code获取微信用户
信息
*
微信服务端通过临时授权码code获取授权用户的个人
信息
*
*
* @param code
* @param state
* @param appId
* @param appSecret
* @return
* @return
*/
*/
public
JSONObject
requestWechatUserByCode
(
String
code
,
String
state
,
String
appId
,
String
appSecret
)
{
public
JSONObject
getUserBySnsToken
(
String
id
,
String
requestAuthCode
)
{
JSONObject
returnObj
=
null
;
JSONObject
returnObj
=
null
;
SysOpenAccess
openAccess
=
getOpenAccess
(
id
);
if
(
openAccess
==
null
||
(
openAccess
.
getDisabled
()!=
null
&&
openAccess
.
getDisabled
()==
1
))
throw
new
BadRequestAlertException
(
"未找到配置"
,
"UserWechatRegisterService"
,
""
);
try
{
try
{
// 1.根据code获取access_token、openid、refresh_token
// 1.根据code获取access_token、openid、refresh_token
String
getAccessTokenUrl
=
"https://api.weixin.qq.com/sns/oauth2/access_token?"
+
String
getAccessTokenUrl
=
"https://api.weixin.qq.com/sns/oauth2/access_token?"
+
"appid="
+
appId
+
"appid="
+
openAccess
.
getAccessKey
()
+
"&secret="
+
appSecret
+
"&secret="
+
openAccess
.
getSecretKey
()
+
"&code="
+
c
ode
+
"&code="
+
requestAuthC
ode
+
"&grant_type=authorization_code"
;
"&grant_type=authorization_code"
;
JSONObject
response
Obj
=
JSONObject
.
parseObject
(
HttpUtils
.
get
(
getAccessTokenUrl
,
null
,
null
));
return
Obj
=
JSONObject
.
parseObject
(
HttpUtils
.
get
(
getAccessTokenUrl
,
null
,
null
));
if
(!
re
sponseObj
.
containsKey
(
"access_token"
)
&&
!
response
Obj
.
containsKey
(
"openid"
))
{
if
(!
re
turnObj
.
containsKey
(
"access_token"
)
&&
!
return
Obj
.
containsKey
(
"openid"
))
{
throw
new
BadRequestAlertException
(
"微信服务端获取access_token失败!"
,
"UserWechatRegisterService"
,
""
);
throw
new
BadRequestAlertException
(
"微信服务端获取access_token失败!"
,
"UserWechatRegisterService"
,
""
);
}
}
String
access_token
=
responseObj
.
getString
(
"access_token"
);
String
access_token
=
returnObj
.
getString
(
"access_token"
);
String
openid
=
responseObj
.
getString
(
"openid"
);
String
openid
=
returnObj
.
getString
(
"openid"
);
String
refresh_token
=
responseObj
.
getString
(
"refresh_token"
);
String
refresh_token
=
returnObj
.
getString
(
"refresh_token"
);
String
unionid
=
returnObj
.
getString
(
"unionid"
);
// 2.检验授权凭证(access_token)是否有效,无效则需要刷新access_token
String
checkAccessTokenUrl
=
"https://api.weixin.qq.com/sns/auth?"
+
"access_token="
+
access_token
+
SysUserAuth
userAuth
=
sysUserAuthService
.
getOne
(
Wrappers
.<
SysUserAuth
>
lambdaQuery
().
eq
(
SysUserAuth:
:
getIdentityType
,
"webchat"
)
"&openid="
+
openid
;
.
and
(
wrapper
->
wrapper
.
eq
(
SysUserAuth:
:
getIdentifier
,
openid
).
or
().
eq
(
SysUserAuth:
:
getIdentifier
,
unionid
)
JSONObject
responseObj2
=
JSONObject
.
parseObject
(
HttpUtils
.
get
(
checkAccessTokenUrl
,
null
,
null
));
),
false
);
if
(
responseObj2
.
getInteger
(
"errcode"
)
!=
0
)
{
// access_token已失效,使用refresh_token刷新access_token
IBZUSER
user
=
null
;
String
refreshAccess_token
=
"https://api.weixin.qq.com/sns/oauth2/refresh_token?"
+
// 该wechat用户注册过账号,登录系统
"appid="
+
appId
+
if
(
userAuth
!=
null
)
{
"&grant_type=refresh_token"
+
user
=
ibzuserService
.
getById
(
userAuth
.
getUserid
());
"&refresh_token="
+
refresh_token
;
if
(
user
==
null
)
JSONObject
responseObj3
=
JSONObject
.
parseObject
(
HttpUtils
.
get
(
refreshAccess_token
,
null
,
null
));
user
=
ibzuserService
.
getOne
(
Wrappers
.<
IBZUSER
>
lambdaQuery
().
eq
(
IBZUSER:
:
getUserid
,
openid
).
or
().
eq
(
IBZUSER:
:
getUsername
,
openid
),
false
);
if
(!
responseObj3
.
containsKey
(
"access_token"
)
||
!
responseObj3
.
containsKey
(
"openid"
))
{
throw
new
BadRequestAlertException
(
"微信服务端刷新access_token失败!"
,
"UserWechatRegisterService"
,
""
);
if
(
user
!=
null
)
{
returnObj
.
put
(
"username"
,
user
.
getLoginname
()+(
StringUtils
.
isEmpty
(
user
.
getDomains
())?
""
:(
"|"
+
user
.
getDomains
())));
}
}
// 重新拿到access_token、openid、refresh_token
access_token
=
responseObj3
.
getString
(
"access_token"
);
openid
=
responseObj3
.
getString
(
"openid"
);
refresh_token
=
responseObj3
.
getString
(
"refresh_token"
);
}
// 3.access_token有效,拉取用户信息
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
));
if
(
StringUtils
.
isEmpty
(
returnObj
))
{
throw
new
BadRequestAlertException
(
"微信服务端返回结果为空!"
,
"UserWechatRegisterService"
,
""
);
}
else
if
(!
returnObj
.
containsKey
(
"openid"
))
{
throw
new
BadRequestAlertException
(
"获取微信用户信息失败!"
,
"UserWechatRegisterService"
,
""
);
}
}
}
catch
(
UnsupportedEncodingException
e
)
{
throw
new
BadRequestAlertException
(
"连接微信服务端失败!"
,
"UserWechatRegisterService"
,
""
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
throw
new
InternalServerErrorException
(
"获取user失败"
);
}
}
return
returnObj
;
return
returnObj
;
...
...
ibzuaa-provider/ibzuaa-provider-api/src/main/java/cn/ibizlab/api/rest/extensions/UserDingtalkRegisterResource.java
浏览文件 @
74bbe35e
...
@@ -50,7 +50,7 @@ public class UserDingtalkRegisterResource {
...
@@ -50,7 +50,7 @@ public class UserDingtalkRegisterResource {
@GetMapping
(
value
=
{
"/uaa/getDingtalkAppId"
,
"/uaa/open/dingtalk/access_token"
,
"/uaa/open/dingtalk/appid"
})
@GetMapping
(
value
=
{
"/uaa/getDingtalkAppId"
,
"/uaa/open/dingtalk/access_token"
,
"/uaa/open/dingtalk/appid"
})
public
ResponseEntity
<
JSONObject
>
getDingtalkAppId
(
@RequestParam
(
value
=
"id"
,
required
=
false
)
String
id
)
{
public
ResponseEntity
<
JSONObject
>
getDingtalkAppId
(
@RequestParam
(
value
=
"id"
,
required
=
false
)
String
id
)
{
JSONObject
obj
=
new
JSONObject
();
JSONObject
obj
=
new
JSONObject
();
SysOpenAccess
openAccess
=
userDingtalkRegisterService
.
getOpenAccess
(
id
);
SysOpenAccess
openAccess
=
userDingtalkRegisterService
.
getOpenAccess
(
id
,
false
);
if
(
openAccess
==
null
||
(
openAccess
.
getDisabled
()!=
null
&&
openAccess
.
getDisabled
()==
1
))
if
(
openAccess
==
null
||
(
openAccess
.
getDisabled
()!=
null
&&
openAccess
.
getDisabled
()==
1
))
return
ResponseEntity
.
ok
(
obj
);
return
ResponseEntity
.
ok
(
obj
);
String
appId
=
openAccess
.
getAccessKey
();
String
appId
=
openAccess
.
getAccessKey
();
...
...
ibzuaa-provider/ibzuaa-provider-api/src/main/java/cn/ibizlab/api/rest/extensions/UserQQRegisterResource.java
浏览文件 @
74bbe35e
...
@@ -3,12 +3,14 @@ package cn.ibizlab.api.rest.extensions;
...
@@ -3,12 +3,14 @@ package cn.ibizlab.api.rest.extensions;
import
cn.ibizlab.core.uaa.domain.SysOpenAccess
;
import
cn.ibizlab.core.uaa.domain.SysOpenAccess
;
import
cn.ibizlab.core.uaa.domain.SysUserAuth
;
import
cn.ibizlab.core.uaa.domain.SysUserAuth
;
import
cn.ibizlab.core.uaa.extensions.service.UserQQRegisterService
;
import
cn.ibizlab.core.uaa.extensions.service.UserQQRegisterService
;
import
cn.ibizlab.core.uaa.extensions.service.UserRegisterService
;
import
cn.ibizlab.core.uaa.service.ISysOpenAccessService
;
import
cn.ibizlab.core.uaa.service.ISysOpenAccessService
;
import
cn.ibizlab.core.uaa.service.ISysUserAuthService
;
import
cn.ibizlab.core.uaa.service.ISysUserAuthService
;
import
cn.ibizlab.util.domain.IBZUSER
;
import
cn.ibizlab.util.domain.IBZUSER
;
import
cn.ibizlab.util.errors.BadRequestAlertException
;
import
cn.ibizlab.util.errors.BadRequestAlertException
;
import
cn.ibizlab.util.helper.CachedBeanCopier
;
import
cn.ibizlab.util.helper.CachedBeanCopier
;
import
cn.ibizlab.util.security.AuthTokenUtil
;
import
cn.ibizlab.util.security.AuthTokenUtil
;
import
cn.ibizlab.util.security.AuthenticationInfo
;
import
cn.ibizlab.util.security.AuthenticationUser
;
import
cn.ibizlab.util.security.AuthenticationUser
;
import
cn.ibizlab.util.service.AuthenticationUserService
;
import
cn.ibizlab.util.service.AuthenticationUserService
;
import
cn.ibizlab.util.service.IBZUSERService
;
import
cn.ibizlab.util.service.IBZUSERService
;
...
@@ -18,10 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
...
@@ -18,10 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.util.StringUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.util.List
;
import
java.util.List
;
import
java.util.UUID
;
import
java.util.UUID
;
...
@@ -33,6 +32,8 @@ public class UserQQRegisterResource {
...
@@ -33,6 +32,8 @@ public class UserQQRegisterResource {
@Autowired
@Autowired
private
UserQQRegisterService
userQQRegisterService
;
private
UserQQRegisterService
userQQRegisterService
;
@Autowired
@Autowired
private
UserRegisterService
userRegisterService
;
@Autowired
private
IBZUSERService
ibzuserService
;
private
IBZUSERService
ibzuserService
;
@Autowired
@Autowired
private
ISysUserAuthService
sysUserAuthService
;
private
ISysUserAuthService
sysUserAuthService
;
...
@@ -47,67 +48,59 @@ public class UserQQRegisterResource {
...
@@ -47,67 +48,59 @@ public class UserQQRegisterResource {
/**
/**
* 获取QQ互联平台创建的网站应用appid
* 获取QQ互联平台创建的网站应用appid
*/
*/
@GetMapping
(
value
=
"/uaa/getQQAppId"
)
@GetMapping
(
value
=
{
"/uaa/getQQAppId"
,
"/uaa/open/qq/access_token"
,
"/uaa/open/qq/appid"
}
)
public
ResponseEntity
<
JSONObject
>
getQQAppId
()
{
public
ResponseEntity
<
JSONObject
>
getQQAppId
(
@RequestParam
(
value
=
"id"
,
required
=
false
)
String
id
)
{
JSONObject
obj
=
new
JSONObject
();
JSONObject
obj
=
new
JSONObject
();
SysOpenAccess
openAccess
=
openAccessService
.
getById
(
"qq"
);
SysOpenAccess
openAccess
=
userQQRegisterService
.
getOpenAccess
(
id
);
if
(
openAccess
==
null
||
(
openAccess
.
getDisabled
()!=
null
&&
openAccess
.
getDisabled
()==
1
))
if
(
openAccess
==
null
||
(
openAccess
.
getDisabled
()!=
null
&&
openAccess
.
getDisabled
()==
1
))
return
ResponseEntity
.
ok
(
obj
);
return
ResponseEntity
.
ok
(
obj
);
String
appId
=
openAccess
.
getAccessKey
();
// qq互联appid
String
appId
=
openAccess
.
getAccessKey
();
// qq互联appid
if
(!
StringUtils
.
isEmpty
(
appId
))
{
if
(!
StringUtils
.
isEmpty
(
appId
))
{
obj
.
put
(
"appid"
,
appId
);
obj
.
put
(
"appid"
,
appId
);
obj
.
put
(
"corp_id"
,
openAccess
.
getRegionId
());
obj
.
put
(
"redirect_uri"
,
openAccess
.
getRedirectUri
());
}
}
return
ResponseEntity
.
ok
(
obj
);
return
ResponseEntity
.
ok
(
obj
);
}
}
/**
/**
* 根据code查
QQ
用户
* 根据code查
qq
用户
*
*
* @param param
* @param param
* @return
* @return
*/
*/
@PostMapping
(
value
=
"/uaa/queryQQUserByCode"
)
@PostMapping
(
value
=
"/uaa/queryQQUserByCode"
)
public
ResponseEntity
<
JSONObject
>
queryQQUserByCode
(
@RequestBody
JSONObject
param
)
{
public
ResponseEntity
<
JSONObject
>
queryQQUserByCode
(
@Request
Param
(
value
=
"id"
,
required
=
false
)
String
id
,
@RequestParam
(
value
=
"code"
,
required
=
false
)
String
tmpcode
,
@Request
Body
JSONObject
param
)
{
JSONObject
object
=
new
JSONObject
();
// 空校验
// 空校验
String
code
=
param
.
getString
(
"code"
);
String
code
=
param
.
getString
(
"code"
);
if
(
StringUtils
.
isEmpty
(
code
))
code
=
tmpcode
;
if
(
StringUtils
.
isEmpty
(
code
))
if
(
StringUtils
.
isEmpty
(
code
))
throw
new
BadRequestAlertException
(
"code为空"
,
"UserQQRegisterResource"
,
""
);
throw
new
BadRequestAlertException
(
"code为空"
,
"UserQQRegisterResource"
,
""
);
// 从数据库中获取qq互联信息
return
ResponseEntity
.
ok
().
body
(
getUserBySnsCode
(
id
,
code
));
SysOpenAccess
openAccess
=
openAccessService
.
getById
(
"qq"
);
}
if
(
openAccess
==
null
||
(
openAccess
.
getDisabled
()!=
null
&&
openAccess
.
getDisabled
()==
1
))
throw
new
BadRequestAlertException
(
"未找到配置"
,
"UserQQRegisterResource"
,
""
);
@GetMapping
(
value
=
"/uaa/open/qq/sns/{code}"
)
String
appId
=
openAccess
.
getAccessKey
();
// qq互联appid
public
ResponseEntity
<
JSONObject
>
getUserBySnsToken
(
@PathVariable
(
value
=
"code"
)
String
code
,
@RequestParam
(
value
=
"id"
,
required
=
false
)
String
id
)
{
String
appSecret
=
openAccess
.
getSecretKey
();
// qq互联appkey
if
(
StringUtils
.
isEmpty
(
code
))
String
redirectUri
=
openAccess
.
getRedirectUri
();
// qq互联应用回调地址
throw
new
BadRequestAlertException
(
"code为空"
,
"UserQQRegisterResource"
,
""
);
// 通过code获取QQ用户信息
String
openid
=
null
;
String
nickname
=
null
;
JSONObject
returnObj
=
userQQRegisterService
.
requestQQUserByCode
(
code
,
redirectUri
,
appId
,
appSecret
);
if
(!
StringUtils
.
isEmpty
(
returnObj
)
&&
!
returnObj
.
containsKey
(
"errcode"
))
{
openid
=
returnObj
.
getString
(
"openid"
);
nickname
=
returnObj
.
getString
(
"nickname"
);
object
.
put
(
"openid"
,
openid
);
object
.
put
(
"nickname"
,
nickname
);
}
// 根据openid查用户授权信息
return
ResponseEntity
.
ok
().
body
(
getUserBySnsCode
(
id
,
code
));
SysUserAuth
userAuth
=
sysUserAuthService
.
getOne
(
Wrappers
.<
SysUserAuth
>
query
().
eq
(
"identifier"
,
openid
));
}
// 该QQ用户注册过账号,登录系统
if
(!
StringUtils
.
isEmpty
(
userAuth
))
{
IBZUSER
ibzuser
=
ibzuserService
.
getById
(
userAuth
.
getUserid
());
JSONObject
ibzuserObj
=
new
JSONObject
();
ibzuserObj
.
put
(
"loginname"
,
ibzuser
.
getLoginname
());
ibzuserObj
.
put
(
"password"
,
ibzuser
.
getPassword
());
object
.
put
(
"ibzuser"
,
ibzuserObj
);
private
JSONObject
getUserBySnsCode
(
String
id
,
String
code
)
{
JSONObject
object
=
userQQRegisterService
.
getUserBySnsToken
(
id
,
code
);
if
(!
StringUtils
.
isEmpty
(
object
.
getString
(
"username"
)))
{
String
username
=
object
.
getString
(
"username"
);
// 生成登录token信息
// 生成登录token信息
userDetailsService
.
resetByUsername
(
ibzuser
.
getLoginname
()
);
userDetailsService
.
resetByUsername
(
username
);
AuthenticationUser
user
=
userDetailsService
.
loadUserBy
Login
(
ibzuser
.
getLoginname
(),
ibzuser
.
getPassword
()
);
AuthenticationUser
user
=
userDetailsService
.
loadUserBy
Username
(
username
);
final
String
token
=
jwtTokenUtil
.
generateToken
(
user
);
final
String
token
=
jwtTokenUtil
.
generateToken
(
user
);
AuthenticationUser
user2
=
new
AuthenticationUser
();
AuthenticationUser
user2
=
new
AuthenticationUser
();
CachedBeanCopier
.
copy
(
user
,
user2
);
CachedBeanCopier
.
copy
(
user
,
user2
);
...
@@ -116,8 +109,7 @@ public class UserQQRegisterResource {
...
@@ -116,8 +109,7 @@ public class UserQQRegisterResource {
object
.
put
(
"token"
,
token
);
object
.
put
(
"token"
,
token
);
object
.
put
(
"user"
,
user2
);
object
.
put
(
"user"
,
user2
);
}
}
return
object
;
return
ResponseEntity
.
ok
().
body
(
object
);
}
}
...
@@ -127,71 +119,55 @@ public class UserQQRegisterResource {
...
@@ -127,71 +119,55 @@ public class UserQQRegisterResource {
* @param param
* @param param
* @return
* @return
*/
*/
@PostMapping
(
value
=
"/uaa/bindQQtoRegister"
)
@PostMapping
(
value
=
{
"/uaa/bindQQtoRegister"
,
"/uaa/open/qq/bind"
}
)
public
ResponseEntity
<
JSONObject
>
bindQQt
oRegister
(
@RequestBody
JSONObject
param
)
{
public
ResponseEntity
<
AuthenticationInfo
>
bindQQT
oRegister
(
@RequestBody
JSONObject
param
)
{
JSONObject
object
=
new
JSONObject
();
// 空校验
// 空校验
String
loginname
=
param
.
getString
(
"loginname"
);
String
loginname
=
param
.
getString
(
"loginname"
);
String
password
=
param
.
getString
(
"password"
);
String
password
=
param
.
getString
(
"password"
);
String
code
=
param
.
getString
(
"code"
);
String
state
=
param
.
getString
(
"state"
);
String
openid
=
param
.
getString
(
"openid"
);
String
openid
=
param
.
getString
(
"openid"
);
String
nickname
=
param
.
getString
(
"nickname"
);
String
nickname
=
param
.
getString
(
"nickname"
);
String
personname
=
param
.
getString
(
"personname"
);
String
phone
=
param
.
getString
(
"phone"
);
String
email
=
param
.
getString
(
"email"
);
String
domains
=
param
.
getString
(
"domains"
);
if
(
StringUtils
.
isEmpty
(
loginname
))
if
(
StringUtils
.
isEmpty
(
loginname
))
throw
new
BadRequestAlertException
(
"用户名为空"
,
"UserQQRegisterResource"
,
""
);
throw
new
BadRequestAlertException
(
"用户名为空"
,
"UserQQRegisterResource"
,
""
);
if
(
StringUtils
.
isEmpty
(
password
))
if
(
StringUtils
.
isEmpty
(
password
))
throw
new
BadRequestAlertException
(
"密码为空"
,
"UserQQRegisterResource"
,
""
);
throw
new
BadRequestAlertException
(
"密码为空"
,
"UserQQRegisterResource"
,
""
);
if
(
StringUtils
.
isEmpty
(
code
))
throw
new
BadRequestAlertException
(
"QQ授权code为空"
,
"UserQQRegisterResource"
,
""
);
if
(
StringUtils
.
isEmpty
(
state
))
throw
new
BadRequestAlertException
(
"QQ授权state为空"
,
"UserQQRegisterResource"
,
""
);
if
(
StringUtils
.
isEmpty
(
openid
))
if
(
StringUtils
.
isEmpty
(
openid
))
throw
new
BadRequestAlertException
(
"QQ信息openid为空"
,
"UserWechatRegisterResource"
,
""
);
throw
new
BadRequestAlertException
(
"QQ信息openid为空"
,
"UserQQRegisterResource"
,
""
);
if
(
StringUtils
.
isEmpty
(
nickname
))
throw
new
BadRequestAlertException
(
"QQ信息nickname为空"
,
"UserWechatRegisterResource"
,
""
);
// 检查用户名是否已被注册
List
<
IBZUSER
>
ibzusers
=
ibzuserService
.
list
(
Wrappers
.<
IBZUSER
>
query
().
eq
(
"loginname"
,
loginname
));
if
(
ibzusers
.
size
()
>
0
)
throw
new
BadRequestAlertException
(
"该用户名已被注册"
,
"UserQQRegisterResource"
,
""
);
//
QQ
用户注册
//
微信
用户注册
IBZUSER
ibzuser
=
new
IBZUSER
();
IBZUSER
ibzuser
=
new
IBZUSER
();
String
uuid
=
UUID
.
randomUUID
().
toString
();
ibzuser
.
setPassword
(
password
);
ibzuser
.
setPassword
(
password
);
ibzuser
.
setLoginname
(
loginname
);
ibzuser
.
setLoginname
(
loginname
);
ibzuser
.
setUserid
(
"qq-"
+
uuid
);
ibzuser
.
setPersonname
(
StringUtils
.
isEmpty
(
personname
)?
nickname:
personname
);
ibzuser
.
setPersonname
(
nickname
);
ibzuser
.
setNickname
(
nickname
);
ibzuser
.
setNickname
(
nickname
);
userQQRegisterService
.
toRegister
(
ibzuser
);
ibzuser
.
setPhone
(
phone
);
// 创建QQ用户授权信息
ibzuser
.
setEmail
(
email
);
ibzuser
.
setDomains
(
domains
);
SysUserAuth
userAuth
=
new
SysUserAuth
();
SysUserAuth
userAuth
=
new
SysUserAuth
();
userAuth
.
setUserid
(
ibzuser
.
getUserid
());
userAuth
.
setIdentifier
(
openid
);
userAuth
.
setIdentifier
(
openid
);
userAuth
.
setIdentityType
(
"qq"
);
userAuth
.
setIdentityType
(
"qq"
);
userQQRegisterService
.
toCreateUserAuth
(
userAuth
);
userRegisterService
.
toRegister
(
ibzuser
,
userAuth
);
// 注册成功,登录系统
if
(!
StringUtils
.
isEmpty
(
ibzuser
))
{
JSONObject
ibzuserObj
=
new
JSONObject
();
ibzuserObj
.
put
(
"loginname"
,
ibzuser
.
getLoginname
());
ibzuserObj
.
put
(
"password"
,
ibzuser
.
getPassword
());
object
.
put
(
"ibzuser"
,
ibzuserObj
);
}
// 生成登录token信息
// 生成登录token信息
userDetailsService
.
resetByUsername
(
ibzuser
.
getLoginname
());
userDetailsService
.
resetByUsername
(
ibzuser
.
getLoginname
()
+(
StringUtils
.
isEmpty
(
ibzuser
.
getDomains
())?
""
:(
"|"
+
ibzuser
.
getDomains
()))
);
AuthenticationUser
user
=
userDetailsService
.
loadUserBy
Login
(
ibzuser
.
getLoginname
(),
ibzuser
.
getPassword
(
));
AuthenticationUser
user
=
userDetailsService
.
loadUserBy
Username
(
ibzuser
.
getLoginname
()+(
StringUtils
.
isEmpty
(
ibzuser
.
getDomains
())?
""
:(
"|"
+
ibzuser
.
getDomains
())
));
final
String
token
=
jwtTokenUtil
.
generateToken
(
user
);
final
String
token
=
jwtTokenUtil
.
generateToken
(
user
);
AuthenticationUser
user2
=
new
AuthenticationUser
();
AuthenticationUser
user2
=
new
AuthenticationUser
();
CachedBeanCopier
.
copy
(
user
,
user2
);
CachedBeanCopier
.
copy
(
user
,
user2
);
user2
.
setAuthorities
(
null
);
user2
.
setAuthorities
(
null
);
user2
.
setPermissionList
(
null
);
user2
.
setPermissionList
(
null
);
object
.
put
(
"token"
,
token
);
object
.
put
(
"user"
,
user2
);
return
ResponseEntity
.
ok
().
body
(
object
);
return
ResponseEntity
.
ok
().
body
(
new
AuthenticationInfo
(
token
,
user2
));
}
}
}
}
ibzuaa-provider/ibzuaa-provider-api/src/main/java/cn/ibizlab/api/rest/extensions/UserWechatRegisterResource.java
浏览文件 @
74bbe35e
...
@@ -2,6 +2,7 @@ package cn.ibizlab.api.rest.extensions;
...
@@ -2,6 +2,7 @@ package cn.ibizlab.api.rest.extensions;
import
cn.ibizlab.core.uaa.domain.SysOpenAccess
;
import
cn.ibizlab.core.uaa.domain.SysOpenAccess
;
import
cn.ibizlab.core.uaa.domain.SysUserAuth
;
import
cn.ibizlab.core.uaa.domain.SysUserAuth
;
import
cn.ibizlab.core.uaa.extensions.service.UserRegisterService
;
import
cn.ibizlab.core.uaa.extensions.service.UserWechatRegisterService
;
import
cn.ibizlab.core.uaa.extensions.service.UserWechatRegisterService
;
import
cn.ibizlab.core.uaa.service.ISysOpenAccessService
;
import
cn.ibizlab.core.uaa.service.ISysOpenAccessService
;
import
cn.ibizlab.core.uaa.service.ISysUserAuthService
;
import
cn.ibizlab.core.uaa.service.ISysUserAuthService
;
...
@@ -9,6 +10,7 @@ import cn.ibizlab.util.domain.IBZUSER;
...
@@ -9,6 +10,7 @@ import cn.ibizlab.util.domain.IBZUSER;
import
cn.ibizlab.util.errors.BadRequestAlertException
;
import
cn.ibizlab.util.errors.BadRequestAlertException
;
import
cn.ibizlab.util.helper.CachedBeanCopier
;
import
cn.ibizlab.util.helper.CachedBeanCopier
;
import
cn.ibizlab.util.security.AuthTokenUtil
;
import
cn.ibizlab.util.security.AuthTokenUtil
;
import
cn.ibizlab.util.security.AuthenticationInfo
;
import
cn.ibizlab.util.security.AuthenticationUser
;
import
cn.ibizlab.util.security.AuthenticationUser
;
import
cn.ibizlab.util.service.AuthenticationUserService
;
import
cn.ibizlab.util.service.AuthenticationUserService
;
import
cn.ibizlab.util.service.IBZUSERService
;
import
cn.ibizlab.util.service.IBZUSERService
;
...
@@ -18,10 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
...
@@ -18,10 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.util.StringUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.util.List
;
import
java.util.List
;
import
java.util.UUID
;
import
java.util.UUID
;
...
@@ -33,6 +32,8 @@ public class UserWechatRegisterResource {
...
@@ -33,6 +32,8 @@ public class UserWechatRegisterResource {
@Autowired
@Autowired
private
UserWechatRegisterService
userWechatRegisterService
;
private
UserWechatRegisterService
userWechatRegisterService
;
@Autowired
@Autowired
private
UserRegisterService
userRegisterService
;
@Autowired
private
IBZUSERService
ibzuserService
;
private
IBZUSERService
ibzuserService
;
@Autowired
@Autowired
private
ISysUserAuthService
sysUserAuthService
;
private
ISysUserAuthService
sysUserAuthService
;
...
@@ -47,21 +48,25 @@ public class UserWechatRegisterResource {
...
@@ -47,21 +48,25 @@ public class UserWechatRegisterResource {
/**
/**
* 获取微信开放平台创建的网站应用appid
* 获取微信开放平台创建的网站应用appid
*/
*/
@GetMapping
(
value
=
"/uaa/getWechatAppId"
)
@GetMapping
(
value
=
{
"/uaa/getWechatAppId"
,
"/uaa/open/wechat/access_token"
,
"/uaa/open/wechat/appid"
}
)
public
ResponseEntity
<
JSONObject
>
getWechatAppId
()
{
public
ResponseEntity
<
JSONObject
>
getWechatAppId
(
@RequestParam
(
value
=
"id"
,
required
=
false
)
String
id
)
{
JSONObject
obj
=
new
JSONObject
();
JSONObject
obj
=
new
JSONObject
();
SysOpenAccess
openAccess
=
openAccessService
.
getById
(
"webchart"
);
SysOpenAccess
openAccess
=
userWechatRegisterService
.
getOpenAccess
(
id
,
false
);
if
(
openAccess
==
null
||
(
openAccess
.
getDisabled
()!=
null
&&
openAccess
.
getDisabled
()==
1
))
if
(
openAccess
==
null
||
(
openAccess
.
getDisabled
()!=
null
&&
openAccess
.
getDisabled
()==
1
))
return
ResponseEntity
.
ok
(
obj
);
return
ResponseEntity
.
ok
(
obj
);
String
appId
=
openAccess
.
getAccessKey
();
// qq互联appid
String
appId
=
openAccess
.
getAccessKey
();
// qq互联appid
if
(!
StringUtils
.
isEmpty
(
appId
))
{
if
(!
StringUtils
.
isEmpty
(
appId
))
{
obj
.
put
(
"appid"
,
appId
);
obj
.
put
(
"appid"
,
appId
);
obj
.
put
(
"corp_id"
,
openAccess
.
getRegionId
());
obj
.
put
(
"redirect_uri"
,
openAccess
.
getRedirectUri
());
}
}
return
ResponseEntity
.
ok
(
obj
);
return
ResponseEntity
.
ok
(
obj
);
}
}
/**
/**
* 根据code查微信用户
* 根据code查微信用户
*
*
...
@@ -69,44 +74,34 @@ public class UserWechatRegisterResource {
...
@@ -69,44 +74,34 @@ public class UserWechatRegisterResource {
* @return
* @return
*/
*/
@PostMapping
(
value
=
"/uaa/queryWechatUserByCode"
)
@PostMapping
(
value
=
"/uaa/queryWechatUserByCode"
)
public
ResponseEntity
<
JSONObject
>
queryWechatUserByCode
(
@RequestBody
JSONObject
param
)
{
public
ResponseEntity
<
JSONObject
>
queryWechatUserByCode
(
@Request
Param
(
value
=
"id"
,
required
=
false
)
String
id
,
@RequestParam
(
value
=
"code"
,
required
=
false
)
String
tmpcode
,
@Request
Body
JSONObject
param
)
{
JSONObject
object
=
new
JSONObject
();
// 空校验
// 空校验
String
code
=
param
.
getString
(
"code"
);
String
code
=
param
.
getString
(
"code"
);
if
(
StringUtils
.
isEmpty
(
code
))
code
=
tmpcode
;
if
(
StringUtils
.
isEmpty
(
code
))
if
(
StringUtils
.
isEmpty
(
code
))
throw
new
BadRequestAlertException
(
"code为空"
,
"UserWechatRegisterResource"
,
""
);
throw
new
BadRequestAlertException
(
"code为空"
,
"UserWechatRegisterResource"
,
""
);
// 从数据库中获取微信授权应用信息
return
ResponseEntity
.
ok
().
body
(
getUserBySnsCode
(
id
,
code
));
SysOpenAccess
openAccess
=
openAccessService
.
getById
(
"webchart"
);
}
if
(
openAccess
==
null
||
(
openAccess
.
getDisabled
()!=
null
&&
openAccess
.
getDisabled
()==
1
))
throw
new
BadRequestAlertException
(
"未找到配置"
,
"UserWechatRegisterResource"
,
""
);
@GetMapping
(
value
=
"/uaa/open/wechat/sns/{code}"
)
String
appId
=
openAccess
.
getAccessKey
();
// 微信开放平台appid
public
ResponseEntity
<
JSONObject
>
getUserBySnsToken
(
@PathVariable
(
value
=
"code"
)
String
code
,
@RequestParam
(
value
=
"id"
,
required
=
false
)
String
id
)
{
String
appSecret
=
openAccess
.
getSecretKey
();
// 微信开放平台appsecret
if
(
StringUtils
.
isEmpty
(
code
))
throw
new
BadRequestAlertException
(
"code为空"
,
"UserWechatRegisterResource"
,
""
);
// 通过code获取微信用户信息
String
openid
=
null
;
String
nickname
=
null
;
JSONObject
returnObj
=
userWechatRegisterService
.
requestWechatUserByCode
(
code
,
null
,
appId
,
appSecret
);
if
(!
StringUtils
.
isEmpty
(
returnObj
)
&&
!
returnObj
.
containsKey
(
"errcode"
))
{
openid
=
returnObj
.
getString
(
"openid"
);
nickname
=
returnObj
.
getString
(
"nickname"
);
object
.
put
(
"openid"
,
openid
);
object
.
put
(
"nickname"
,
nickname
);
}
// 根据openid查用户授权信息
return
ResponseEntity
.
ok
().
body
(
getUserBySnsCode
(
id
,
code
));
SysUserAuth
userAuth
=
sysUserAuthService
.
getOne
(
Wrappers
.<
SysUserAuth
>
query
().
eq
(
"identifier"
,
openid
));
}
// 该微信用户注册过账号,登录系统
if
(!
StringUtils
.
isEmpty
(
userAuth
))
{
IBZUSER
ibzuser
=
ibzuserService
.
getById
(
userAuth
.
getUserid
());
JSONObject
ibzuserObj
=
new
JSONObject
();
ibzuserObj
.
put
(
"loginname"
,
ibzuser
.
getLoginname
());
ibzuserObj
.
put
(
"password"
,
ibzuser
.
getPassword
());
object
.
put
(
"ibzuser"
,
ibzuserObj
);
private
JSONObject
getUserBySnsCode
(
String
id
,
String
code
)
{
JSONObject
object
=
userWechatRegisterService
.
getUserBySnsToken
(
id
,
code
);
if
(!
StringUtils
.
isEmpty
(
object
.
getString
(
"username"
)))
{
String
username
=
object
.
getString
(
"username"
);
// 生成登录token信息
// 生成登录token信息
userDetailsService
.
resetByUsername
(
ibzuser
.
getLoginname
()
);
userDetailsService
.
resetByUsername
(
username
);
AuthenticationUser
user
=
userDetailsService
.
loadUserBy
Login
(
ibzuser
.
getLoginname
(),
ibzuser
.
getPassword
()
);
AuthenticationUser
user
=
userDetailsService
.
loadUserBy
Username
(
username
);
final
String
token
=
jwtTokenUtil
.
generateToken
(
user
);
final
String
token
=
jwtTokenUtil
.
generateToken
(
user
);
AuthenticationUser
user2
=
new
AuthenticationUser
();
AuthenticationUser
user2
=
new
AuthenticationUser
();
CachedBeanCopier
.
copy
(
user
,
user2
);
CachedBeanCopier
.
copy
(
user
,
user2
);
...
@@ -115,8 +110,7 @@ public class UserWechatRegisterResource {
...
@@ -115,8 +110,7 @@ public class UserWechatRegisterResource {
object
.
put
(
"token"
,
token
);
object
.
put
(
"token"
,
token
);
object
.
put
(
"user"
,
user2
);
object
.
put
(
"user"
,
user2
);
}
}
return
object
;
return
ResponseEntity
.
ok
().
body
(
object
);
}
}
...
@@ -126,71 +120,56 @@ public class UserWechatRegisterResource {
...
@@ -126,71 +120,56 @@ public class UserWechatRegisterResource {
* @param param
* @param param
* @return
* @return
*/
*/
@PostMapping
(
value
=
"/uaa/bindWechatToRegister"
)
@PostMapping
(
value
=
{
"/uaa/bindWechatToRegister"
,
"/uaa/open/wechat/bind"
}
)
public
ResponseEntity
<
JSONObject
>
bindWechatToRegister
(
@RequestBody
JSONObject
param
)
{
public
ResponseEntity
<
AuthenticationInfo
>
bindWechatToRegister
(
@RequestBody
JSONObject
param
)
{
JSONObject
object
=
new
JSONObject
();
// 空校验
// 空校验
String
loginname
=
param
.
getString
(
"loginname"
);
String
loginname
=
param
.
getString
(
"loginname"
);
String
password
=
param
.
getString
(
"password"
);
String
password
=
param
.
getString
(
"password"
);
String
openid
=
param
.
getString
(
"openid"
);
String
openid
=
param
.
getString
(
"openid"
);
String
nickname
=
param
.
getString
(
"nickname"
);
String
nickname
=
param
.
getString
(
"nickname"
);
String
code
=
param
.
getString
(
"code"
);
String
personname
=
param
.
getString
(
"personname"
);
String
state
=
param
.
getString
(
"state"
);
String
phone
=
param
.
getString
(
"phone"
);
String
email
=
param
.
getString
(
"email"
);
String
domains
=
param
.
getString
(
"domains"
);
if
(
StringUtils
.
isEmpty
(
loginname
))
if
(
StringUtils
.
isEmpty
(
loginname
))
throw
new
BadRequestAlertException
(
"用户名为空"
,
"UserWechatRegisterResource"
,
""
);
throw
new
BadRequestAlertException
(
"用户名为空"
,
"UserWechatRegisterResource"
,
""
);
if
(
StringUtils
.
isEmpty
(
password
))
if
(
StringUtils
.
isEmpty
(
password
))
throw
new
BadRequestAlertException
(
"密码为空"
,
"UserWechatRegisterResource"
,
""
);
throw
new
BadRequestAlertException
(
"密码为空"
,
"UserWechatRegisterResource"
,
""
);
if
(
StringUtils
.
isEmpty
(
openid
))
if
(
StringUtils
.
isEmpty
(
openid
))
throw
new
BadRequestAlertException
(
"微信信息openid为空"
,
"UserWechatRegisterResource"
,
""
);
throw
new
BadRequestAlertException
(
"微信信息openid为空"
,
"UserWechatRegisterResource"
,
""
);
if
(
StringUtils
.
isEmpty
(
nickname
))
throw
new
BadRequestAlertException
(
"微信信息nickname为空"
,
"UserWechatRegisterResource"
,
""
);
if
(
StringUtils
.
isEmpty
(
code
))
throw
new
BadRequestAlertException
(
"微信授权code为空"
,
"UserWechatRegisterResource"
,
""
);
if
(
StringUtils
.
isEmpty
(
state
))
throw
new
BadRequestAlertException
(
"微信授权state为空"
,
"UserWechatRegisterResource"
,
""
);
// 检查用户名是否已被注册
List
<
IBZUSER
>
ibzusers
=
ibzuserService
.
list
(
Wrappers
.<
IBZUSER
>
query
().
eq
(
"loginname"
,
loginname
));
if
(
ibzusers
.
size
()
>
0
)
throw
new
BadRequestAlertException
(
"该用户名已被注册"
,
"UserWechatRegisterResource"
,
""
);
// 微信用户注册
// 微信用户注册
IBZUSER
ibzuser
=
new
IBZUSER
();
IBZUSER
ibzuser
=
new
IBZUSER
();
String
uuid
=
UUID
.
randomUUID
().
toString
();
ibzuser
.
setPassword
(
password
);
ibzuser
.
setPassword
(
password
);
ibzuser
.
setLoginname
(
loginname
);
ibzuser
.
setLoginname
(
loginname
);
ibzuser
.
setUserid
(
"wechat-"
+
uuid
);
ibzuser
.
setPersonname
(
StringUtils
.
isEmpty
(
personname
)?
nickname:
personname
);
ibzuser
.
setPersonname
(
nickname
);
ibzuser
.
setNickname
(
nickname
);
ibzuser
.
setNickname
(
nickname
);
userWechatRegisterService
.
toRegister
(
ibzuser
);
ibzuser
.
setPhone
(
phone
);
// 创建微信用户授权信息
ibzuser
.
setEmail
(
email
);
ibzuser
.
setDomains
(
domains
);
SysUserAuth
userAuth
=
new
SysUserAuth
();
SysUserAuth
userAuth
=
new
SysUserAuth
();
userAuth
.
setUserid
(
ibzuser
.
getUserid
());
userAuth
.
setIdentifier
(
openid
);
userAuth
.
setIdentifier
(
openid
);
userAuth
.
setIdentityType
(
"wechat"
);
userAuth
.
setIdentityType
(
"wechat"
);
userWechatRegisterService
.
toCreateUserAuth
(
userAuth
);
userRegisterService
.
toRegister
(
ibzuser
,
userAuth
);
// 注册成功,登录系统
if
(!
StringUtils
.
isEmpty
(
ibzuser
))
{
JSONObject
ibzuserObj
=
new
JSONObject
();
ibzuserObj
.
put
(
"loginname"
,
ibzuser
.
getLoginname
());
ibzuserObj
.
put
(
"password"
,
ibzuser
.
getPassword
());
object
.
put
(
"ibzuser"
,
ibzuserObj
);
}
// 生成登录token信息
// 生成登录token信息
userDetailsService
.
resetByUsername
(
ibzuser
.
getLoginname
());
userDetailsService
.
resetByUsername
(
ibzuser
.
getLoginname
()
+(
StringUtils
.
isEmpty
(
ibzuser
.
getDomains
())?
""
:(
"|"
+
ibzuser
.
getDomains
()))
);
AuthenticationUser
user
=
userDetailsService
.
loadUserBy
Login
(
ibzuser
.
getLoginname
(),
ibzuser
.
getPassword
(
));
AuthenticationUser
user
=
userDetailsService
.
loadUserBy
Username
(
ibzuser
.
getLoginname
()+(
StringUtils
.
isEmpty
(
ibzuser
.
getDomains
())?
""
:(
"|"
+
ibzuser
.
getDomains
())
));
final
String
token
=
jwtTokenUtil
.
generateToken
(
user
);
final
String
token
=
jwtTokenUtil
.
generateToken
(
user
);
AuthenticationUser
user2
=
new
AuthenticationUser
();
AuthenticationUser
user2
=
new
AuthenticationUser
();
CachedBeanCopier
.
copy
(
user
,
user2
);
CachedBeanCopier
.
copy
(
user
,
user2
);
user2
.
setAuthorities
(
null
);
user2
.
setAuthorities
(
null
);
user2
.
setPermissionList
(
null
);
user2
.
setPermissionList
(
null
);
object
.
put
(
"token"
,
token
);
object
.
put
(
"user"
,
user2
);
return
ResponseEntity
.
ok
().
body
(
object
);
return
ResponseEntity
.
ok
().
body
(
new
AuthenticationInfo
(
token
,
user2
));
}
}
}
}
编辑
预览
Markdown
格式
0%
请重试
or
添加新附件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录