Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
聂康
local-rnr-user
Commits
02be8110
Commit
02be8110
authored
Jun 17, 2025
by
kang.nie@inzymeits.com
Browse files
初始化代码
parent
e9f88257
Pipeline
#3111
failed with stages
in 0 seconds
Changes
259
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/auth/identification/service/impl/CaptchaServiceImpl.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.auth.identification.service.impl
;
import
com.cache.CacheFactory
;
import
com.cache.exception.CacheException
;
import
com.cusc.nirvana.user.auth.common.constants.RedisConstant
;
import
com.cusc.nirvana.user.auth.common.constants.ResponseCode
;
import
com.cusc.nirvana.user.auth.identification.dto.CaptchaCreateReq
;
import
com.cusc.nirvana.user.auth.identification.dto.CaptchaCreateResp
;
import
com.cusc.nirvana.user.auth.identification.dto.CaptchaVerificationReq
;
import
com.cusc.nirvana.user.auth.identification.service.ICaptchaService
;
import
com.cusc.nirvana.user.config.SmsPropertyConfig
;
import
com.cusc.nirvana.user.exception.CuscUserException
;
import
com.wf.captcha.SpecCaptcha
;
import
com.wf.captcha.base.Captcha
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.awt.FontFormatException
;
import
java.io.*
;
import
java.util.UUID
;
/**
* Description: 图形验证码业务层
* <br />
* CreateDate 2022-01-24 10:32:09
*
* @author yuyi
**/
@Service
@Slf4j
public
class
CaptchaServiceImpl
implements
ICaptchaService
{
//验证码的失效时间:秒
private
static
final
int
CAPTCHA_EXPIRE_TIME
=
10800
;
@Autowired
private
CacheFactory
cacheFactory
;
@Autowired
private
SmsPropertyConfig
smsPropertyConfig
;
/**
* Description: 生成图形验证码
* <br />
* CreateDate 2022-01-24 10:32:21
*
* @param bean 图形验证码请求对象
* @author yuyi
**/
@Override
public
CaptchaCreateResp
generateCaptcha
(
CaptchaCreateReq
bean
)
{
CaptchaCreateResp
ret
=
new
CaptchaCreateResp
();
ret
.
setRequestId
(
UUID
.
randomUUID
().
toString
());
//生成随机4位
SpecCaptcha
captcha
=
new
SpecCaptcha
(
bean
.
getCaptchaWidth
(),
bean
.
getCaptchaHeight
(),
bean
.
getCaptchaLength
());
captcha
.
setCharType
(
bean
.
getCaptchaType
().
getCode
());
//不区分大小写
String
code
=
captcha
.
text
().
toLowerCase
();
//放到redis
try
{
cacheFactory
.
getExpireStringService
()
.
setExpireValue
(
RedisConstant
.
IMAGE_CAPTCHA_KEY
+
ret
.
getRequestId
()
+
"_"
+
bean
.
getApplicationId
(),
code
,
CAPTCHA_EXPIRE_TIME
);
}
catch
(
CacheException
e
)
{
log
.
error
(
"generateCaptcha 请求reids失败 :"
,
e
);
throw
new
CuscUserException
(
ResponseCode
.
REDIS_OPT_FAIL
.
getCode
()
+
""
,
ResponseCode
.
REDIS_OPT_FAIL
.
getMsg
());
}
try
{
captcha
.
setFont
(
Captcha
.
FONT_2
);
}
catch
(
IOException
e
)
{
log
.
error
(
"generateCaptcha IOException 设置字体失败 :"
,
e
);
}
catch
(
FontFormatException
e
)
{
log
.
error
(
"generateCaptcha FontFormatException 设置字体失败 :"
,
e
);
}
ret
.
setCaptchaImg
(
captcha
.
toBase64
());
return
ret
;
}
/**
* Description: 验证图形验证码
* <br />
* CreateDate 2022-01-24 10:32:21
*
* @param bean 图形验证码请求对象
* @author yuyi
**/
@Override
public
boolean
verificationCaptcha
(
CaptchaVerificationReq
bean
)
{
// 获取redis中的验证码
String
redisCode
;
try
{
String
imageCaptchaKey
=
RedisConstant
.
IMAGE_CAPTCHA_KEY
+
bean
.
getRequestId
()
+
"_"
+
bean
.
getApplicationId
();
redisCode
=
cacheFactory
.
getExpireStringService
()
.
getValue
(
imageCaptchaKey
,
String
.
class
);
//清理图形验证码
cacheFactory
.
getExpireStringService
().
delete
(
imageCaptchaKey
);
}
catch
(
CacheException
e
)
{
log
.
error
(
"verificationCaptcha 请求reids失败 :"
,
e
);
throw
new
CuscUserException
(
ResponseCode
.
REDIS_OPT_FAIL
.
getCode
()
+
""
,
ResponseCode
.
REDIS_OPT_FAIL
.
getMsg
());
}
// 判断验证码
if
(
bean
.
getCaptchaValue
().
toLowerCase
().
equals
(
redisCode
))
{
return
true
;
}
log
.
info
(
"verificationCaptcha false . param requestId:{} , input code:{} , redis code:{} "
,
bean
.
getRequestId
(),
bean
.
getCaptchaValue
(),
redisCode
);
return
false
;
}
/**
* Description: 检查短信验证码验证错误次数
* <br />
* CreateDate 2022-07-08 19:41:51
*
* @author yuyi
**/
public
void
checkSmsCaptchaErrorCount
(
String
phone
,
String
tenantNo
,
String
appId
)
{
Integer
errorCount
;
try
{
String
redisKey
=
RedisConstant
.
SMS_CAPTCHA_ERROR_COUNT_KEY
+
phone
+
"_"
+
tenantNo
+
"_"
+
appId
;
errorCount
=
cacheFactory
.
getExpireStringService
().
getValue
(
redisKey
,
Integer
.
class
);
if
(
errorCount
==
null
)
{
errorCount
=
0
;
}
errorCount
++;
if
(
errorCount
.
intValue
()
>
smsPropertyConfig
.
errorCount
.
intValue
())
{
//超过错误次数之后清理验证码
cacheFactory
.
getExpireStringService
()
.
delete
(
RedisConstant
.
SMS_CAPTCHA_KEY
+
phone
+
"_"
+
tenantNo
+
"_"
+
appId
);
cacheFactory
.
getExpireStringService
().
delete
(
redisKey
);
return
;
}
cacheFactory
.
getExpireStringService
()
.
setExpireValue
(
redisKey
,
errorCount
,
RedisConstant
.
SMS_CAPTCHA_ERROR_COUNT_EXPIRE
);
}
catch
(
CacheException
e
)
{
log
.
error
(
"checkSmsCaptchaErrorCount 检查短信验证码验证错误次数 访问redis异常:"
,
e
);
}
}
@Override
public
void
delSmsCaptchaErrorCount
(
String
phone
,
String
tenantNo
,
String
appId
)
{
try
{
cacheFactory
.
getExpireStringService
()
.
delete
(
RedisConstant
.
SMS_CAPTCHA_ERROR_COUNT_KEY
+
phone
+
"_"
+
tenantNo
+
"_"
+
appId
);
}
catch
(
CacheException
e
)
{
log
.
error
(
"delSmsCaptchaErrorCount 检查短信验证码验证错误次数 访问redis异常:"
,
e
);
}
}
}
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/auth/identification/service/impl/LoginServiceImpl.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.auth.identification.service.impl
;
import
com.alibaba.fastjson.JSONObject
;
import
com.cache.CacheFactory
;
import
com.cache.exception.CacheException
;
import
com.cusc.nirvana.common.result.Response
;
import
com.cusc.nirvana.user.auth.common.constants.RedisConstant
;
import
com.cusc.nirvana.user.auth.common.constants.ResponseCode
;
import
com.cusc.nirvana.user.auth.common.constants.UserTypeEnum
;
import
com.cusc.nirvana.user.auth.common.dto.SmsResponseDTO
;
import
com.cusc.nirvana.user.auth.common.service.AppConfigService
;
import
com.cusc.nirvana.user.auth.identification.dto.CaptchaVerificationReq
;
import
com.cusc.nirvana.user.auth.identification.dto.MobileLoginReq
;
import
com.cusc.nirvana.user.auth.identification.dto.Oauth2Token
;
import
com.cusc.nirvana.user.auth.identification.dto.SmsSendConfig
;
import
com.cusc.nirvana.user.auth.identification.dto.UserNameLoginReq
;
import
com.cusc.nirvana.user.auth.identification.service.ICaptchaService
;
import
com.cusc.nirvana.user.auth.identification.service.ILoginService
;
import
com.cusc.nirvana.user.auth.identification.service.IRandomIdService
;
import
com.cusc.nirvana.user.auth.identification.service.ISmsService
;
import
com.cusc.nirvana.user.auth.identification.service.ITokenService
;
import
com.cusc.nirvana.user.ciam.dto.CiamUserDTO
;
import
com.cusc.nirvana.user.ciam.service.ICiamUserService
;
import
com.cusc.nirvana.user.eiam.constants.CommonStatusEnum
;
import
com.cusc.nirvana.user.eiam.dto.ApplicationDTO
;
import
com.cusc.nirvana.user.eiam.dto.EiamUrlDTO
;
import
com.cusc.nirvana.user.eiam.dto.UserDTO
;
import
com.cusc.nirvana.user.eiam.service.IUrlService
;
import
com.cusc.nirvana.user.eiam.service.IUserService
;
import
com.cusc.nirvana.user.exception.CuscUserException
;
import
com.cusc.nirvana.user.util.CuscRandomUtils
;
import
com.cusc.nirvana.user.util.CuscStringUtils
;
import
com.cusc.nirvana.user.util.crypt.Sm4Util
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
/**
* Description: 登录service实现类
* <br />
* CreateDate 2021-11-02 20:25:49
*
* @author yuyi
**/
@Service
@Slf4j
public
class
LoginServiceImpl
implements
ILoginService
{
@Autowired
private
CacheFactory
cacheFactory
;
@Autowired
private
IUserService
userClient
;
@Autowired
private
IUrlService
eiamUrlClient
;
@Autowired
private
ITokenService
tokenService
;
@Autowired
private
ISmsService
smsService
;
@Autowired
private
ICaptchaService
captchaService
;
@Autowired
private
AppConfigService
appConfigService
;
@Autowired
private
IRandomIdService
randomIdService
;
@Autowired
private
ICiamUserService
ciamUserClient
;
/**
* Description: 手机号登录
* <br />
* CreateDate 2021-11-04 19:53:41
*
* @author yuyi
**/
@Override
public
Response
<
Oauth2Token
>
mobileLogin
(
MobileLoginReq
bean
)
{
//校验短信验证码
Response
ret
=
checkSmsCaptcha
(
bean
);
if
(!
ret
.
isSuccess
())
{
return
ret
;
}
//校验手机号等信息
Response
<
UserDTO
>
retUser
=
checkUserByPhone
(
bean
);
if
(!
retUser
.
isSuccess
())
{
return
Response
.
createError
(
retUser
.
getMsg
(),
retUser
.
getCode
());
}
//将用户对应的url写入redis 异步
eiamUrlClient
.
userRoleResUrlToRedis
(
retUser
.
getData
().
getUuid
(),
bean
.
getTenantNo
(),
bean
.
getApplicationId
());
//创建token
return
tokenService
.
createOauth2TokenByMobile
(
bean
,
retUser
.
getData
());
}
/**
* Description: C端用户手机号登录
* <br />
* CreateDate 2022-04-15 19:53:41
*
* @author huzl
**/
@Override
public
Response
<
Oauth2Token
>
ciamMobileLogin
(
MobileLoginReq
bean
)
{
//校验短信验证码
Response
ret
=
checkSmsCaptcha
(
bean
);
if
(!
ret
.
isSuccess
())
{
return
ret
;
}
//检查手机号是否存在
Response
<
CiamUserDTO
>
userResp
=
checkCiamUserByPhone
(
bean
);
CiamUserDTO
ciamUser
=
userResp
.
getData
();
//创建token
UserDTO
userDTO
=
new
UserDTO
();
userDTO
.
setApplicationId
(
bean
.
getApplicationId
());
userDTO
.
setTenantNo
(
bean
.
getTenantNo
());
userDTO
.
setPhone
(
ciamUser
.
getPhoneNum
());
userDTO
.
setUuid
(
ciamUser
.
getUuid
());
return
tokenService
.
createOauth2TokenByMobile
(
bean
,
userDTO
);
}
/**
* Description: C端用户手机号登录小鹏
* <br />
* CreateDate 2022-04-15 19:53:41
*
* @author huzl
**/
@Override
public
Response
<
Oauth2Token
>
ciamMobileLoginXP
(
MobileLoginReq
bean
)
{
//校验短信验证码
// Response ret = checkSmsCaptcha(bean);
// if (!ret.isSuccess()) {
// return ret;
// }
//检查手机号是否存在
UserDTO
userDTO
=
new
UserDTO
();
CiamUserDTO
ciamUser
=
new
CiamUserDTO
();
ciamUser
.
setPhoneNum
(
bean
.
getPhone
());
ciamUser
.
setTenantNo
(
bean
.
getTenantNo
());
CiamUserDTO
selectCiamUserDTO
=
ciamUserClient
.
getUserByPhoneTenantNo
(
ciamUser
);
System
.
out
.
println
(
JSONObject
.
toJSONString
(
selectCiamUserDTO
));
if
(
null
!=
selectCiamUserDTO
)
{
userDTO
.
setPhone
(
selectCiamUserDTO
.
getPhoneNum
());
userDTO
.
setUuid
(
selectCiamUserDTO
.
getUuid
());
}
else
{
CiamUserDTO
insertBean
=
new
CiamUserDTO
();
insertBean
.
setPhoneNum
(
bean
.
getPhone
());
insertBean
.
setTenantNo
(
bean
.
getTenantNo
());
CiamUserDTO
insertReturnBean
=
ciamUserClient
.
addOrGet
(
insertBean
);
userDTO
.
setPhone
(
insertReturnBean
.
getPhoneNum
());
userDTO
.
setUuid
(
insertReturnBean
.
getUuid
());
}
userDTO
.
setApplicationId
(
bean
.
getApplicationId
());
userDTO
.
setTenantNo
(
bean
.
getTenantNo
());
return
tokenService
.
createOauth2TokenByMobile
(
bean
,
userDTO
);
}
@Override
public
Response
<
Oauth2Token
>
userLoginAdd
(
MobileLoginReq
bean
)
{
//校验短信验证码
Response
ret
=
checkSmsCaptcha
(
bean
);
if
(!
ret
.
isSuccess
())
{
return
ret
;
}
//检查手机号是否存在
Response
<
CiamUserDTO
>
userResp
=
checkCiamUserByPhone
(
bean
);
String
uuid
;
if
(!
userResp
.
isSuccess
())
{
//如果说是已停用的账号直接返回
if
(
ResponseCode
.
LOGIN_NAME_STOP
.
getCode
().
equals
(
userResp
.
getCode
()))
{
return
Response
.
createError
(
userResp
.
getMsg
(),
userResp
.
getCode
());
}
//如果账号不存在,先给账号创建
CiamUserDTO
userDTO
=
new
CiamUserDTO
();
userDTO
.
setPhoneNum
(
bean
.
getPhone
());
userDTO
.
setTenantNo
(
bean
.
getTenantNo
());
CiamUserDTO
ciamUser
=
ciamUserClient
.
addOrGet
(
userDTO
);
if
(
ciamUser
==
null
||
CuscStringUtils
.
isEmpty
(
ciamUser
.
getUuid
()))
{
return
Response
.
createError
(
ResponseCode
.
REGISTER_PHONE_FAIL
.
getMsg
(),
ResponseCode
.
REGISTER_PHONE_FAIL
.
getCode
());
}
uuid
=
ciamUser
.
getUuid
();
}
else
{
if
(
CuscStringUtils
.
isEmpty
(
userResp
.
getData
().
getUuid
()))
{
return
Response
.
createError
(
ResponseCode
.
REGISTER_PHONE_FAIL
.
getMsg
(),
ResponseCode
.
REGISTER_PHONE_FAIL
.
getCode
());
}
uuid
=
userResp
.
getData
().
getUuid
();
}
//创建token
UserDTO
userDTO
=
new
UserDTO
();
userDTO
.
setApplicationId
(
bean
.
getApplicationId
());
userDTO
.
setTenantNo
(
bean
.
getTenantNo
());
userDTO
.
setPhone
(
bean
.
getPhone
());
userDTO
.
setUuid
(
uuid
);
return
tokenService
.
createOauth2TokenByMobile
(
bean
,
userDTO
);
}
@Override
public
Response
<
Boolean
>
checkSmsCaptcha
(
MobileLoginReq
bean
)
{
String
smsCaptcha
;
try
{
String
redisKey
=
RedisConstant
.
SMS_CAPTCHA_KEY
+
bean
.
getPhone
()
+
"_"
+
bean
.
getTenantNo
()
+
"_"
+
bean
.
getApplicationId
();
smsCaptcha
=
cacheFactory
.
getExpireStringService
().
getValue
(
redisKey
,
String
.
class
);
if
(
CuscStringUtils
.
isEmpty
(
smsCaptcha
)
||
!
smsCaptcha
.
equals
(
bean
.
getCaptcha
()))
{
captchaService
.
checkSmsCaptchaErrorCount
(
bean
.
getPhone
(),
bean
.
getTenantNo
(),
bean
.
getApplicationId
());
return
Response
.
createError
(
ResponseCode
.
SMS_CAPTCHA_INVALID
.
getMsg
(),
ResponseCode
.
SMS_CAPTCHA_INVALID
.
getCode
()
+
""
);
}
//验证成功之后清理验证码
cacheFactory
.
getExpireStringService
().
delete
(
redisKey
);
captchaService
.
delSmsCaptchaErrorCount
(
bean
.
getPhone
(),
bean
.
getTenantNo
(),
bean
.
getApplicationId
());
}
catch
(
CacheException
e
)
{
log
.
error
(
"checkSmsCaptcha 获取reids失败 :"
,
e
);
Response
.
createError
(
ResponseCode
.
SMS_GET_CAPTCHA_FAIL
.
getMsg
(),
ResponseCode
.
SMS_GET_CAPTCHA_FAIL
.
getCode
()
+
""
);
}
return
Response
.
createSuccess
(
true
);
}
@Override
public
Response
<
Oauth2Token
>
userNameLogin
(
UserNameLoginReq
bean
)
{
//获取应用配置
ApplicationDTO
appBean
=
appConfigService
.
getAppConfigByCode
(
bean
.
getApplicationId
());
//检查账号是否锁定
checkUserNameLock
(
bean
);
//通过requestId 解密密码
String
respPwd
=
bean
.
getPassword
();
if
(
CuscStringUtils
.
isNotEmpty
(
bean
.
getRequestId
()))
{
String
secretKey
=
randomIdService
.
getByRequestId
(
bean
.
getRequestId
(),
bean
.
getApplicationId
());
if
(
CuscStringUtils
.
isEmpty
(
secretKey
))
{
return
Response
.
createError
(
ResponseCode
.
REQUEST_ID_SECRET_KEY_INVALID
.
getMsg
(),
ResponseCode
.
REQUEST_ID_SECRET_KEY_INVALID
.
getCode
());
}
//删除随机数
randomIdService
.
delRequestIdRedis
(
bean
.
getRequestId
(),
bean
.
getApplicationId
());
respPwd
=
Sm4Util
.
decryptEcbPadding
(
secretKey
,
respPwd
);
}
//通过用户名查询用户信息
Response
<
UserDTO
>
retUser
=
checkUserByUserName
(
bean
);
if
(!
retUser
.
isSuccess
())
{
//增加错误次数
checkPwdFailCount
(
bean
,
appBean
);
return
Response
.
createError
(
retUser
.
getMsg
(),
retUser
.
getCode
());
}
//将输入的密码进行加密
respPwd
=
Sm4Util
.
encryptEcbPadding
(
retUser
.
getData
().
getUuid
().
substring
(
16
),
respPwd
);
//判断密码是否正确
if
(!
respPwd
.
equals
(
retUser
.
getData
().
getPassword
()))
{
//增加错误次数
checkPwdFailCount
(
bean
,
appBean
);
return
Response
.
createError
(
ResponseCode
.
LOGIN_USER_NAME_PASSWORD_INVALID
.
getMsg
(),
ResponseCode
.
LOGIN_USER_NAME_PASSWORD_INVALID
.
getCode
());
}
//将用户对应的url写入redis 异步
EiamUrlDTO
urlDTO
=
new
EiamUrlDTO
();
urlDTO
.
setUserId
(
retUser
.
getData
().
getUuid
());
urlDTO
.
setApplicationId
(
bean
.
getApplicationId
());
urlDTO
.
setTenantNo
(
bean
.
getTenantNo
());
eiamUrlClient
.
userRoleResUrlToRedis
(
retUser
.
getData
().
getUuid
(),
bean
.
getTenantNo
(),
bean
.
getApplicationId
());
//创建token
return
tokenService
.
createOauth2TokenByUserName
(
bean
,
retUser
.
getData
());
}
@Override
public
Response
<
Boolean
>
sendSmsCaptcha
(
MobileLoginReq
bean
)
{
//获取应用配置
ApplicationDTO
appBean
=
appConfigService
.
getAppConfigByCode
(
bean
.
getApplicationId
());
SmsSendConfig
smsConfig
=
bean
.
getSmsSendConfig
();
//短信配置为空,从应用配置中取
smsService
.
convertToSmsConfig
(
appBean
,
smsConfig
);
smsConfig
.
setAppId
(
bean
.
getApplicationId
());
smsConfig
.
setTenantNo
(
bean
.
getTenantNo
());
//判断是否需要验证图形验证码
if
(
bean
.
isCheckCaptchaImg
())
{
CaptchaVerificationReq
cv
=
new
CaptchaVerificationReq
();
cv
.
setRequestId
(
bean
.
getRequestId
());
cv
.
setCaptchaValue
(
bean
.
getCaptchaImage
());
cv
.
setApplicationId
(
bean
.
getApplicationId
());
boolean
checkCaptchaImg
=
captchaService
.
verificationCaptcha
(
cv
);
if
(!
checkCaptchaImg
)
{
return
Response
.
createError
(
ResponseCode
.
CAPTCHA_IMAGGE_CHECK_FAIL
.
getMsg
(),
ResponseCode
.
CAPTCHA_IMAGGE_CHECK_FAIL
.
getCode
());
}
}
String
smsCaptcha
;
try
{
//判断当前验证码是否达到发送间隔
//if (cacheFactory.getExpireStringService().containsKey(
// RedisConstant.SMS_CAPTCHA_SEND_INTERVAL_KEY + bean.getPhone() + "_" + bean.getTenantNo() + "_"
// + bean.getApplicationId())) {
// return Response.createError(ResponseCode.SMS_CAPTCHA_INTERVAL_FAIL.getMsg(),
// ResponseCode.SMS_CAPTCHA_INTERVAL_FAIL.getCode());
//}
smsConfig
.
setTotalLimitKey
(
RedisConstant
.
SMS_CAPTCHA_SEND_TOTAL_KEY
);
smsService
.
checkSmsSendLimit
(
bean
.
getPhone
(),
smsConfig
);
Response
ret
;
//检查手机号是否正确
if
(
bean
.
getUserType
()
==
null
||
UserTypeEnum
.
EIAM
.
getCode
()
==
bean
.
getUserType
().
intValue
())
{
//EIAM用户
ret
=
checkUserByPhone
(
bean
);
}
else
{
//CIAM用户
ret
=
checkCiamUserByPhone
(
bean
);
}
if
(!
ret
.
isSuccess
())
{
return
ret
;
}
//创建随机验证
smsCaptcha
=
CuscRandomUtils
.
randomNumeric
(
6
);
int
captchaExpire
=
RedisConstant
.
SMS_CAPTCHA_EXPIRE
;
if
(
bean
.
getCaptchaExpire
()
!=
null
)
{
captchaExpire
=
bean
.
getCaptchaExpire
();
}
//放到redis
cacheFactory
.
getExpireStringService
()
.
setExpireValue
(
RedisConstant
.
SMS_CAPTCHA_KEY
+
bean
.
getPhone
()
+
"_"
+
bean
.
getTenantNo
()
+
"_"
+
bean
.
getApplicationId
(),
smsCaptcha
,
captchaExpire
);
}
catch
(
CacheException
e
)
{
log
.
error
(
"sendSmsCaptcha 存放reids失败:"
,
e
);
return
Response
.
createError
(
ResponseCode
.
SMS_CREATE_CAPTCHA_FAIL
.
getMsg
(),
ResponseCode
.
SMS_CREATE_CAPTCHA_FAIL
.
getCode
());
}
smsConfig
.
setIntervalLimitKey
(
RedisConstant
.
SMS_CAPTCHA_SEND_INTERVAL_KEY
);
//发送短信
SmsResponseDTO
ret
=
smsService
.
sendSms
(
bean
.
getPhone
(),
smsCaptcha
,
smsConfig
);
if
(
ret
==
null
)
{
return
Response
.
createError
(
ResponseCode
.
SMS_CAPTCHA_SEND_FAIL
.
getMsg
(),
ResponseCode
.
SMS_CAPTCHA_SEND_FAIL
.
getCode
());
}
return
Response
.
createSuccess
(
true
);
}
@Override
public
Response
<
Boolean
>
sendSmsCaptchaNew
(
MobileLoginReq
bean
)
{
//获取应用配置
ApplicationDTO
appBean
=
appConfigService
.
getAppConfigByCode
(
bean
.
getApplicationId
());
SmsSendConfig
smsConfig
=
bean
.
getSmsSendConfig
();
//短信配置为空,从应用配置中取
smsService
.
convertToSmsConfig
(
appBean
,
smsConfig
);
smsConfig
.
setAppId
(
bean
.
getApplicationId
());
smsConfig
.
setTenantNo
(
bean
.
getTenantNo
());
//判断是否需要验证图形验证码
if
(
bean
.
isCheckCaptchaImg
())
{
CaptchaVerificationReq
cv
=
new
CaptchaVerificationReq
();
cv
.
setRequestId
(
bean
.
getRequestId
());
cv
.
setCaptchaValue
(
bean
.
getCaptchaImage
());
cv
.
setApplicationId
(
bean
.
getApplicationId
());
boolean
checkCaptchaImg
=
captchaService
.
verificationCaptcha
(
cv
);
if
(!
checkCaptchaImg
)
{
return
Response
.
createError
(
ResponseCode
.
CAPTCHA_IMAGGE_CHECK_FAIL
.
getMsg
(),
ResponseCode
.
CAPTCHA_IMAGGE_CHECK_FAIL
.
getCode
());
}
}
String
smsCaptcha
;
try
{
//判断当前验证码是否达到发送间隔
if
(
cacheFactory
.
getExpireStringService
().
containsKey
(
RedisConstant
.
SMS_CAPTCHA_SEND_INTERVAL_KEY
+
bean
.
getPhone
()
+
"_"
+
bean
.
getTenantNo
()
+
"_"
+
bean
.
getApplicationId
()))
{
return
Response
.
createError
(
ResponseCode
.
SMS_CAPTCHA_INTERVAL_FAIL
.
getMsg
(),
ResponseCode
.
SMS_CAPTCHA_INTERVAL_FAIL
.
getCode
());
}
smsConfig
.
setTotalLimitKey
(
RedisConstant
.
SMS_CAPTCHA_SEND_TOTAL_KEY
);
smsService
.
checkSmsSendLimit
(
bean
.
getPhone
(),
smsConfig
);
Response
ret
;
//检查手机号是否正确
if
(
bean
.
getUserType
()
==
null
||
UserTypeEnum
.
EIAM
.
getCode
()
==
bean
.
getUserType
().
intValue
())
{
//EIAM用户
ret
=
checkUserByPhone
(
bean
);
}
else
{
//CIAM用户
ret
=
checkCiamUserByPhone
(
bean
);
}
if
(!
ret
.
isSuccess
()
&&
!
StringUtils
.
isEmpty
(
bean
.
getLoginType
())
&&
"allow"
.
equals
(
bean
.
getLoginType
()))
{
ret
.
setSuccess
(
true
);
}
if
(!
ret
.
isSuccess
())
{
return
ret
;
}
//创建随机验证
smsCaptcha
=
CuscRandomUtils
.
randomNumeric
(
6
);
int
captchaExpire
=
RedisConstant
.
SMS_CAPTCHA_EXPIRE
;
if
(
bean
.
getCaptchaExpire
()
!=
null
)
{
captchaExpire
=
bean
.
getCaptchaExpire
();
}
//放到redis
cacheFactory
.
getExpireStringService
()
.
setExpireValue
(
RedisConstant
.
SMS_CAPTCHA_KEY
+
bean
.
getPhone
()
+
"_"
+
bean
.
getTenantNo
()
+
"_"
+
bean
.
getApplicationId
(),
smsCaptcha
,
captchaExpire
);
}
catch
(
CacheException
e
)
{
log
.
error
(
"sendSmsCaptcha 存放reids失败:"
,
e
);
return
Response
.
createError
(
ResponseCode
.
SMS_CREATE_CAPTCHA_FAIL
.
getMsg
(),
ResponseCode
.
SMS_CREATE_CAPTCHA_FAIL
.
getCode
());
}
smsConfig
.
setIntervalLimitKey
(
RedisConstant
.
SMS_CAPTCHA_SEND_INTERVAL_KEY
);
//发送短信
smsService
.
sendSms
(
bean
.
getPhone
(),
smsCaptcha
,
smsConfig
);
return
Response
.
createSuccess
(
true
);
}
//-----------------私有方法区--------------------------------
/**
* Description: 通过手机号检查用户-eiam
* <br />
* CreateDate 2021-11-04 20:26:42
*
* @author yuyi
**/
private
Response
<
UserDTO
>
checkUserByPhone
(
MobileLoginReq
bean
)
{
//检查手机号是否存在
UserDTO
user
=
new
UserDTO
();
user
.
setPhone
(
bean
.
getPhone
());
user
.
setTenantNo
(
bean
.
getTenantNo
());
user
.
setApplicationId
(
bean
.
getApplicationId
());
UserDTO
userResp
=
userClient
.
getUser
(
user
);
if
(
userResp
==
null
||
CuscStringUtils
.
isEmpty
(
userResp
.
getPhone
()))
{
return
Response
.
createError
(
ResponseCode
.
LOGIN_USER_NAME_PASSWORD_INVALID
.
getMsg
(),
ResponseCode
.
LOGIN_USER_NAME_PASSWORD_INVALID
.
getCode
());
}
user
=
userResp
;
//检查状态是否正确
if
(
CommonStatusEnum
.
ENABLE
.
getCode
()
!=
user
.
getStatus
())
{
return
Response
.
createError
(
ResponseCode
.
LOGIN_NAME_STOP
.
getMsg
(),
ResponseCode
.
LOGIN_NAME_STOP
.
getCode
());
}
return
Response
.
createSuccess
(
user
);
}
/**
* Description: 通过手机号检查用户-ciam
* <br />
* CreateDate 2021-11-04 20:26:42
*
* @author yuyi
**/
private
Response
<
CiamUserDTO
>
checkCiamUserByPhone
(
MobileLoginReq
bean
)
{
CiamUserDTO
ciamUser
=
new
CiamUserDTO
();
ciamUser
.
setPhoneNum
(
bean
.
getPhone
());
ciamUser
.
setTenantNo
(
bean
.
getTenantNo
());
//ciamUser.setStatus(CommonStatusEnum.ENABLE.getCode());
CiamUserDTO
userResp
=
ciamUserClient
.
getUserByPhoneTenantNo
(
ciamUser
);
if
(
userResp
==
null
||
CuscStringUtils
.
isEmpty
(
userResp
.
getPhoneNum
()))
{
return
Response
.
createError
(
ResponseCode
.
LOGIN_USER_NAME_PASSWORD_INVALID
.
getMsg
(),
ResponseCode
.
LOGIN_USER_NAME_PASSWORD_INVALID
.
getCode
());
}
ciamUser
=
userResp
;
//检查状态是否正确
if
(
CommonStatusEnum
.
ENABLE
.
getCode
()
!=
ciamUser
.
getStatus
())
{
return
Response
.
createError
(
ResponseCode
.
LOGIN_NAME_STOP
.
getMsg
(),
ResponseCode
.
LOGIN_NAME_STOP
.
getCode
());
}
return
Response
.
createSuccess
(
ciamUser
);
}
/**
* Description: 通过手机号检查用户
* <br />
* CreateDate 2021-11-04 20:26:42
*
* @author yuyi
**/
private
Response
<
UserDTO
>
checkUserByUserName
(
UserNameLoginReq
bean
)
{
//检查手机号是否存在
UserDTO
user
=
new
UserDTO
();
user
.
setUserName
(
bean
.
getUserName
());
user
.
setTenantNo
(
bean
.
getTenantNo
());
user
.
setApplicationId
(
bean
.
getApplicationId
());
UserDTO
userResp
=
userClient
.
getUser
(
user
);
if
(
userResp
==
null
)
{
return
Response
.
createError
(
ResponseCode
.
LOGIN_NAME_INVALID
.
getMsg
(),
ResponseCode
.
LOGIN_NAME_INVALID
.
getCode
());
}
user
=
userResp
;
//检查状态是否正确
if
(
CommonStatusEnum
.
ENABLE
.
getCode
()
!=
user
.
getStatus
())
{
return
Response
.
createError
(
ResponseCode
.
LOGIN_NAME_STOP
.
getMsg
(),
ResponseCode
.
LOGIN_NAME_STOP
.
getCode
());
}
return
Response
.
createSuccess
(
user
);
}
/**
* Description: 保存密码失败次数
* <br />
* CreateDate 2022-02-17 11:03:42
*
* @author yuyi
**/
private
void
checkPwdFailCount
(
UserNameLoginReq
bean
,
ApplicationDTO
appDTO
)
{
//密码错误锁定次数、时间、期限为空或小于0时,不生效
if
(
appDTO
.
getPwsErrorLockNum
()
==
null
||
appDTO
.
getPwsErrorLockNum
()
<=
0
||
appDTO
.
getPwsErrorLockTerm
()
==
null
||
appDTO
.
getPwsErrorLockTerm
()
<=
0
||
appDTO
.
getPwsErrorLockTime
()
==
null
||
appDTO
.
getPwsErrorLockTime
()
<=
0
)
{
return
;
}
try
{
Integer
failCount
=
cacheFactory
.
getExpireStringService
().
getValue
(
RedisConstant
.
USERNAME_PASSWORD_FAIL_COUNT_KEY
+
bean
.
getUserName
()
+
"_"
+
bean
.
getTenantNo
()
+
"_"
+
bean
.
getApplicationId
(),
Integer
.
class
);
int
expireTime
;
if
(
failCount
==
null
||
failCount
==
0
)
{
failCount
=
1
;
expireTime
=
appDTO
.
getPwsErrorLockTerm
();
}
else
{
failCount
++;
expireTime
=
cacheFactory
.
getExpireStringService
().
getKeyExpireTime
(
RedisConstant
.
USERNAME_PASSWORD_FAIL_COUNT_KEY
+
bean
.
getUserName
()
+
"_"
+
bean
.
getTenantNo
()
+
"_"
+
bean
.
getApplicationId
()).
intValue
();
}
cacheFactory
.
getExpireStringService
()
.
setExpireValue
(
RedisConstant
.
USERNAME_PASSWORD_FAIL_COUNT_KEY
+
bean
.
getUserName
()
+
"_"
+
bean
.
getTenantNo
()
+
"_"
+
bean
.
getApplicationId
(),
failCount
,
expireTime
);
if
(
failCount
>=
appDTO
.
getPwsErrorLockNum
())
{
//锁定账号
cacheFactory
.
getExpireStringService
()
.
setExpireValue
(
RedisConstant
.
USERNAME_LOCK_KEY
+
bean
.
getUserName
()
+
"_"
+
bean
.
getTenantNo
()
+
"_"
+
bean
.
getApplicationId
(),
failCount
,
appDTO
.
getPwsErrorLockTime
());
}
}
catch
(
CacheException
e
)
{
log
.
error
(
"保存密码失败次数至redis异常:"
,
e
);
}
}
/**
* Description: 检查账号是否锁定
* <br />
* CreateDate 2022-02-17 11:03:42
*
* @author yuyi
**/
private
void
checkUserNameLock
(
UserNameLoginReq
bean
)
{
boolean
isLock
=
false
;
try
{
isLock
=
cacheFactory
.
getExpireStringService
().
containsKey
(
RedisConstant
.
USERNAME_LOCK_KEY
+
bean
.
getUserName
()
+
"_"
+
bean
.
getTenantNo
()
+
"_"
+
bean
.
getApplicationId
());
}
catch
(
CacheException
e
)
{
log
.
error
(
"检查密码失败次数访问redis异常:"
,
e
);
}
if
(
isLock
)
{
throw
new
CuscUserException
(
ResponseCode
.
USER_NAME_PWD_FAIL_LOCK
.
getCode
(),
ResponseCode
.
USER_NAME_PWD_FAIL_LOCK
.
getMsg
());
}
}
}
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/auth/identification/service/impl/RandomIdServiceImpl.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.auth.identification.service.impl
;
import
com.cache.CacheFactory
;
import
com.cache.exception.CacheException
;
import
com.cusc.nirvana.user.auth.common.constants.RedisConstant
;
import
com.cusc.nirvana.user.auth.identification.service.IRandomIdService
;
import
com.cusc.nirvana.user.util.CuscStringUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
/**
* Description: 随机id或随机数业务层
* <br />
* CreateDate 2022-01-24 10:32:09
*
* @author yuyi
**/
@Service
@Slf4j
public
class
RandomIdServiceImpl
implements
IRandomIdService
{
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
RandomIdServiceImpl
.
class
);
@Autowired
private
CacheFactory
cacheFactory
;
@Override
public
String
getRequestIdToRedis
(
String
applicationId
,
int
expireTime
,
String
content
)
{
String
requestId
=
CuscStringUtils
.
generateUuid
();
//放redis
try
{
cacheFactory
.
getExpireStringService
()
.
setExpireValue
(
RedisConstant
.
RANDOM_REQUEST_ID
+
requestId
+
"_"
+
applicationId
,
content
,
expireTime
);
}
catch
(
CacheException
e
)
{
log
.
error
(
"getRequestIdToRedis 请求reids失败 :"
,
e
);
return
null
;
}
return
requestId
;
}
@Override
public
boolean
existsRequestIdRedis
(
String
requestId
,
String
applicationId
)
{
try
{
return
cacheFactory
.
getExpireStringService
()
.
containsKey
(
RedisConstant
.
RANDOM_REQUEST_ID
+
requestId
+
"_"
+
applicationId
);
}
catch
(
CacheException
e
)
{
log
.
error
(
"getRequestIdToRedis 请求reids失败 :"
,
e
);
}
return
false
;
}
@Override
public
boolean
delRequestIdRedis
(
String
requestId
,
String
applicationId
)
{
try
{
return
cacheFactory
.
getExpireStringService
()
.
delete
(
RedisConstant
.
RANDOM_REQUEST_ID
+
requestId
+
"_"
+
applicationId
);
}
catch
(
CacheException
e
)
{
log
.
error
(
"getRequestIdToRedis 请求reids失败 :"
,
e
);
}
return
false
;
}
@Override
public
String
getByRequestId
(
String
requestId
,
String
applicationId
)
{
try
{
return
cacheFactory
.
getExpireStringService
()
.
getValue
(
RedisConstant
.
RANDOM_REQUEST_ID
+
requestId
+
"_"
+
applicationId
,
String
.
class
);
}
catch
(
CacheException
e
)
{
log
.
error
(
"getByRequestId 请求reids失败 :"
,
e
);
return
null
;
}
}
}
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/auth/identification/service/impl/SmsServiceImpl.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.auth.identification.service.impl
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.TypeReference
;
import
com.cache.CacheFactory
;
import
com.cache.exception.CacheException
;
import
com.cusc.nirvana.common.encrypt.sign.HMAC
;
import
com.cusc.nirvana.common.result.Response
;
import
com.cusc.nirvana.user.auth.common.constants.ResponseCode
;
import
com.cusc.nirvana.user.auth.common.dto.SmsResponseDTO
;
import
com.cusc.nirvana.user.auth.common.dto.SmsSendDTO
;
import
com.cusc.nirvana.user.auth.identification.dto.SmsSendConfig
;
import
com.cusc.nirvana.user.auth.identification.service.ISmsService
;
import
com.cusc.nirvana.user.config.SignConstants
;
import
com.cusc.nirvana.user.config.SmsPropertyConfig
;
import
com.cusc.nirvana.user.eiam.dto.ApplicationDTO
;
import
com.cusc.nirvana.user.exception.CuscUserException
;
import
com.cusc.nirvana.user.util.CuscStringUtils
;
import
com.cusc.nirvana.user.util.DateUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.client.RestTemplate
;
import
java.time.LocalDateTime
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* Description: 短信service
* <br />
* CreateDate 2021-11-02 20:25:49
*
* @author yuyi
**/
@Service
@Slf4j
public
class
SmsServiceImpl
implements
ISmsService
{
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
SmsServiceImpl
.
class
);
@Autowired
private
RestTemplate
restTemplate
;
@Autowired
private
SmsPropertyConfig
smsPropertyConfig
;
@Autowired
private
CacheFactory
cacheFactory
;
@Value
(
"${sms.cusc.strategyCode:}"
)
private
String
strategyCode
;
@Value
(
"${sms.cusc.templateCode:}"
)
private
String
smsTemplate
;
@Override
public
SmsResponseDTO
sendSms
(
String
phone
,
List
<
String
>
paramterList
,
SmsSendConfig
config
)
{
SmsSendDTO
send
=
new
SmsSendDTO
();
if
(
CuscStringUtils
.
isEmpty
(
smsPropertyConfig
.
getAccessKey
()))
{
send
.
setAccesskey
(
config
.
getSmsPlatformKey
());
}
else
{
send
.
setAccesskey
(
smsPropertyConfig
.
getAccessKey
());
}
List
<
String
>
phoneList
=
new
ArrayList
<>();
phoneList
.
add
(
phone
);
send
.
setPhoneNumbers
(
phoneList
);
send
.
setTemplateParams
(
paramterList
);
if
(
CuscStringUtils
.
isEmpty
(
smsPropertyConfig
.
getSignatureCode
()))
{
send
.
setSignatureCode
(
config
.
getSmsSignatureCode
());
}
else
{
send
.
setSignatureCode
(
smsPropertyConfig
.
getSignatureCode
());
}
send
.
setStrategyCode
(
strategyCode
);
send
.
setTemplateCode
(
smsTemplate
);
Response
<
SmsResponseDTO
>
retResp
;
try
{
//判断当前验证码是否达到发送间隔
String
intervalKey
=
config
.
getIntervalLimitKey
()
+
phone
+
"_"
+
config
.
getTenantNo
()
+
"_"
+
config
.
getAppId
();
int
expireLock
=
config
.
getSmsIntervalLimit
()
*
1000
;
log
.
info
(
"sendSms intervalKey : {} , expire : {}"
,
intervalKey
,
expireLock
);
if
(!
cacheFactory
.
getLockService
().
lock
(
intervalKey
,
expireLock
))
{
throw
new
CuscUserException
(
ResponseCode
.
SMS_CAPTCHA_INTERVAL_FAIL
.
getCode
(),
ResponseCode
.
SMS_CAPTCHA_INTERVAL_FAIL
.
getMsg
());
}
HttpEntity
httpEntity
=
new
HttpEntity
(
JSON
.
toJSONString
(
send
),
headers
());
String
url
=
smsPropertyConfig
.
getSmsUrl
()
+
smsPropertyConfig
.
getSendUrl
();
log
.
info
(
"SmsServiceImpl sendSms 短信平台 url : {}, request : {}"
,
url
,
JSON
.
toJSONString
(
httpEntity
));
ResponseEntity
<
String
>
entity
=
restTemplate
.
exchange
(
url
,
HttpMethod
.
POST
,
httpEntity
,
String
.
class
);
retResp
=
JSON
.
parseObject
(
entity
.
getBody
(),
new
TypeReference
<
Response
<
SmsResponseDTO
>>(
SmsResponseDTO
.
class
)
{
}.
getType
());
log
.
info
(
"SmsServiceImpl sendSms 短信平台 url : {}, response : {}"
,
url
,
JSON
.
toJSONString
(
retResp
));
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"短信发送失败: "
,
e
);
throw
new
CuscUserException
(
ResponseCode
.
SMS_CAPTCHA_SEND_FAIL
.
getCode
()
+
""
,
ResponseCode
.
SMS_CAPTCHA_SEND_FAIL
.
getMsg
());
}
//记录短信发送次数和间隔
saveSmsSendLimitToRedis
(
phone
,
config
);
if
(
retResp
!=
null
)
{
return
retResp
.
getData
();
}
return
null
;
}
@Override
public
SmsResponseDTO
sendSms
(
String
phone
,
String
parameter
,
SmsSendConfig
config
)
{
List
<
String
>
list
=
new
ArrayList
<>();
list
.
add
(
parameter
);
return
sendSms
(
phone
,
list
,
config
);
}
@Override
public
boolean
checkSmsConfigNotNull
(
SmsSendConfig
bean
)
{
//return bean != null && CuscStringUtils.isNotEmpty(bean.getSmsTemplateCode());
return
bean
!=
null
;
}
@Override
public
void
convertToSmsConfig
(
ApplicationDTO
fromBean
,
SmsSendConfig
toBean
)
{
//短信配置为空,从应用配置中取
if
(!
checkSmsConfigNotNull
(
toBean
))
{
throw
new
CuscUserException
(
ResponseCode
.
SMS_SEND_CONFIG_NOT_NULL
.
getCode
()
+
""
,
ResponseCode
.
SMS_SEND_CONFIG_NOT_NULL
.
getMsg
());
}
if
(
toBean
.
getSmsTotalLimit
()
==
null
)
{
if
(
fromBean
.
getSmsTotalLimit
()
==
null
)
{
log
.
warn
(
"sms config smsTotalLimit is null"
);
throw
new
CuscUserException
(
ResponseCode
.
SMS_SEND_CONFIG_NOT_NULL
.
getCode
()
+
""
,
ResponseCode
.
SMS_SEND_CONFIG_NOT_NULL
.
getMsg
());
}
toBean
.
setSmsTotalLimit
(
fromBean
.
getSmsTotalLimit
());
}
if
(
toBean
.
getSmsIntervalLimit
()
==
null
)
{
if
(
fromBean
.
getSmsIntervalLimit
()
==
null
)
{
log
.
warn
(
"sms config smsIntervalLimit is null"
);
throw
new
CuscUserException
(
ResponseCode
.
SMS_SEND_CONFIG_NOT_NULL
.
getCode
()
+
""
,
ResponseCode
.
SMS_SEND_CONFIG_NOT_NULL
.
getMsg
());
}
toBean
.
setSmsIntervalLimit
(
fromBean
.
getSmsIntervalLimit
());
}
if
(
toBean
.
getSmsPlatformKey
()
==
null
)
{
if
(
fromBean
.
getSmsPlatformKey
()
==
null
)
{
log
.
warn
(
"sms config smsPlatformKey is null"
);
throw
new
CuscUserException
(
ResponseCode
.
SMS_SEND_CONFIG_NOT_NULL
.
getCode
()
+
""
,
ResponseCode
.
SMS_SEND_CONFIG_NOT_NULL
.
getMsg
());
}
toBean
.
setSmsPlatformKey
(
fromBean
.
getSmsPlatformKey
());
}
if
(
toBean
.
getSmsSignatureCode
()
==
null
)
{
if
(
fromBean
.
getSmsSignatureCode
()
==
null
)
{
log
.
warn
(
"sms config smsSignatureCode is null"
);
throw
new
CuscUserException
(
ResponseCode
.
SMS_SEND_CONFIG_NOT_NULL
.
getCode
()
+
""
,
ResponseCode
.
SMS_SEND_CONFIG_NOT_NULL
.
getMsg
());
}
toBean
.
setSmsSignatureCode
(
fromBean
.
getSmsSignatureCode
());
}
}
/**
* Description: 短信发送限制检查
* <br />
* CreateDate 2022-01-27 14:43:41
*
* @author yuyi
**/
@Override
public
void
checkSmsSendLimit
(
String
phone
,
SmsSendConfig
bean
)
{
try
{
if
(
bean
.
getSmsTotalLimit
()
!=
null
&&
bean
.
getSmsTotalLimit
()
>
0
&&
CuscStringUtils
.
isNotEmpty
(
bean
.
getTotalLimitKey
()))
{
//记录发送总次数限制
Integer
smsTotal
=
cacheFactory
.
getExpireStringService
()
.
getValue
(
bean
.
getTotalLimitKey
()
+
phone
+
"_"
+
bean
.
getTenantNo
()
+
"_"
+
bean
.
getAppId
(),
Integer
.
class
);
if
(
smsTotal
!=
null
&&
smsTotal
>=
bean
.
getSmsTotalLimit
())
{
throw
new
CuscUserException
(
ResponseCode
.
SMS_TOTAL_LIMIT_OVERRUN
.
getCode
(),
ResponseCode
.
SMS_TOTAL_LIMIT_OVERRUN
.
getMsg
());
}
}
if
(
bean
.
getSmsIntervalLimit
()
!=
null
&&
bean
.
getSmsIntervalLimit
()
>
0
&&
CuscStringUtils
.
isNotEmpty
(
bean
.
getIntervalLimitKey
()))
{
//记录发送间隔限制
boolean
isExists
=
cacheFactory
.
getExpireStringService
()
.
containsKey
(
bean
.
getIntervalLimitKey
()
+
phone
+
"_"
+
bean
.
getTenantNo
()
+
"_"
+
bean
.
getAppId
());
if
(
isExists
)
{
throw
new
CuscUserException
(
ResponseCode
.
SMS_INTERVAL_LIMIT_OVERRUN
.
getCode
(),
ResponseCode
.
SMS_INTERVAL_LIMIT_OVERRUN
.
getMsg
());
}
}
}
catch
(
Exception
e
)
{
//只记录,不抛出异常,屏蔽对业务的影响
log
.
error
(
"检查短信发送限制信息时访问redis 异常:"
,
e
);
}
}
//----------------私有方法区域--------------------------
/**
* Description: 保存短信发送限制信息到redis
* <br />
* CreateDate 2022-02-16 09:50:25
*
* @author yuyi
**/
private
void
saveSmsSendLimitToRedis
(
String
phone
,
SmsSendConfig
bean
)
{
try
{
if
(
bean
.
getSmsTotalLimit
()
!=
null
&&
bean
.
getSmsTotalLimit
()
>
0
&&
CuscStringUtils
.
isNotEmpty
(
bean
.
getTotalLimitKey
()))
{
//记录发送总次数限制
Integer
smsTotal
=
cacheFactory
.
getExpireStringService
().
getValue
(
bean
.
getTotalLimitKey
()
+
phone
+
"_"
+
bean
.
getTenantNo
()
+
"_"
+
bean
.
getAppId
(),
Integer
.
class
);
Long
expireTime
;
if
(
smsTotal
==
null
)
{
smsTotal
=
1
;
LocalDateTime
begin
=
LocalDateTime
.
now
();
expireTime
=
DateUtils
.
secondBetween
(
begin
,
DateUtils
.
getDayEnd
(
begin
));
}
else
{
smsTotal
++;
expireTime
=
cacheFactory
.
getExpireStringService
().
getKeyExpireTime
(
bean
.
getTotalLimitKey
()
+
phone
+
"_"
+
bean
.
getTenantNo
()
+
"_"
+
bean
.
getAppId
());
}
cacheFactory
.
getExpireStringService
().
setExpireValue
(
bean
.
getTotalLimitKey
()
+
phone
+
"_"
+
bean
.
getTenantNo
()
+
"_"
+
bean
.
getAppId
(),
smsTotal
,
expireTime
.
intValue
());
}
}
catch
(
CacheException
e
)
{
//只记录,不抛出异常,屏蔽对业务的影响
log
.
error
(
"保存短信发送限制信息到redis 异常:"
,
e
);
}
}
/**
* 生成请求头
*
* @return
*/
public
HttpHeaders
headers
()
{
HttpHeaders
httpHeaders
=
new
HttpHeaders
();
httpHeaders
.
add
(
SignConstants
.
APP_ID
,
smsPropertyConfig
.
getAPPID
());
httpHeaders
.
add
(
SignConstants
.
NONCE_STR
,
CuscStringUtils
.
generateUuid
());
httpHeaders
.
add
(
SignConstants
.
TIMESTAMP
,
String
.
valueOf
(
System
.
currentTimeMillis
()));
httpHeaders
.
add
(
SignConstants
.
VERSION
,
smsPropertyConfig
.
getVERSION
());
httpHeaders
.
setContentType
(
MediaType
.
parseMediaType
(
"application/json; charset=UTF-8"
));
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
SignConstants
.
APP_ID
+
smsPropertyConfig
.
getAPPID
());
sb
.
append
(
SignConstants
.
NONCE_STR
+
httpHeaders
.
get
(
SignConstants
.
NONCE_STR
).
get
(
0
));
sb
.
append
(
SignConstants
.
TIMESTAMP
+
httpHeaders
.
get
(
SignConstants
.
TIMESTAMP
).
get
(
0
));
sb
.
append
(
SignConstants
.
VERSION
+
httpHeaders
.
get
(
SignConstants
.
VERSION
).
get
(
0
));
String
scret
=
HMAC
.
sign
(
sb
.
toString
(),
smsPropertyConfig
.
getAPPSCRET
(),
HMAC
.
Type
.
HmacSHA256
);
httpHeaders
.
add
(
SignConstants
.
SIGN
,
scret
);
return
httpHeaders
;
}
}
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/auth/identification/service/impl/TokenServiceImpl.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.auth.identification.service.impl
;
import
com.cache.CacheFactory
;
import
com.cache.constants.CacheConstants
;
import
com.cache.exception.CacheException
;
import
com.cusc.nirvana.common.result.Response
;
import
com.cusc.nirvana.user.auth.common.constants.AppConfigConstant
;
import
com.cusc.nirvana.user.auth.common.constants.RedisConstant
;
import
com.cusc.nirvana.user.auth.common.constants.ResponseCode
;
import
com.cusc.nirvana.user.auth.common.dto.AccessTokenHashDTO
;
import
com.cusc.nirvana.user.auth.common.dto.LogoutDTO
;
import
com.cusc.nirvana.user.auth.common.dto.RefreshTokenHashDTO
;
import
com.cusc.nirvana.user.auth.common.dto.UserTokenListDTO
;
import
com.cusc.nirvana.user.auth.common.service.AppConfigService
;
import
com.cusc.nirvana.user.auth.identification.dto.MobileLoginReq
;
import
com.cusc.nirvana.user.auth.identification.dto.Oauth2Token
;
import
com.cusc.nirvana.user.auth.identification.dto.UserLoginResp
;
import
com.cusc.nirvana.user.auth.identification.dto.UserNameLoginReq
;
import
com.cusc.nirvana.user.auth.identification.service.ITokenService
;
import
com.cusc.nirvana.user.eiam.dto.ApplicationDTO
;
import
com.cusc.nirvana.user.eiam.dto.UserDTO
;
import
com.cusc.nirvana.user.eiam.dto.UserOrganDTO
;
import
com.cusc.nirvana.user.eiam.service.IUserOrganService
;
import
com.cusc.nirvana.user.util.CuscStringUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.util.CollectionUtils
;
import
org.springframework.util.StringUtils
;
import
javax.annotation.Resource
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.UUID
;
/**
* Description: 令牌service实现类
* <br />
* CreateDate 2021-11-02 20:25:49
*
* @author yuyi
**/
@Service
@Slf4j
public
class
TokenServiceImpl
implements
ITokenService
{
@Autowired
private
CacheFactory
cacheFactory
;
@Autowired
private
AppConfigService
appConfigService
;
@Resource
private
IUserOrganService
userOrganService
;
/**
* Description: 创建Oauth2Token
* <br />
* CreateDate 2021-11-04 19:53:41
*
* @author yuyi
**/
@Override
public
Response
<
Oauth2Token
>
createOauth2TokenByMobile
(
MobileLoginReq
bean
,
UserDTO
user
)
{
//创建用户信息
UserLoginResp
userLogin
=
new
UserLoginResp
();
userLogin
.
setUserId
(
user
.
getUuid
());
userLogin
.
setLoginName
(
bean
.
getPhone
().
substring
(
0
,
3
)
+
"****"
+
bean
.
getPhone
().
substring
(
bean
.
getPhone
().
length
()
-
4
));
return
createOauth2Token
(
userLogin
,
bean
.
getApplicationId
(),
user
.
getTenantNo
());
}
/**
* Description: 创建Oauth2Token小鹏
* <br />
* CreateDate 2021-11-04 19:53:41
*
* @author yuyi
**/
@Override
public
Response
<
Oauth2Token
>
createOauth2TokenByMobileXP
(
MobileLoginReq
bean
,
UserDTO
user
)
{
//创建用户信息
UserLoginResp
userLogin
=
new
UserLoginResp
();
userLogin
.
setUserId
(
UUID
.
randomUUID
().
toString
().
replace
(
"-"
,
""
));
userLogin
.
setLoginName
(
bean
.
getPhone
().
substring
(
0
,
3
)
+
"****"
+
bean
.
getPhone
().
substring
(
bean
.
getPhone
().
length
()
-
4
));
return
createOauth2Token
(
userLogin
,
bean
.
getApplicationId
(),
user
.
getTenantNo
());
}
@Override
public
Response
<
Oauth2Token
>
createOauth2TokenByUserName
(
UserNameLoginReq
bean
,
UserDTO
user
)
{
//创建用户信息
UserLoginResp
userLogin
=
new
UserLoginResp
();
userLogin
.
setUserId
(
user
.
getUuid
());
userLogin
.
setLoginName
(
bean
.
getUserName
());
return
createOauth2Token
(
userLogin
,
bean
.
getApplicationId
(),
user
.
getTenantNo
());
}
/**
* Description: 退出
* <br />
* CreateDate 2021-11-09 16:09:36
*
* @author yuyi
**/
@Override
public
Response
logout
(
LogoutDTO
logoutDTO
)
{
String
accessToken
=
logoutDTO
.
getAccessToken
();
AccessTokenHashDTO
accessTokenHashDTO
;
try
{
accessTokenHashDTO
=
cacheFactory
.
getExpireHashService
()
.
getHash
(
RedisConstant
.
TOKEN_ACCESS_TOKEN_INFO
+
accessToken
,
AccessTokenHashDTO
.
class
);
if
(
accessTokenHashDTO
!=
null
)
{
//删除刷新token
cacheFactory
.
getExpireHashService
()
.
delete
(
RedisConstant
.
TOKEN_REFRESH_TOKEN_INFO
+
accessTokenHashDTO
.
getRefresh
());
String
userTokenListKey
=
RedisConstant
.
TOKEN_USER_TOKEN_INFO
+
accessTokenHashDTO
.
getTenantNo
()
+
":"
+
accessTokenHashDTO
.
getUserId
();
//删除用户对应的token
List
<
UserTokenListDTO
>
userList
=
cacheFactory
.
getExpireListService
().
getList
(
userTokenListKey
,
UserTokenListDTO
.
class
);
userList
=
deleteListContent
(
userList
,
accessToken
);
//删除用户token集合
cacheFactory
.
getListService
().
delete
(
userTokenListKey
);
if
(!
CollectionUtils
.
isEmpty
(
userList
))
{
int
userTokenInfoTtl
=
Math
.
toIntExact
(
cacheFactory
.
getListService
().
getKeyExpireTime
(
userTokenListKey
));
cacheFactory
.
getExpireListService
().
setExpireList
(
userTokenListKey
,
userList
,
userTokenInfoTtl
);
}
//删除访问token
cacheFactory
.
getExpireHashService
()
.
delete
(
RedisConstant
.
TOKEN_ACCESS_TOKEN_INFO
+
accessToken
);
}
}
catch
(
CacheException
e
)
{
log
.
error
(
"logout 访问reids失败 :{}"
,
e
);
return
Response
.
createError
(
ResponseCode
.
LOGOUT_FAIL
.
getMsg
(),
ResponseCode
.
LOGOUT_FAIL
.
getCode
()
+
""
);
}
return
Response
.
createSuccess
();
}
@Override
public
Response
kickOutByUserId
(
String
userId
,
String
tenantNo
,
String
appId
)
{
//通过用户id和应用id找到对应的token信息
try
{
String
userTokenListKey
=
RedisConstant
.
TOKEN_USER_TOKEN_INFO
+
tenantNo
+
":"
+
userId
;
List
<
UserTokenListDTO
>
userList
=
cacheFactory
.
getExpireListService
().
getList
(
userTokenListKey
,
UserTokenListDTO
.
class
);
if
(
CollectionUtils
.
isEmpty
(
userList
))
{
return
Response
.
createSuccess
();
}
List
<
UserTokenListDTO
>
userListNew
=
new
ArrayList
<>();
for
(
UserTokenListDTO
userToken
:
userList
)
{
if
(!
userToken
.
getAppId
().
equals
(
appId
))
{
userListNew
.
add
(
userToken
);
continue
;
}
//删除刷新token
cacheFactory
.
getExpireHashService
()
.
delete
(
RedisConstant
.
TOKEN_REFRESH_TOKEN_INFO
+
userToken
.
getRefresh
());
//删除访问token
cacheFactory
.
getExpireHashService
()
.
delete
(
RedisConstant
.
TOKEN_ACCESS_TOKEN_INFO
+
userToken
.
getAccess
());
}
if
(!
CollectionUtils
.
isEmpty
(
userListNew
))
{
int
userTokenInfoTtl
=
Math
.
toIntExact
(
cacheFactory
.
getListService
().
getKeyExpireTime
(
userTokenListKey
));
cacheFactory
.
getExpireListService
().
delete
(
userTokenListKey
);
cacheFactory
.
getExpireListService
().
setExpireList
(
userTokenListKey
,
userListNew
,
userTokenInfoTtl
);
}
}
catch
(
CacheException
e
)
{
log
.
error
(
"kickOutByUserId 访问reids失败 :{}"
,
e
);
return
Response
.
createError
(
ResponseCode
.
KICK_OUT_FAIL
.
getMsg
(),
ResponseCode
.
KICK_OUT_FAIL
.
getCode
()
+
""
);
}
return
Response
.
createSuccess
();
}
/**
* Description: access token续期
* <br />
* CreateDate 2022-01-10 15:24:51
*
* @author yuyi
**/
@Override
public
Response
tokenRenewal
(
String
accessToken
,
String
appId
)
{
ApplicationDTO
appBean
=
appConfigService
.
getAppConfigByCode
(
appId
);
try
{
cacheFactory
.
getExpireHashService
()
.
expireKey
(
RedisConstant
.
TOKEN_REFRESH_TOKEN_INFO
+
accessToken
,
appBean
.
getRenewalTokenTime
(),
CacheConstants
.
TimeType
.
EX
);
//同时续期用户id对应的token信息
AccessTokenHashDTO
accessTokenHashDTO
=
cacheFactory
.
getExpireHashService
()
.
getHash
(
RedisConstant
.
TOKEN_ACCESS_TOKEN_INFO
+
accessToken
,
AccessTokenHashDTO
.
class
);
if
(
accessTokenHashDTO
!=
null
)
{
cacheFactory
.
getExpireListService
()
.
updateExpire
(
RedisConstant
.
TOKEN_USER_TOKEN_INFO
+
accessTokenHashDTO
.
getTenantNo
()
+
":"
+
accessTokenHashDTO
.
getUserId
(),
appBean
.
getRenewalTokenTime
());
}
}
catch
(
CacheException
e
)
{
log
.
error
(
"tokenRenewal 访问reids失败 :{}"
,
e
);
return
Response
.
createError
();
}
return
Response
.
createSuccess
();
}
//--------------------------私有方法区--------------------------------
/**
* Description: 检查已过期的key
* <br />
* CreateDate 2021-11-05 12:50:00
*
* @author yuyi
**/
private
List
<
UserTokenListDTO
>
checkExpireContent
(
List
<
UserTokenListDTO
>
userList
)
{
if
(!
CollectionUtils
.
isEmpty
(
userList
))
{
List
<
UserTokenListDTO
>
ret
=
new
ArrayList
<>();
for
(
UserTokenListDTO
userListDTO
:
userList
)
{
//只需要检查refresh token是否已过期,过期则删除
try
{
Long
expireIn
=
cacheFactory
.
getExpireHashService
()
.
getKeyExpireTime
(
RedisConstant
.
TOKEN_REFRESH_TOKEN_INFO
+
userListDTO
.
getRefresh
());
if
(
expireIn
>
0
)
{
ret
.
add
(
userListDTO
);
}
}
catch
(
CacheException
e
)
{
log
.
error
(
"checkExpireContent 获取reids失败 :{}"
,
e
);
ret
.
add
(
userListDTO
);
}
}
return
ret
;
}
return
userList
;
}
/**
* Description: 删除list中的元素
* <br />
* CreateDate 2021-11-05 12:50:00
*
* @author yuyi
**/
private
List
<
UserTokenListDTO
>
deleteListContent
(
List
<
UserTokenListDTO
>
userList
,
String
accessToken
)
{
if
(!
CollectionUtils
.
isEmpty
(
userList
))
{
List
<
UserTokenListDTO
>
ret
=
new
ArrayList
<>();
for
(
UserTokenListDTO
userListDTO
:
userList
)
{
if
(!
accessToken
.
equals
(
userListDTO
.
getAccess
()))
{
ret
.
add
(
userListDTO
);
}
}
return
ret
;
}
return
userList
;
}
/**
* Description: 创建Oauth2 Token信息
* <br />
* CreateDate 2022-01-10 15:29:37
*
* @author yuyi
**/
private
Response
<
Oauth2Token
>
createOauth2Token
(
UserLoginResp
userLogin
,
String
appId
,
String
tenantNo
)
{
String
userId
=
userLogin
.
getUserId
();
if
(
StringUtils
.
isEmpty
(
userId
)){
return
Response
.
createError
(
ResponseCode
.
TOEKN_CREATE_FAIL
.
getMsg
()+
",userId为空"
,
ResponseCode
.
TOEKN_CREATE_FAIL
.
getCode
()
+
""
);
}
Oauth2Token
oauth2Token
=
new
Oauth2Token
();
//创建token
oauth2Token
.
setAccess_token
(
CuscStringUtils
.
generateUuid
());
oauth2Token
.
setRefresh_token
(
CuscStringUtils
.
generateUuid
());
oauth2Token
.
setToken_type
(
"bearer"
);
oauth2Token
.
setScope
(
"ALL"
);
//读取应用配置信息
ApplicationDTO
appBean
=
appConfigService
.
getAppConfigByCode
(
appId
);
//设置token失效时间
oauth2Token
.
setExpires_in
(
appBean
.
getAccessTokenTerm
());
if
(
appBean
.
getIsDeviceLogin
()
!=
null
&&
AppConfigConstant
.
IS_DEVICE_LOGIN_1
==
appBean
.
getIsDeviceLogin
())
{
//单设备登录
kickOutByUserId
(
userId
,
tenantNo
,
appId
);
}
//创建用户信息
oauth2Token
.
setInfo
(
userLogin
);
//存放redis
try
{
String
organId
=
""
;
//用户对应的组织id
UserOrganDTO
userOrgDto
=
new
UserOrganDTO
();
userOrgDto
.
setUserId
(
userId
);
userOrgDto
.
setTenantNo
(
tenantNo
);
List
<
UserOrganDTO
>
userOrganList
=
userOrganService
.
queryByList
(
userOrgDto
);
if
(!
CollectionUtils
.
isEmpty
(
userOrganList
))
{
organId
=
userOrganList
.
stream
().
filter
(
userOrganDTO
->
userId
.
equals
(
userOrganDTO
.
getUserId
()))
.
findFirst
().
map
(
UserOrganDTO:
:
getOrganId
).
orElse
(
""
);
}
//access_token对应的用户id、refresh_token、scope map格式
AccessTokenHashDTO
accessTokenHashDTO
=
new
AccessTokenHashDTO
();
accessTokenHashDTO
.
setUserId
(
userId
);
accessTokenHashDTO
.
setRefresh
(
oauth2Token
.
getRefresh_token
());
accessTokenHashDTO
.
setScope
(
oauth2Token
.
getScope
());
accessTokenHashDTO
.
setAppId
(
appId
);
accessTokenHashDTO
.
setTenantNo
(
tenantNo
);
accessTokenHashDTO
.
setOrganId
(
organId
);
cacheFactory
.
getExpireHashService
()
.
setExpireHash
(
RedisConstant
.
TOKEN_ACCESS_TOKEN_INFO
+
oauth2Token
.
getAccess_token
(),
accessTokenHashDTO
,
appBean
.
getAccessTokenTerm
());
//refresh_token对应的token和用户id
RefreshTokenHashDTO
refreshTokenHashDTO
=
new
RefreshTokenHashDTO
();
refreshTokenHashDTO
.
setAccess
(
oauth2Token
.
getAccess_token
());
refreshTokenHashDTO
.
setUserId
(
userId
);
refreshTokenHashDTO
.
setScope
(
oauth2Token
.
getScope
());
refreshTokenHashDTO
.
setAppId
(
appId
);
refreshTokenHashDTO
.
setTenantNo
(
tenantNo
);
cacheFactory
.
getExpireHashService
()
.
setExpireHash
(
RedisConstant
.
TOKEN_REFRESH_TOKEN_INFO
+
oauth2Token
.
getRefresh_token
(),
refreshTokenHashDTO
,
appBean
.
getRefreshTokenTerm
());
//用户id对应的access_token、refresh_token
String
userTokenListKey
=
RedisConstant
.
TOKEN_USER_TOKEN_INFO
+
tenantNo
+
":"
+
userId
;
List
<
UserTokenListDTO
>
userList
=
cacheFactory
.
getExpireListService
()
.
getList
(
userTokenListKey
,
UserTokenListDTO
.
class
);
if
(
CollectionUtils
.
isEmpty
(
userList
))
{
userList
=
new
ArrayList
<>();
}
//检查现有list中是否有已过期的key,过期则删除
userList
=
checkExpireContent
(
userList
);
UserTokenListDTO
userListDTO
=
new
UserTokenListDTO
();
userListDTO
.
setAccess
(
oauth2Token
.
getAccess_token
());
userListDTO
.
setRefresh
(
oauth2Token
.
getRefresh_token
());
userListDTO
.
setAppId
(
appId
);
userList
.
add
(
userListDTO
);
//先删除后新增
cacheFactory
.
getExpireListService
().
delete
(
userTokenListKey
);
cacheFactory
.
getExpireListService
().
setExpireList
(
userTokenListKey
,
userList
,
appBean
.
getAccessTokenTerm
());
}
catch
(
CacheException
e
)
{
log
.
error
(
"createOauth2Token 存放reids失败 :{}"
,
e
);
return
Response
.
createError
(
ResponseCode
.
TOEKN_CREATE_FAIL
.
getMsg
(),
ResponseCode
.
TOEKN_CREATE_FAIL
.
getCode
()
+
""
);
}
return
Response
.
createSuccess
(
oauth2Token
);
}
/**
* Description: 创建Oauth2 Token信息小鹏
* <br />
* CreateDate 2022-01-10 15:29:37
*
* @author yuyi
**/
private
Response
<
Oauth2Token
>
createOauth2TokenXP
(
UserLoginResp
userLogin
,
String
appId
,
String
tenantNo
)
{
String
userId
=
userLogin
.
getUserId
();
Oauth2Token
oauth2Token
=
new
Oauth2Token
();
//创建token
oauth2Token
.
setAccess_token
(
CuscStringUtils
.
generateUuid
());
oauth2Token
.
setRefresh_token
(
CuscStringUtils
.
generateUuid
());
oauth2Token
.
setToken_type
(
"bearer"
);
oauth2Token
.
setScope
(
"ALL"
);
//读取应用配置信息
ApplicationDTO
appBean
=
appConfigService
.
getAppConfigByCode
(
appId
);
//设置token失效时间
oauth2Token
.
setExpires_in
(
appBean
.
getAccessTokenTerm
());
if
(
appBean
.
getIsDeviceLogin
()
!=
null
&&
AppConfigConstant
.
IS_DEVICE_LOGIN_1
==
appBean
.
getIsDeviceLogin
())
{
//单设备登录
kickOutByUserId
(
userId
,
tenantNo
,
appId
);
}
//创建用户信息
oauth2Token
.
setInfo
(
userLogin
);
//存放redis
try
{
//access_token对应的用户id、refresh_token、scope map格式
AccessTokenHashDTO
accessTokenHashDTO
=
new
AccessTokenHashDTO
();
accessTokenHashDTO
.
setUserId
(
userId
);
accessTokenHashDTO
.
setRefresh
(
oauth2Token
.
getRefresh_token
());
accessTokenHashDTO
.
setScope
(
oauth2Token
.
getScope
());
accessTokenHashDTO
.
setAppId
(
appId
);
accessTokenHashDTO
.
setTenantNo
(
tenantNo
);
cacheFactory
.
getExpireHashService
()
.
setExpireHash
(
RedisConstant
.
TOKEN_ACCESS_TOKEN_INFO
+
oauth2Token
.
getAccess_token
(),
accessTokenHashDTO
,
appBean
.
getAccessTokenTerm
());
//refresh_token对应的token和用户id
RefreshTokenHashDTO
refreshTokenHashDTO
=
new
RefreshTokenHashDTO
();
refreshTokenHashDTO
.
setAccess
(
oauth2Token
.
getAccess_token
());
refreshTokenHashDTO
.
setUserId
(
userId
);
refreshTokenHashDTO
.
setScope
(
oauth2Token
.
getScope
());
refreshTokenHashDTO
.
setAppId
(
appId
);
refreshTokenHashDTO
.
setTenantNo
(
tenantNo
);
cacheFactory
.
getExpireHashService
()
.
setExpireHash
(
RedisConstant
.
TOKEN_REFRESH_TOKEN_INFO
+
oauth2Token
.
getRefresh_token
(),
refreshTokenHashDTO
,
appBean
.
getRefreshTokenTerm
());
//用户id对应的access_token、refresh_token
String
userTokenListKey
=
RedisConstant
.
TOKEN_USER_TOKEN_INFO
+
tenantNo
+
":"
+
userId
;
List
<
UserTokenListDTO
>
userList
=
cacheFactory
.
getExpireListService
()
.
getList
(
userTokenListKey
,
UserTokenListDTO
.
class
);
if
(
CollectionUtils
.
isEmpty
(
userList
))
{
userList
=
new
ArrayList
<>();
}
//检查现有list中是否有已过期的key,过期则删除
userList
=
checkExpireContent
(
userList
);
UserTokenListDTO
userListDTO
=
new
UserTokenListDTO
();
userListDTO
.
setAccess
(
oauth2Token
.
getAccess_token
());
userListDTO
.
setRefresh
(
oauth2Token
.
getRefresh_token
());
userListDTO
.
setAppId
(
appId
);
userList
.
add
(
userListDTO
);
//先删除后新增
cacheFactory
.
getExpireListService
().
delete
(
userTokenListKey
);
cacheFactory
.
getExpireListService
().
setExpireList
(
userTokenListKey
,
userList
,
appBean
.
getAccessTokenTerm
());
}
catch
(
CacheException
e
)
{
log
.
error
(
"createOauth2Token 存放reids失败 :{}"
,
e
);
return
Response
.
createError
(
ResponseCode
.
TOEKN_CREATE_FAIL
.
getMsg
(),
ResponseCode
.
TOEKN_CREATE_FAIL
.
getCode
()
+
""
);
}
return
Response
.
createSuccess
(
oauth2Token
);
}
}
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/auth/identification/util/CommonParamterCheck.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.auth.identification.util
;
import
com.cusc.nirvana.user.auth.common.constants.ResponseCode
;
import
com.cusc.nirvana.user.exception.CuscUserException
;
import
com.cusc.nirvana.user.util.CuscStringUtils
;
/**
* Description: 参数校验
* <br />
* CreateDate 2022-02-17 10:52
*
* @author yuy336
**/
public
class
CommonParamterCheck
{
/**
* Description: 检查应用和租户是否为空
* <br />
* CreateDate 2022-02-17 10:55:16
*
* @author yuyi
**/
public
static
void
appIdAndTenantNoRequired
(
String
applicationId
,
String
tenantNo
)
{
if
(
CuscStringUtils
.
isEmpty
(
applicationId
)
||
CuscStringUtils
.
isEmpty
(
tenantNo
))
{
throw
new
CuscUserException
(
ResponseCode
.
APPLICATION_TENANT_REQUIRED
.
getCode
(),
ResponseCode
.
APPLICATION_TENANT_REQUIRED
.
getMsg
());
}
}
}
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/ciam/common/BaseIamPO.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.ciam.common
;
import
com.baomidou.mybatisplus.annotation.FieldStrategy
;
import
com.baomidou.mybatisplus.annotation.IdType
;
import
com.baomidou.mybatisplus.annotation.TableField
;
import
com.baomidou.mybatisplus.annotation.TableId
;
import
java.io.*
;
import
java.util.Date
;
/**
* <p>
* iam的DO基类
* </p>
*
* @author yuyi
* @since 2021-10-21
*/
public
class
BaseIamPO
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
/**
* 主键
*/
@TableId
(
value
=
"id"
,
type
=
IdType
.
AUTO
)
private
Long
id
;
/**
* 逻辑删除(0-未删除,1-已删除)
*/
@TableField
(
"is_delete"
)
private
Integer
isDelete
;
/**
* 创建时间
*/
@TableField
(
value
=
"create_time"
,
insertStrategy
=
FieldStrategy
.
NEVER
,
updateStrategy
=
FieldStrategy
.
NEVER
)
private
Date
createTime
;
/**
* 更新时间
*/
@TableField
(
value
=
"update_time"
,
insertStrategy
=
FieldStrategy
.
NEVER
,
updateStrategy
=
FieldStrategy
.
NEVER
)
private
Date
updateTime
;
/**
* 创建人
*/
@TableField
(
value
=
"creator"
,
updateStrategy
=
FieldStrategy
.
NEVER
)
private
String
creator
;
public
Long
getId
()
{
return
id
;
}
public
void
setId
(
Long
id
)
{
this
.
id
=
id
;
}
public
Integer
getIsDelete
()
{
return
isDelete
;
}
public
void
setIsDelete
(
Integer
isDelete
)
{
this
.
isDelete
=
isDelete
;
}
public
Date
getCreateTime
()
{
return
createTime
;
}
public
void
setCreateTime
(
Date
createTime
)
{
this
.
createTime
=
createTime
;
}
public
Date
getUpdateTime
()
{
return
updateTime
;
}
public
void
setUpdateTime
(
Date
updateTime
)
{
this
.
updateTime
=
updateTime
;
}
public
String
getCreator
()
{
return
creator
;
}
public
void
setCreator
(
String
creator
)
{
this
.
creator
=
creator
;
}
@Override
public
String
toString
()
{
return
"BaseIamDO{"
+
"id="
+
id
+
", isDelete="
+
isDelete
+
", createTime="
+
createTime
+
", updateTime="
+
updateTime
+
", creator='"
+
creator
+
'\''
+
'}'
;
}
}
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/ciam/constants/CiamConstant.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.ciam.constants
;
/**
* <p>
* ciam es常量类
* </p>
*
* @author yuy336
* @since 2021-10-14
*/
public
class
CiamConstant
{
/**
* 用户实名信息索引名称
*/
public
final
static
String
INDEX_CIAM_USER_RNR_INFO
=
"user_iam.ciam_user_rnr_info"
;
/**
* 用户信息索引名称
*/
public
final
static
String
INDEX_CIAM_USER
=
"user_iam.ciam_user"
;
/**
* ciam路由键key
*/
public
final
static
String
CIAM_ROUTING_KEY
=
"CT:USER:CIAM:ROUTING_KEY"
;
}
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/ciam/constants/CiamMqConstant.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.ciam.constants
;
/**
* <p>
* ciam es常量类
* </p>
*
* @author yuy336
* @since 2021-10-14
*/
public
class
CiamMqConstant
{
/**
* 实名修改手机号topic
*/
public
final
static
String
RNR_TO_CIAM_CHANGE_PHONE_TOPIC
=
"rnr_change_topic"
;
/**
* 实名修改手机号group
*/
public
final
static
String
RNR_TO_CIAM_CHANGE_PHONE_GROUP
=
"rnr_change_ciam_group"
;
/**
* 实名修改手机号expression
*/
public
final
static
String
RNR_TO_CIAM_CHANGE_PHONE_EXPRESSION
=
"change_rnr_info"
;
}
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/ciam/constants/ResponseCode.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.ciam.constants
;
public
enum
ResponseCode
{
USER_SUBJECT_INVALID
(
1701
,
""
),
CIAM_ROUTING_KEY_FAIL
(
1701
,
"获取路由键失败"
),
COMPANY_UUID_INVALID
(
1702
,
"项目关联的公司信息无效"
),
USER_INVALID
(
1703
,
"用户信息无效"
),
SUBJECT_CUSTOMER_INFO_INVALID
(
1704
,
"主体实名信息无效"
),
USER_PHONE_REPEAT
(
1705
,
"用户手机号重复"
),
SUBJECT_CUSTOMER_INFO_NOT_FOUND
(
1706
,
"未找到主体实名信息"
),
ENCRYPT_FAIL
(
1707
,
"加密失败"
),
DECRYPT_FAIL
(
1708
,
"解密失败"
),
REPEAT_REQUEST_FAIL
(
1709
,
"请勿重复提交"
),
INVALID_DATA
(
1001
,
"数据校验不通过"
),
SYS_BUSY
(
1002
,
"服务调用失败"
),
SERVICE_NOT_FOUND
(
1003
,
"服务不存在"
),
TP_SYS_BUSY
(
1004
,
"第三方服务调用失败"
),
JSON_FORMAT_ERROR
(
1005
,
"参数格式错误"
),
REQ_TOO_MANY_TIMES
(
1007
,
"请求过于频繁,请稍后再试!"
),
NO_DATA_AUTH
(
1008
,
"服务,请稍后再试!"
),
PARAMETER_NOT_NULL
(
1009
,
"参数不能为空!"
),
;
private
Integer
code
;
private
String
msg
;
ResponseCode
(
Integer
code
,
String
msg
)
{
this
.
code
=
code
;
this
.
msg
=
msg
;
}
public
Integer
getCode
()
{
return
code
;
}
public
void
setCode
(
Integer
code
)
{
this
.
code
=
code
;
}
public
String
getMsg
()
{
return
msg
;
}
public
void
setMsg
(
String
msg
)
{
this
.
msg
=
msg
;
}
}
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/ciam/controller/CiamUserController.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.ciam.controller
;
import
com.alibaba.fastjson.JSONObject
;
import
com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
;
import
com.baomidou.mybatisplus.extension.plugins.pagination.Page
;
import
com.cusc.nirvana.common.result.BeanUtilsHelper
;
import
com.cusc.nirvana.common.result.PageResult
;
import
com.cusc.nirvana.common.result.Response
;
import
com.cusc.nirvana.rds.mybatis.PageHelper
;
import
com.cusc.nirvana.user.ciam.constants.ResponseCode
;
import
com.cusc.nirvana.user.ciam.dao.entity.CiamUserPO
;
import
com.cusc.nirvana.user.ciam.dto.CiamUserDTO
;
import
com.cusc.nirvana.user.ciam.service.ICiamUserService
;
import
com.cusc.nirvana.user.util.CuscStringUtils
;
import
io.swagger.annotations.ApiOperation
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.BeanUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
/**
* <p>
* 终端用户 前端控制器
* </p>
*
* @author auto-generator
* @since 2021-10-14
*/
@Slf4j
@RestController
@RequestMapping
(
"/user/ciam"
)
public
class
CiamUserController
{
@Autowired
ICiamUserService
userService
;
@PostMapping
(
"/add"
)
@ApiOperation
(
value
=
"新增(add)"
,
notes
=
"测试demo"
)
public
Response
add
(
CiamUserDTO
entity
)
{
try
{
CiamUserPO
entity0
=
new
CiamUserPO
();
BeanUtils
.
copyProperties
(
entity
,
entity0
);
return
Response
.
createSuccess
(
userService
.
save
(
entity0
));
}
catch
(
Exception
e
)
{
log
.
error
(
"测试demo,新增方法执行出错,错误信息为:{}"
,
e
);
return
Response
.
createError
(
"服务忙"
,
Collections
.
emptyList
());
}
}
@PostMapping
(
"/update"
)
@ApiOperation
(
value
=
"更新(update)"
,
notes
=
"测试demo"
)
public
Response
update
(
CiamUserDTO
entity
)
{
try
{
CiamUserPO
entity0
=
new
CiamUserPO
();
BeanUtils
.
copyProperties
(
entity
,
entity0
);
return
Response
.
createSuccess
(
userService
.
updateById
(
entity0
));
}
catch
(
Exception
e
)
{
log
.
error
(
"测试demo,更新方法执行出错,错误信息为:{}"
,
e
);
return
Response
.
createError
(
"服务忙"
,
Collections
.
emptyList
());
}
}
@PostMapping
(
"/query"
)
@ApiOperation
(
value
=
"查询(query)"
,
notes
=
"测试demo"
)
public
Response
<
List
<
CiamUserDTO
>>
query
()
{
try
{
QueryWrapper
queryWrapper
=
new
QueryWrapper
();
queryWrapper
.
orderByDesc
(
"u_time2"
);
queryWrapper
.
eq
(
"del2"
,
0
);
List
<
CiamUserPO
>
list
=
userService
.
list
(
queryWrapper
);
List
<
CiamUserDTO
>
result
=
BeanUtilsHelper
.
convert
(
list
,
CiamUserDTO
.
class
);
return
Response
.
createSuccess
(
result
);
}
catch
(
Exception
e
)
{
log
.
error
(
"测试demo,查询方法执行出错,错误信息为:{}"
,
e
);
return
Response
.
createError
(
"服务忙"
,
Collections
.
emptyList
());
}
}
@PostMapping
(
"/get"
)
@ApiOperation
(
value
=
"查询(get)"
,
notes
=
"测试demo"
)
public
Response
<
CiamUserDTO
>
get
(
long
id
)
{
try
{
QueryWrapper
queryWrapper
=
new
QueryWrapper
();
queryWrapper
.
eq
(
"id"
,
id
);
queryWrapper
.
eq
(
"del2"
,
0
);
CiamUserPO
record
=
userService
.
getOne
(
queryWrapper
);
CiamUserDTO
resp
=
new
CiamUserDTO
();
BeanUtils
.
copyProperties
(
record
,
resp
);
return
Response
.
createSuccess
(
resp
);
}
catch
(
Exception
e
)
{
log
.
error
(
"测试demo,查询方法执行出错,错误信息为:{}"
,
e
);
return
Response
.
createError
(
"服务忙"
,
Collections
.
emptyList
());
}
}
@PostMapping
(
"/queryUserByUuid"
)
@ApiOperation
(
value
=
"查询(queryUser)"
,
notes
=
"查询(queryUser)"
)
public
Response
<
CiamUserDTO
>
queryUserByUuid
(
@RequestBody
CiamUserDTO
entity
)
{
log
.
info
(
"queryUserByUuid入参{}"
,
JSONObject
.
toJSONString
(
entity
));
try
{
QueryWrapper
queryWrapper
=
new
QueryWrapper
();
queryWrapper
.
eq
(
"uuid"
,
entity
.
getUuid
());
queryWrapper
.
eq
(
"is_delete"
,
0
);
CiamUserPO
record
=
userService
.
getOne
(
queryWrapper
);
if
(
null
==
record
){
return
Response
.
createError
(
"没有查询到用户信息"
,
Collections
.
emptyList
());
}
CiamUserDTO
resp
=
new
CiamUserDTO
();
BeanUtils
.
copyProperties
(
record
,
resp
);
return
Response
.
createSuccess
(
resp
);
}
catch
(
Exception
e
)
{
return
Response
.
createError
(
"查询失败"
,
Collections
.
emptyList
());
}
}
@PostMapping
(
"/page"
)
@ApiOperation
(
value
=
"分页查询(page)"
,
notes
=
"测试demo"
)
public
PageResult
<
CiamUserDTO
>
page
(
long
current
,
long
size
)
{
try
{
QueryWrapper
queryWrapper
=
new
QueryWrapper
();
queryWrapper
.
orderByDesc
(
"u_time2"
);
queryWrapper
.
eq
(
"del2"
,
0
);
Page
<
CiamUserPO
>
page
=
userService
.
page
(
new
Page
<>(
current
,
size
),
queryWrapper
);
return
PageHelper
.
convert
(
page
,
CiamUserDTO
.
class
);
}
catch
(
Exception
e
)
{
log
.
error
(
"测试demo,分页查询方法执行出错,错误信息为:{}"
,
e
);
return
PageResult
.
createError
(
"服务忙"
);
}
}
@PostMapping
(
"/getByPhone"
)
@ApiOperation
(
value
=
"通过手机号查询用户信息"
,
notes
=
"通过手机号查询用户信息"
)
public
Response
<
List
<
CiamUserDTO
>>
getByPhone
(
@RequestBody
CiamUserDTO
bean
)
{
if
(
CuscStringUtils
.
isEmpty
(
bean
.
getPhoneNum
()))
{
return
Response
.
createError
(
ResponseCode
.
PARAMETER_NOT_NULL
.
getMsg
(),
ResponseCode
.
PARAMETER_NOT_NULL
.
getCode
());
}
List
<
CiamUserDTO
>
retList
=
null
;
if
(
bean
.
getTenantNo
()
!=
null
)
{
//有租户,走数据库查询
CiamUserDTO
user
=
userService
.
getUserByPhoneTenantNo
(
bean
);
if
(
user
!=
null
)
{
retList
=
new
ArrayList
<>();
retList
.
add
(
user
);
}
}
else
{
//没有租户,走es查询
//retList = ciamEsService.getUserList(bean);
}
return
Response
.
createSuccess
(
retList
);
}
@PostMapping
(
"/getByPhoneTenantNo"
)
@ApiOperation
(
value
=
"通过手机号和租户编号查询用户信息"
,
notes
=
"通过手机号和租户编号查询用户信息"
)
public
Response
<
CiamUserDTO
>
getByPhoneTenantNo
(
@RequestBody
CiamUserDTO
bean
)
{
if
(
CuscStringUtils
.
isEmpty
(
bean
.
getTenantNo
())
||
CuscStringUtils
.
isEmpty
(
bean
.
getPhoneNum
()))
{
return
Response
.
createError
(
ResponseCode
.
PARAMETER_NOT_NULL
.
getMsg
(),
ResponseCode
.
PARAMETER_NOT_NULL
.
getCode
());
}
return
Response
.
createSuccess
(
userService
.
getUserByPhoneTenantNo
(
bean
));
}
/**
* 自助注册,如果有账户就直接登录,没有账户就创建一个账户,然后登录
* @return
*/
//@PostMapping("/login")
//public Response<LoginResponseDTO> login(@RequestBody LoginRequestDTO loginRequestDTO){
// return Response.createSuccess(userService.login(loginRequestDTO));
//}
/**
* 实名注册
*
* @return
*/
@PostMapping
(
"/createUser"
)
@ApiOperation
(
value
=
"实名注册"
,
notes
=
"创建用户"
)
public
Response
createUser
(
@RequestBody
CiamUserDTO
entity
)
{
if
(
CuscStringUtils
.
isEmpty
(
entity
.
getTenantNo
())
||
CuscStringUtils
.
isEmpty
(
entity
.
getPhoneNum
()))
{
return
Response
.
createError
(
ResponseCode
.
PARAMETER_NOT_NULL
.
getMsg
(),
ResponseCode
.
PARAMETER_NOT_NULL
.
getCode
());
}
return
Response
.
createSuccess
(
userService
.
addOrGet
(
entity
));
}
}
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/ciam/controller/CryptController.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.ciam.controller
;
import
com.cusc.nirvana.common.result.Response
;
import
com.cusc.nirvana.user.ciam.dto.CiamUserDTO
;
import
com.cusc.nirvana.user.util.CuscStringUtils
;
import
com.cusc.nirvana.user.util.crypt.CryptKeyUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.util.HashMap
;
import
java.util.Map
;
@Slf4j
@RestController
@RequestMapping
(
"/crypt"
)
public
class
CryptController
{
@PostMapping
(
"/encrypt"
)
public
Response
encrypt
(
@RequestBody
CiamUserDTO
bean
)
{
Map
<
String
,
String
>
ret
=
new
HashMap
<>();
if
(
CuscStringUtils
.
isNotEmpty
(
bean
.
getPhoneNum
()))
{
ret
.
put
(
"phone"
,
CryptKeyUtil
.
encryptToBase64
(
bean
.
getPhoneNum
()));
}
return
Response
.
createSuccess
(
ret
);
}
@PostMapping
(
"/decrypt"
)
public
Response
decrypt
(
@RequestBody
CiamUserDTO
bean
)
{
Map
<
String
,
String
>
ret
=
new
HashMap
<>();
if
(
CuscStringUtils
.
isNotEmpty
(
bean
.
getPhoneNum
()))
{
ret
.
put
(
"phone"
,
CryptKeyUtil
.
decryptByBase64
(
bean
.
getPhoneNum
()));
}
return
Response
.
createSuccess
(
ret
);
}
}
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/ciam/controller/UserThirdpartyController.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.ciam.controller
;
import
com.cusc.nirvana.common.result.Response
;
import
com.cusc.nirvana.user.ciam.dto.CiamUserThirdpartyDTO
;
import
com.cusc.nirvana.user.ciam.service.IUserThirdpartyService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.util.List
;
/**
* 终端用户三方认证(UserThirdparty)表控制层
*
* @author yuy336
* @since 2022-05-05 19:54:47
*/
@RestController
@RequestMapping
(
"/userThirdparty"
)
public
class
UserThirdpartyController
{
/**
* 服务对象
*/
@Autowired
private
IUserThirdpartyService
userThirdpartyService
;
/**
* 通过主键查询单条数据
*
* @param bean 实体
* @return 单条数据
*/
@PostMapping
(
"/getByUuid"
)
public
Response
<
CiamUserThirdpartyDTO
>
getByUuid
(
@RequestBody
CiamUserThirdpartyDTO
bean
)
{
return
Response
.
createSuccess
(
userThirdpartyService
.
getByUuid
(
bean
));
}
/**
* 通过主键查询单条数据
*
* @param bean 实体
* @return 单条数据
*/
@PostMapping
(
"/queryByList"
)
public
Response
<
List
<
CiamUserThirdpartyDTO
>>
queryByList
(
@RequestBody
CiamUserThirdpartyDTO
bean
)
{
return
Response
.
createSuccess
(
userThirdpartyService
.
queryByList
(
bean
));
}
/**
* 新增数据
*
* @param bean 实体
* @return 新增结果
*/
@PostMapping
(
"/add"
)
public
Response
add
(
@RequestBody
CiamUserThirdpartyDTO
bean
)
{
return
Response
.
createSuccess
(
userThirdpartyService
.
insert
(
bean
));
}
/**
* 编辑数据
*
* @param bean 实体
* @return 编辑结果
*/
@PostMapping
(
"/update"
)
public
Response
update
(
@RequestBody
CiamUserThirdpartyDTO
bean
)
{
return
Response
.
createSuccess
(
userThirdpartyService
.
update
(
bean
));
}
/**
* 删除数据
*
* @param bean 实体
* @return 删除是否成功
*/
@PostMapping
(
"/deleteById"
)
public
Response
<
Boolean
>
deleteById
(
@RequestBody
CiamUserThirdpartyDTO
bean
)
{
return
Response
.
createSuccess
(
userThirdpartyService
.
deleteById
(
bean
));
}
}
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/ciam/converter/UserConverter.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.ciam.converter
;
import
com.cusc.nirvana.user.ciam.dao.entity.CiamUserPO
;
import
com.cusc.nirvana.user.ciam.dto.CiamUserDTO
;
import
org.mapstruct.Mapper
;
import
org.mapstruct.factory.Mappers
;
/**
* Description: 用户信息转换器
* <br />
* CreateDate 2021-11-22 10:46
*
* @author yuy336
**/
@Mapper
public
interface
UserConverter
{
UserConverter
INSTANCE
=
Mappers
.
getMapper
(
UserConverter
.
class
);
CiamUserDTO
poDataToDto
(
CiamUserPO
user
);
CiamUserPO
dtoDataToPo
(
CiamUserDTO
user
);
}
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/ciam/converter/UserThirdpartyConverter.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.ciam.converter
;
import
com.cusc.nirvana.user.ciam.dao.entity.UserThirdpartyPO
;
import
com.cusc.nirvana.user.ciam.dto.CiamUserThirdpartyDTO
;
import
org.mapstruct.Mapper
;
import
org.mapstruct.factory.Mappers
;
import
java.util.List
;
/**
* 终端用户三方认证(UserThirdparty)表服务接口
*
* @author yuy336
* @since 2022-05-05 19:54:47
*/
@Mapper
public
interface
UserThirdpartyConverter
{
UserThirdpartyConverter
INSTANCE
=
Mappers
.
getMapper
(
UserThirdpartyConverter
.
class
);
/**
* Description: do 转 dto
* <br />
* CreateDate 2021-11-18 15:21:27
*
* @author yuyi
**/
CiamUserThirdpartyDTO
poToDto
(
UserThirdpartyPO
bean
);
/**
* Description: dto 转 do
* <br />
* CreateDate 2021-11-18 15:21:27
*
* @author yuyi
**/
UserThirdpartyPO
dtoToPo
(
CiamUserThirdpartyDTO
bean
);
/**
* Description: do list 转 dto list
* <br />
* CreateDate 2021-11-18 15:21:27
*
* @author yuyi
**/
List
<
CiamUserThirdpartyDTO
>
poListToDtoList
(
List
<
UserThirdpartyPO
>
list
);
}
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/ciam/dao/CiamUserDao.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.ciam.dao
;
import
com.baomidou.mybatisplus.core.mapper.BaseMapper
;
import
com.cusc.nirvana.user.ciam.dao.entity.CiamUserPO
;
/**
* <p>
* 终端用户 Mapper 接口
* </p>
*
* @author auto-generator
* @since 2021-10-14
*/
public
interface
CiamUserDao
extends
BaseMapper
<
CiamUserPO
>
{
}
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/ciam/dao/UserThirdpartyDao.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.ciam.dao
;
import
com.cusc.nirvana.user.ciam.dao.entity.UserThirdpartyPO
;
import
com.baomidou.mybatisplus.core.mapper.BaseMapper
;
/**
* 终端用户三方认证(UserThirdparty)表数据库访问层
*
* @author yuy336
* @since 2022-05-05 19:54:47
*/
public
interface
UserThirdpartyDao
extends
BaseMapper
<
UserThirdpartyPO
>
{
}
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/ciam/dao/entity/CiamUserPO.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.ciam.dao.entity
;
import
com.baomidou.mybatisplus.annotation.TableField
;
import
com.baomidou.mybatisplus.annotation.TableName
;
import
com.cusc.nirvana.user.ciam.common.BaseIamPO
;
import
com.cusc.nirvana.user.ciam.dao.handler.EncryptDataTypeHandler
;
import
lombok.Data
;
/**
* <p>
* 用户DO
* </p>
*
* @author yuy336
* @since 2021-10-14
*/
@Data
@TableName
(
value
=
"ciam_user"
,
autoResultMap
=
true
)
public
class
CiamUserPO
extends
BaseIamPO
{
private
static
final
long
serialVersionUID
=
1L
;
/**
* 用户uuid
*/
@TableField
(
"uuid"
)
private
String
uuid
;
/**
* 手机号码
*/
@TableField
(
value
=
"phone_num"
,
typeHandler
=
EncryptDataTypeHandler
.
class
)
private
String
phoneNum
;
/**
* 昵称
*/
@TableField
(
value
=
"nick_name"
)
private
String
nickName
;
/**
* 头像
*/
@TableField
(
value
=
"head_portrait"
)
private
String
headPortrait
;
/**
* 租户id
*/
@TableField
(
"tenant_no"
)
private
String
tenantNo
;
/**
* 状态
*/
@TableField
(
"status"
)
private
Integer
status
;
/**
* 路由值
*/
@TableField
(
"routing_key"
)
private
Long
routingKey
;
/**
* 操作人
*/
@TableField
(
value
=
"operator"
)
private
String
operator
;
}
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/ciam/dao/entity/UserThirdpartyPO.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.ciam.dao.entity
;
import
com.baomidou.mybatisplus.annotation.TableField
;
import
com.baomidou.mybatisplus.annotation.TableName
;
import
com.cusc.nirvana.user.ciam.common.BaseIamPO
;
import
lombok.Data
;
/**
* 终端用户三方认证(UserThirdparty)实体类
*
* @author yuy336
* @since 2022-05-05 19:54:47
*/
@TableName
(
"ciam_user_thirdparty"
)
@Data
public
class
UserThirdpartyPO
extends
BaseIamPO
{
private
static
final
long
serialVersionUID
=
394105662979888841L
;
/**
* 业务主键
*/
@TableField
(
"uuid"
)
private
String
uuid
;
/**
* 终端用户id(ciam_user)
*/
@TableField
(
"user_id"
)
private
String
userId
;
/**
* 认证类型:1 微信 2 支付宝 3 钉钉
*/
@TableField
(
"third_party_type"
)
private
Integer
thirdPartyType
;
/**
* 三方id
*/
@TableField
(
"third_party_id"
)
private
String
thirdPartyId
;
/**
* 租户编号
*/
@TableField
(
"tenant_no"
)
private
String
tenantNo
;
/**
* 分表路由键
*/
@TableField
(
"routing_key"
)
private
Long
routingKey
;
/**
* 最后一次操作人
*/
@TableField
(
"operator"
)
private
String
operator
;
}
local-rnr-user-server/src/main/java/com/cusc/nirvana/user/ciam/dao/handler/EncryptData.java
0 → 100644
View file @
02be8110
package
com.cusc.nirvana.user.ciam.dao.handler
;
import
org.apache.ibatis.type.Alias
;
/**
* MyBatis JavaType 别名
*/
@Alias
(
"encryptData"
)
public
class
EncryptData
{
}
Prev
1
…
3
4
5
6
7
8
9
10
11
…
13
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment