Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
X
xyqb-user2
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
head_group
xyqb-user2
Commits
4af777a0
Commit
4af777a0
authored
Jul 13, 2023
by
王亮
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update previous code.
parent
a6d30a2a
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
154 additions
and
77 deletions
+154
-77
PasswordFreeAccessValidateAdvisor.java
.../xyqb/aspect/limit/PasswordFreeAccessValidateAdvisor.java
+13
-4
UserController.java
...n/quantgroup/xyqb/controller/external/UserController.java
+60
-52
UserApiController.java
...roup/xyqb/controller/internal/user/UserApiController.java
+8
-2
InnerInterceptor.java
...main/java/cn/quantgroup/xyqb/filter/InnerInterceptor.java
+1
-1
SessionServiceImpl.java
...ntgroup/xyqb/service/session/impl/SessionServiceImpl.java
+57
-15
XyqbSessionContextHolder.java
.../cn/quantgroup/xyqb/session/XyqbSessionContextHolder.java
+15
-3
No files found.
src/main/java/cn/quantgroup/xyqb/aspect/limit/PasswordFreeAccessValidateAdvisor.java
View file @
4af777a0
package
cn
.
quantgroup
.
xyqb
.
aspect
.
limit
;
import
cn.quantgroup.xyqb.Constants
;
import
cn.quantgroup.xyqb.constant.UserConstant
;
import
cn.quantgroup.xyqb.entity.User
;
import
cn.quantgroup.xyqb.model.JsonResult
;
import
cn.quantgroup.xyqb.model.LoginProperties
;
...
...
@@ -81,25 +82,33 @@ public class PasswordFreeAccessValidateAdvisor {
}
// 当前请求的Token
String
token
=
request
.
getHeader
(
Constants
.
X_AUTH_TOKEN
);
int
tenantId
=
UserConstant
.
defaultTenantId
;
String
tenantIdString
=
request
.
getHeader
(
Constants
.
X_AUTH_TENANT
);
if
(
StringUtils
.
isNotEmpty
(
tenantIdString
)){
tenantId
=
Integer
.
parseInt
(
tenantIdString
);
}
// if (StringUtils.length(token) != Constants.TOKEN_LENGTH) {
// log.info("非法请求 - 无效token, token={}, phoneNo={}, userId={}, clientIp={}", token, phoneNo, userId, clientIp);
// return false;
// }
// 当前session
SessionStruct
session
=
XyqbSessionContextHolder
.
getXSessionFromRedis
(
token
);
SessionStruct
session
=
XyqbSessionContextHolder
.
getXSessionFromRedis
(
token
,
tenantId
);
if
(
Objects
.
isNull
(
session
)
||
Objects
.
isNull
(
session
.
getValues
())
||
Objects
.
isNull
(
session
.
getValues
().
getUser
()))
{
log
.
info
(
"非法请求 - 未登录, token={}, phoneNo={}, userId={}, clientIp={}"
,
token
,
phoneNo
,
userId
,
clientIp
);
return
false
;
}
// 获取头部qg-tenant-id
String
tenantId
=
request
.
getHeader
(
Constants
.
X_AUTH_TENANT
);
LoginProperties
loginProperties
=
session
.
getValues
().
getLoginProperties
();
// 如果token Session tenantId 不为空
if
(!
Objects
.
isNull
(
loginProperties
.
getTenantId
()))
{
// 如果头部没有tenantId参数
if
(
StringUtils
.
isBlank
(
tenantId
))
{
if
(
StringUtils
.
isBlank
(
tenantId
String
))
{
// 如果 token Session tenantId 不是默认羊小咩, 那么拒绝登陆
if
(!
loginProperties
.
getTenantId
().
equals
(
TenantUtil
.
TENANT_DEFAULT
))
{
log
.
info
(
"非法请求 - 错误租户, token={}, phoneNo={}, userId={}, clientIp={}, tenantId={}, loginTenantId={}"
,
token
,
phoneNo
,
userId
,
clientIp
,
tenantId
,
loginProperties
.
getTenantId
().
toString
());
...
...
@@ -114,7 +123,7 @@ public class PasswordFreeAccessValidateAdvisor {
}
}
else
{
// 如果token seesion tenantId 为空, tenantId不为空,并且不是默认羊小咩
if
(!
StringUtils
.
isBlank
(
tenantId
)
&&
!
TenantUtil
.
TENANT_DEFAULT
.
toString
().
equals
(
tenantId
))
{
if
(!
StringUtils
.
isBlank
(
tenantId
String
)
&&
!
TenantUtil
.
TENANT_DEFAULT
.
toString
().
equals
(
tenantId
))
{
log
.
info
(
"非法请求 - 错误租户, token={}, phoneNo={}, userId={}, clientIp={}, tenantId={}"
,
token
,
phoneNo
,
userId
,
clientIp
,
tenantId
);
return
false
;
}
...
...
src/main/java/cn/quantgroup/xyqb/controller/external/UserController.java
View file @
4af777a0
...
...
@@ -10,7 +10,10 @@ import cn.quantgroup.xyqb.aspect.lock.PasswordErrorFiniteValidator;
import
cn.quantgroup.xyqb.constant.UserConstant
;
import
cn.quantgroup.xyqb.constant.enums.LoginType
;
import
cn.quantgroup.xyqb.controller.IBaseController
;
import
cn.quantgroup.xyqb.entity.*
;
import
cn.quantgroup.xyqb.entity.LoginRecord
;
import
cn.quantgroup.xyqb.entity.Merchant
;
import
cn.quantgroup.xyqb.entity.User
;
import
cn.quantgroup.xyqb.entity.UserInfoEntity
;
import
cn.quantgroup.xyqb.exception.UserNotExistException
;
import
cn.quantgroup.xyqb.exception.VerificationCodeErrorException
;
import
cn.quantgroup.xyqb.model.*
;
...
...
@@ -119,12 +122,12 @@ public class UserController implements IBaseController {
@RequestParam
(
required
=
false
,
defaultValue
=
""
)
String
userId
,
@RequestParam
(
required
=
false
,
defaultValue
=
"xyqb"
)
String
key
,
@RequestParam
(
required
=
false
)
String
dimension
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_APP_ID
,
defaultValue
=
UserConstant
.
defaultAppId
)
String
appId
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_APP_ID
,
defaultValue
=
UserConstant
.
defaultAppId
)
String
appId
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantId
,
HttpServletRequest
request
)
{
log
.
info
(
"loginV1 -> channelId:{},appChennel:{},createdFrom:{},userId:{},key:{},dimension:{}"
,
channelId
,
appChannel
,
createdFrom
,
userId
,
key
,
dimension
);
return
login
(
channelId
,
appChannel
,
createdFrom
,
userId
,
key
,
dimension
,
null
,
request
,
appId
,
tenantId
);
return
login
(
channelId
,
appChannel
,
createdFrom
,
userId
,
key
,
dimension
,
null
,
request
,
appId
,
tenantId
);
}
...
...
@@ -154,11 +157,11 @@ public class UserController implements IBaseController {
@RequestParam
(
required
=
false
)
String
dimension
,
@RequestParam
Long
geetestlog_id
,
HttpServletRequest
request
,
@RequestParam
(
value
=
Constants
.
X_AUTH_APP_ID
,
defaultValue
=
UserConstant
.
defaultAppId
)
String
appId
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantId
@RequestParam
(
value
=
Constants
.
X_AUTH_APP_ID
,
defaultValue
=
UserConstant
.
defaultAppId
)
String
appId
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantId
)
{
log
.
info
(
"loginV2 -> channelId:{},appChennel:{},createdFrom:{},userId:{},key:{},dimension:{}"
,
channelId
,
appChannel
,
createdFrom
,
userId
,
key
,
dimension
);
return
login
(
channelId
,
appChannel
,
createdFrom
,
userId
,
key
,
dimension
,
geetestlog_id
,
request
,
appId
,
tenantId
);
return
login
(
channelId
,
appChannel
,
createdFrom
,
userId
,
key
,
dimension
,
geetestlog_id
,
request
,
appId
,
tenantId
);
}
...
...
@@ -177,7 +180,7 @@ public class UserController implements IBaseController {
@RequestParam
(
required
=
false
)
String
dimension
,
@RequestParam
(
required
=
false
)
Long
geetestLogId
,
HttpServletRequest
request
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_APP_ID
,
defaultValue
=
UserConstant
.
defaultAppId
)
String
appId
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_APP_ID
,
defaultValue
=
UserConstant
.
defaultAppId
)
String
appId
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantId
)
{
log
.
info
(
"login -> channelId:{},appChannel:{},createdFrom:{},userId:{},key:{},dimension:{}"
,
channelId
,
appChannel
,
createdFrom
,
userId
,
key
,
dimension
);
...
...
@@ -186,9 +189,9 @@ public class UserController implements IBaseController {
return
JsonResult
.
buildErrorStateResult
(
"未知的连接"
,
null
);
}
if
(
StringUtils
.
length
(
userId
)
>
Constants
.
UUID_MIN_LENGTH
)
{
return
loginWithUserId
(
channelId
,
appChannel
,
createdFrom
,
userId
,
merchant
,
geetestLogId
,
request
,
appId
,
tenantId
);
return
loginWithUserId
(
channelId
,
appChannel
,
createdFrom
,
userId
,
merchant
,
geetestLogId
,
request
,
appId
,
tenantId
);
}
else
{
return
loginWithHttpBasic
(
channelId
,
appChannel
,
createdFrom
,
merchant
,
dimension
,
geetestLogId
,
request
,
appId
,
tenantId
);
return
loginWithHttpBasic
(
channelId
,
appChannel
,
createdFrom
,
merchant
,
dimension
,
geetestLogId
,
request
,
appId
,
tenantId
);
}
}
...
...
@@ -216,10 +219,10 @@ public class UserController implements IBaseController {
@RequestParam
(
required
=
false
)
String
dimension
,
@RequestParam
(
name
=
"click_id"
,
required
=
false
)
String
clickId
,
@RequestParam
(
required
=
false
)
Integer
tenantId
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_APP_ID
,
defaultValue
=
UserConstant
.
defaultAppId
)
String
appId
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_APP_ID
,
defaultValue
=
UserConstant
.
defaultAppId
)
String
appId
,
HttpServletRequest
request
)
{
log
.
info
(
"login/fastV1 -> channelId:{},ZappChennel:{},createdFrom:{},key:{},btRegisterChannelId:{},dimension:{},clickId:{}"
,
channelId
,
appChannel
,
createdFrom
,
key
,
btRegisterChannelId
,
dimension
,
clickId
);
return
loginFast
(
channelId
,
appChannel
,
createdFrom
,
key
,
btRegisterChannelId
,
dimension
,
clickId
,
tenantId
,
null
,
appId
,
request
);
return
loginFast
(
channelId
,
appChannel
,
createdFrom
,
key
,
btRegisterChannelId
,
dimension
,
clickId
,
tenantId
,
null
,
appId
,
request
);
}
/**
...
...
@@ -237,7 +240,7 @@ public class UserController implements IBaseController {
@RequestParam
(
name
=
"click_id"
,
required
=
false
)
String
clickId
,
@RequestParam
(
required
=
false
)
Integer
tenantId
,
@RequestParam
(
required
=
false
)
Long
geetestLogId
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_APP_ID
,
defaultValue
=
UserConstant
.
defaultAppId
)
String
appId
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_APP_ID
,
defaultValue
=
UserConstant
.
defaultAppId
)
String
appId
,
HttpServletRequest
request
)
{
Map
<
String
,
JsonResult
>
validMap
=
getHeaderParam
(
request
);
log
.
info
(
"login/fast -> channelId:{},appChannel:{},createdFrom:{},btRegisterChannelId:{},key:{},dimension:{},clickId:{}"
,
channelId
,
appChannel
,
createdFrom
,
btRegisterChannelId
,
key
,
dimension
,
clickId
);
...
...
@@ -262,7 +265,7 @@ public class UserController implements IBaseController {
if
(
TenantUtil
.
validationTenantIdIsNullOrZero
(
tenantId
))
{
tenantId
=
TenantUtil
.
TENANT_DEFAULT
;
}
return
userService
.
loginFast
(
channelId
,
appChannel
,
createdFrom
,
btRegisterChannelId
,
dimension
,
clickId
,
request
,
merchant
,
phoneNo
,
tenantId
,
geetestLogId
,
appId
);
return
userService
.
loginFast
(
channelId
,
appChannel
,
createdFrom
,
btRegisterChannelId
,
dimension
,
clickId
,
request
,
merchant
,
phoneNo
,
tenantId
,
geetestLogId
,
appId
);
}
/**
...
...
@@ -365,10 +368,10 @@ public class UserController implements IBaseController {
@IpValidator
@RequestMapping
(
"/exist"
)
@Deprecated
public
JsonResult
exist
(
@RequestParam
String
phoneNo
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantId
public
JsonResult
exist
(
@RequestParam
String
phoneNo
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantId
)
{
log
.
info
(
"检查用户是否存在, phoneNo:{}"
,
phoneNo
);
if
(
userService
.
exist
(
phoneNo
,
tenantId
))
{
if
(
userService
.
exist
(
phoneNo
,
tenantId
))
{
log
.
info
(
"该手机号已经注册, phoneNo:{},remoteIp:{}"
,
phoneNo
,
getIp
());
return
JsonResult
.
buildErrorStateResult
(
"该手机号已经注册"
,
null
);
}
...
...
@@ -386,10 +389,10 @@ public class UserController implements IBaseController {
@IpValidator
@RequestMapping
(
"/exist_check"
)
@Deprecated
public
JsonResult
existForResetPwd
(
@RequestParam
String
phoneNo
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantId
public
JsonResult
existForResetPwd
(
@RequestParam
String
phoneNo
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantId
)
{
log
.
info
(
"检查用户是否存在, phoneNo:{},remoteIp:{}"
,
phoneNo
,
getIp
());
return
JsonResult
.
buildSuccessResult
(
null
,
userService
.
exist
(
phoneNo
,
tenantId
));
return
JsonResult
.
buildSuccessResult
(
null
,
userService
.
exist
(
phoneNo
,
tenantId
));
}
/**
...
...
@@ -405,7 +408,7 @@ public class UserController implements IBaseController {
public
JsonResult
resetPassword
(
@RequestParam
String
phoneNo
,
@RequestParam
String
password
,
@RequestParam
(
required
=
false
)
String
registerFrom
,
@RequestParam
String
verificationCode
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantId
@RequestParam
String
verificationCode
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantId
)
{
if
(!
ValidationUtil
.
validatePhoneNo
(
phoneNo
))
{
return
JsonResult
.
buildErrorStateResult
(
"手机号错误"
,
null
);
...
...
@@ -417,11 +420,11 @@ public class UserController implements IBaseController {
return
JsonResult
.
buildErrorStateResult
(
PasswordUtil
.
TOAST_MSG
,
null
);
}
verifyPhoneAndCode
(
phoneNo
,
verificationCode
);
if
(!
userService
.
exist
(
phoneNo
,
tenantId
))
{
if
(!
userService
.
exist
(
phoneNo
,
tenantId
))
{
log
.
info
(
"修改密码失败,该手机号尚未注册, registerFrom:{}, phoneNo:{}"
,
registerFrom
,
phoneNo
);
return
JsonResult
.
buildErrorStateResult
(
"修改密码失败"
,
null
);
}
if
(!
userService
.
resetPassword
(
phoneNo
,
password
,
tenantId
))
{
if
(!
userService
.
resetPassword
(
phoneNo
,
password
,
tenantId
))
{
return
JsonResult
.
buildErrorStateResult
(
"修改密码失败"
,
null
);
}
...
...
@@ -441,7 +444,7 @@ public class UserController implements IBaseController {
@Deprecated
@PasswordFreeAccessValidator
@RequestMapping
(
path
=
"/resetPassword"
,
method
=
RequestMethod
.
POST
)
public
JsonResult
resetPassword
(
@RequestParam
String
phoneNo
,
@RequestParam
String
password
,
@RequestParam
(
required
=
false
)
String
passwordNew
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantId
public
JsonResult
resetPassword
(
@RequestParam
String
phoneNo
,
@RequestParam
String
password
,
@RequestParam
(
required
=
false
)
String
passwordNew
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantId
)
{
if
(!
ValidationUtil
.
validatePhoneNo
(
phoneNo
))
{
return
JsonResult
.
buildErrorStateResult
(
"手机号错误"
,
null
);
...
...
@@ -452,7 +455,7 @@ public class UserController implements IBaseController {
if
(!
ValidationUtil
.
validatePassword
(
password
))
{
return
JsonResult
.
buildErrorStateResult
(
PasswordUtil
.
TOAST_MSG
,
null
);
}
User
user
=
userService
.
findByPhoneWithCache
(
phoneNo
,
tenantId
);
User
user
=
userService
.
findByPhoneWithCache
(
phoneNo
,
tenantId
);
if
(
Objects
.
isNull
(
user
))
{
log
.
info
(
"修改密码失败,该手机号尚未注册, phoneNo:{}"
,
phoneNo
);
return
JsonResult
.
buildErrorStateResult
(
"修改密码失败"
,
null
);
...
...
@@ -471,12 +474,12 @@ public class UserController implements IBaseController {
return
JsonResult
.
buildErrorStateResult
(
"修改密码失败"
,
null
);
}
}
else
{
if
(
StringUtils
.
isNotBlank
(
user
.
getPassword
())
&&
!
PasswordUtil
.
validatePassword
(
password
,
user
.
getPassword
(),
user
.
getPasswordType
()))
{
if
(
StringUtils
.
isNotBlank
(
user
.
getPassword
())
&&
!
PasswordUtil
.
validatePassword
(
password
,
user
.
getPassword
(),
user
.
getPasswordType
()))
{
return
JsonResult
.
buildErrorStateResult
(
"修改密码失败"
,
null
);
}
}
if
(!
userService
.
resetPassword
(
phoneNo
,
passwordNew
,
tenantId
))
{
if
(!
userService
.
resetPassword
(
phoneNo
,
passwordNew
,
tenantId
))
{
return
JsonResult
.
buildErrorStateResult
(
"修改密码失败"
,
null
);
}
return
JsonResult
.
buildSuccessResult
(
"修改密码成功"
);
...
...
@@ -492,7 +495,7 @@ public class UserController implements IBaseController {
*/
@IpValidator
@RequestMapping
(
"/exists_token"
)
public
JsonResult
checkToken
(
@RequestParam
String
token
)
{
public
JsonResult
checkToken
(
@RequestParam
String
token
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantId
)
{
if
(
StringUtils
.
isEmpty
(
token
))
{
return
JsonResult
.
buildSuccessResult
(
null
,
false
);
}
...
...
@@ -500,11 +503,16 @@ public class UserController implements IBaseController {
return
JsonResult
.
buildSuccessResult
(
null
,
false
);
}
String
tokenKey
=
Constants
.
SESSION_PREFIX
+
token
;
String
tokenKey2
=
Constants
.
Session
.
USER_SESSION_CACHE
+
token
;
String
tokenKey2
;
if
(
tenantId
==
null
||
UserConstant
.
defaultTenantId
.
equals
(
tenantId
))
{
tokenKey2
=
Constants
.
Session
.
USER_SESSION_CACHE
+
token
;
}
else
{
tokenKey2
=
Constants
.
Session
.
USER_SESSION_CACHE
+
tenantId
+
":"
+
token
;
}
// 判断token是否存在
boolean
exist
=
stringRedisTemplate
.
hasKey
(
tokenKey
)
||
stringRedisTemplate
.
hasKey
(
tokenKey2
);
if
(!
token
.
contains
(
prefix
)
&&
!
exist
)
{
Integer
tenantId
=
TenantUtil
.
TENANT_DEFAULT
;
findTokenExchange
(
token
,
tenantId
);
exist
=
stringRedisTemplate
.
hasKey
(
tokenKey
)
||
stringRedisTemplate
.
hasKey
(
tokenKey2
);
}
...
...
@@ -517,7 +525,7 @@ public class UserController implements IBaseController {
* @yapi http://yapi.quantgroups.com/project/17/interface/api/9191
*/
@RequestMapping
(
"/token"
)
public
JsonResult
token
(
@RequestParam
String
token
,
@RequestParam
(
required
=
false
)
Integer
tenantId
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantIdHeader
)
{
public
JsonResult
token
(
@RequestParam
String
token
,
@RequestParam
(
required
=
false
)
Integer
tenantId
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantIdHeader
)
{
if
(
tenantId
==
null
)
{
tenantId
=
tenantIdHeader
;
...
...
@@ -530,12 +538,12 @@ public class UserController implements IBaseController {
if
(
token
.
contains
(
Constants
.
TOKEN_MASTER
))
{
return
JsonResult
.
buildSuccessResult
(
null
,
result
);
}
SessionStruct
sessionStruct
=
XyqbSessionContextHolder
.
getXSessionFromRedis
(
token
);
SessionStruct
sessionStruct
=
XyqbSessionContextHolder
.
getXSessionFromRedis
(
token
,
tenantId
);
if
(
sessionStruct
==
null
||
sessionStruct
.
getValues
()
==
null
)
{
// 使用token去电商查询信息, 如果token 不包含本系统token前缀
if
(!
token
.
contains
(
prefix
))
{
findTokenExchange
(
token
,
tenantId
);
sessionStruct
=
XyqbSessionContextHolder
.
getXSessionFromRedis
(
token
);
sessionStruct
=
XyqbSessionContextHolder
.
getXSessionFromRedis
(
token
,
tenantId
);
if
(
sessionStruct
==
null
||
sessionStruct
.
getValues
()
==
null
)
{
return
JsonResult
.
buildSuccessResult
(
null
,
result
);
}
...
...
@@ -545,7 +553,7 @@ public class UserController implements IBaseController {
}
User
user
=
sessionStruct
.
getValues
().
getUser
();
if
(
user
.
getTenantId
()
==
null
||
!
user
.
getEnable
().
equals
(
tenantId
))
{
log
.
info
(
"当前token对应的用户非当前租户,userId:{},用户tenantId:{},入参tenantId:{},token:{}"
,
user
.
getId
(),
user
.
getTenantId
(),
tenantId
,
token
);
log
.
info
(
"当前token对应的用户非当前租户,userId:{},用户tenantId:{},入参tenantId:{},token:{}"
,
user
.
getId
(),
user
.
getTenantId
(),
tenantId
,
token
);
return
JsonResult
.
buildSuccessResult
(
null
,
result
);
}
...
...
@@ -563,7 +571,7 @@ public class UserController implements IBaseController {
* token 交换
*/
@RequestMapping
(
"/tokenExchange"
)
public
JsonResult
tokenExchange
(
@RequestParam
String
token
,
@RequestParam
(
required
=
false
)
Integer
tenantId
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantIdHeader
)
{
public
JsonResult
tokenExchange
(
@RequestParam
String
token
,
@RequestParam
(
required
=
false
)
Integer
tenantId
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantIdHeader
)
{
if
(
tenantId
==
null
)
{
tenantId
=
tenantIdHeader
;
...
...
@@ -576,13 +584,13 @@ public class UserController implements IBaseController {
if
(
token
.
contains
(
Constants
.
TOKEN_MASTER
))
{
return
JsonResult
.
buildSuccessResult
(
null
,
tokenExchange
);
}
SessionStruct
sessionStruct
=
XyqbSessionContextHolder
.
getXSessionFromRedis
(
token
);
SessionStruct
sessionStruct
=
XyqbSessionContextHolder
.
getXSessionFromRedis
(
token
,
tenantId
);
if
(
sessionStruct
==
null
||
sessionStruct
.
getValues
()
==
null
)
{
return
JsonResult
.
buildSuccessResult
(
null
,
tokenExchange
);
}
User
user
=
sessionStruct
.
getValues
().
getUser
();
if
(
user
.
getTenantId
()
==
null
||
!
user
.
getEnable
().
equals
(
tenantId
))
{
log
.
info
(
"当前token对应的用户非当前租户,userId:{},用户tenantId:{},入参tenantId:{},token:{}"
,
user
.
getId
(),
user
.
getTenantId
(),
tenantId
,
token
);
log
.
info
(
"当前token对应的用户非当前租户,userId:{},用户tenantId:{},入参tenantId:{},token:{}"
,
user
.
getId
(),
user
.
getTenantId
(),
tenantId
,
token
);
return
JsonResult
.
buildSuccessResult
(
null
,
tokenExchange
);
}
...
...
@@ -619,14 +627,14 @@ public class UserController implements IBaseController {
TokenExchange
tokenExchange
=
tokenExchangeResult
.
getData
();
if
(
tokenExchange
!=
null
)
{
SessionValue
sessionValue
=
new
SessionValue
();
User
user
=
userService
.
findById
(
tokenExchange
.
getUserId
(),
tenantId
);
User
user
=
userService
.
findById
(
tokenExchange
.
getUserId
(),
tenantId
);
if
(
user
!=
null
)
{
LoginProperties
loginProperties
=
JSONObject
.
parseObject
(
tokenExchange
.
getLoginProperties
(),
LoginProperties
.
class
);
// 根据返回值生成token, 返回值包含user:session:token的值
sessionValue
.
setUser
(
user
);
sessionValue
.
setLoginProperties
(
loginProperties
);
sessionService
.
persistSessionExchange
(
token
,
sessionValue
,
tokenExchange
.
getExpire
(),
tenantId
);
sessionService
.
persistSessionExchange
(
token
,
sessionValue
,
tokenExchange
.
getExpire
(),
tenantId
);
}
}
...
...
@@ -686,7 +694,7 @@ public class UserController implements IBaseController {
* @yapi http://yapi.quantgroups.com/project/17/interface/api/23661
*/
@RequestMapping
(
value
=
"/logout"
,
method
=
RequestMethod
.
GET
)
public
JsonResult
logout
(
HttpServletRequest
request
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantId
public
JsonResult
logout
(
HttpServletRequest
request
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantId
)
{
String
token
=
request
.
getHeader
(
"x-auth-token"
);
...
...
@@ -696,7 +704,7 @@ public class UserController implements IBaseController {
}
try
{
userService
.
logout
(
token
,
tenantId
);
userService
.
logout
(
token
,
tenantId
);
}
catch
(
Exception
e
)
{
log
.
error
(
"登出接口系统异常token:{}"
,
token
);
return
JsonResult
.
buildErrorStateResult
(
"服务器异常,请稍后再试"
,
null
);
...
...
@@ -705,17 +713,17 @@ public class UserController implements IBaseController {
return
JsonResult
.
buildSuccessResult
(
"登出成功"
);
}
private
JsonResult
loginWithHttpBasic
(
Long
channelId
,
String
appChannel
,
Long
createdFrom
,
Merchant
merchant
,
String
dimension
,
Long
geetestLogId
,
HttpServletRequest
request
,
String
appId
,
Integer
tenantId
)
{
User
user
=
verificateUserNameAndPassword
(
request
,
tenantId
);
private
JsonResult
loginWithHttpBasic
(
Long
channelId
,
String
appChannel
,
Long
createdFrom
,
Merchant
merchant
,
String
dimension
,
Long
geetestLogId
,
HttpServletRequest
request
,
String
appId
,
Integer
tenantId
)
{
User
user
=
verificateUserNameAndPassword
(
request
,
tenantId
);
if
(
user
==
null
)
{
return
JsonResult
.
buildErrorStateResult
(
"用户名或密码不正确"
,
null
);
}
else
if
(!
user
.
getEnable
())
{
return
JsonResult
.
buildErrorStateResult
(
USER_ERROR_OR_ENABLE_ERROR
,
null
);
}
else
if
(!
wechatRelateUserIfNecessary
(
user
,
request
,
appId
,
tenantId
))
{
}
else
if
(!
wechatRelateUserIfNecessary
(
user
,
request
,
appId
,
tenantId
))
{
return
JsonResult
.
buildErrorStateResult
(
"登录时微信关联失败"
,
null
);
}
LoginProperties
loginProperties
=
new
LoginProperties
(
""
,
1
,
channelId
,
createdFrom
,
appChannel
,
merchant
.
getId
(),
merchant
.
getName
(),
null
);
AuthBean
authBean
=
sessionService
.
createSession
(
user
,
loginProperties
,
LoginType
.
ACCOUNTPASSWORD
.
ordinal
(),
tenantId
);
AuthBean
authBean
=
sessionService
.
createSession
(
user
,
loginProperties
,
LoginType
.
ACCOUNTPASSWORD
.
ordinal
(),
tenantId
);
if
(
authBean
!=
null
)
{
authBean
.
setRegister
(
false
);
}
...
...
@@ -726,7 +734,7 @@ public class UserController implements IBaseController {
return
new
JsonResult
(
authBean
);
}
private
User
verificateUserNameAndPassword
(
HttpServletRequest
request
,
Integer
tenantId
private
User
verificateUserNameAndPassword
(
HttpServletRequest
request
,
Integer
tenantId
)
{
String
credential
=
request
.
getHeader
(
"authorization"
);
if
(
StringUtils
.
isBlank
(
credential
)
||
!
credential
.
startsWith
(
Constants
.
PASSWORD_HEADER
))
{
...
...
@@ -750,7 +758,7 @@ public class UserController implements IBaseController {
}
String
phoneNo
=
credentialArr
[
0
];
String
pass
=
credentialArr
[
1
];
User
user
=
userService
.
findByPhoneWithCache
(
phoneNo
,
tenantId
);
User
user
=
userService
.
findByPhoneWithCache
(
phoneNo
,
tenantId
);
if
(
user
==
null
)
{
// 向该ipv4添加错误计数器
lockIpv4Service
.
countErrorByIpv4
(
clientIp
);
...
...
@@ -770,7 +778,7 @@ public class UserController implements IBaseController {
}
}
else
{
//验证密码
if
(!
PasswordUtil
.
validatePassword
(
pass
,
user
.
getPassword
(),
user
.
getPasswordType
()))
{
if
(!
PasswordUtil
.
validatePassword
(
pass
,
user
.
getPassword
(),
user
.
getPasswordType
()))
{
// 向该ipv4添加错误计数器
lockIpv4Service
.
countErrorByIpv4
(
clientIp
);
// 向该phoneNo添加错误计数器
...
...
@@ -797,7 +805,7 @@ public class UserController implements IBaseController {
if
(
Objects
.
isNull
(
user
)
||
!
user
.
getEnable
())
{
log
.
error
(
"用户不存在,或者已经注销,userId:{}"
,
userId
);
return
JsonResult
.
buildErrorStateResult
(
USER_ERROR_OR_ENABLE_ERROR
,
null
);
}
else
if
(!
wechatRelateUserIfNecessary
(
user
,
request
,
appId
,
tenantId
))
{
}
else
if
(!
wechatRelateUserIfNecessary
(
user
,
request
,
appId
,
tenantId
))
{
return
JsonResult
.
buildErrorStateResult
(
"登录时微信关联失败"
,
null
);
}
LoginProperties
loginProperties
=
new
LoginProperties
(
""
,
4
,
channelId
,
createdFrom
,
appChannel
,
merchant
.
getId
(),
merchant
.
getName
(),
null
);
...
...
@@ -810,7 +818,7 @@ public class UserController implements IBaseController {
}
//更新session
return
new
JsonResult
(
sessionService
.
createSession
(
user
,
loginProperties
,
LoginType
.
ACCOUNTPASSWORD
.
ordinal
(),
tenantId
));
return
new
JsonResult
(
sessionService
.
createSession
(
user
,
loginProperties
,
LoginType
.
ACCOUNTPASSWORD
.
ordinal
(),
tenantId
));
}
/**
...
...
@@ -820,7 +828,7 @@ public class UserController implements IBaseController {
* @param request - 当前请求
* @return true - 继续登录,false - 微信关联失败,重新登录
*/
private
boolean
wechatRelateUserIfNecessary
(
User
user
,
HttpServletRequest
request
,
String
appId
,
Integer
tenantId
)
{
private
boolean
wechatRelateUserIfNecessary
(
User
user
,
HttpServletRequest
request
,
String
appId
,
Integer
tenantId
)
{
Objects
.
requireNonNull
(
request
,
"无效请求"
);
String
clientIp
=
IpUtil
.
getRemoteIP
(
request
);
Set
<
String
>
paramKeys
=
request
.
getParameterMap
().
keySet
();
...
...
@@ -834,7 +842,7 @@ public class UserController implements IBaseController {
Long
userId
=
user
.
getId
();
String
phoneNo
=
user
.
getEncryptedPhoneNo
();
try
{
int
rows
=
wechatService
.
relateUser
(
userId
,
phoneNo
,
request
.
getParameter
(
Constants
.
WECHAT_OPEN_ID
),
appId
,
tenantId
);
int
rows
=
wechatService
.
relateUser
(
userId
,
phoneNo
,
request
.
getParameter
(
Constants
.
WECHAT_OPEN_ID
),
appId
,
tenantId
);
return
rows
>
0
;
}
catch
(
Exception
e
)
{
log
.
error
(
"微信关联失败,user:{}, request-Header:{}"
,
user
,
JSON
.
toJSONString
(
getRequestHeaderMap
(
request
)),
e
);
...
...
@@ -893,11 +901,11 @@ public class UserController implements IBaseController {
@RequestParam
(
required
=
false
,
defaultValue
=
""
)
Long
userId
,
@RequestParam
(
required
=
false
,
defaultValue
=
"xyqb"
)
String
key
,
@RequestParam
(
required
=
false
)
String
dimension
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantId
,
@RequestHeader
(
value
=
Constants
.
X_AUTH_TENANT
,
defaultValue
=
UserConstant
.
defaultTenantIdString
)
Integer
tenantId
)
{
//查询用户
User
user
=
userService
.
findById
(
userId
,
tenantId
);
User
user
=
userService
.
findById
(
userId
,
tenantId
);
if
(
Objects
.
isNull
(
user
)
||
!
user
.
getEnable
())
{
log
.
error
(
"用户不存在,或者已经注销,userId:{}"
,
userId
);
return
JsonResult
.
buildErrorStateResult
(
USER_ERROR_OR_ENABLE_ERROR
,
null
);
...
...
@@ -910,7 +918,7 @@ public class UserController implements IBaseController {
LoginProperties
loginProperties
=
new
LoginProperties
(
""
,
4
,
channelId
,
createdFrom
,
appChannel
,
merchant
.
getId
(),
merchant
.
getName
(),
null
);
try
{
userService
.
kdspLogout
(
userId
,
loginProperties
,
tenantId
);
userService
.
kdspLogout
(
userId
,
loginProperties
,
tenantId
);
}
catch
(
Exception
e
)
{
return
JsonResult
.
buildErrorStateResult
(
"服务器异常,请稍后再试"
,
null
);
}
...
...
src/main/java/cn/quantgroup/xyqb/controller/internal/user/UserApiController.java
View file @
4af777a0
...
...
@@ -72,14 +72,20 @@ public class UserApiController {
return
JsonResult
.
buildErrorStateResult
(
"token regular invalid "
,
token
);
}
String
tokenKey
=
Constants
.
SESSION_PREFIX
+
token
;
String
tokenKey2
=
Constants
.
Session
.
USER_SESSION_CACHE
+
token
;
String
tokenKey2
;
if
(
tenantId
==
null
||
UserConstant
.
defaultTenantId
.
equals
(
tenantId
))
{
tokenKey2
=
Constants
.
Session
.
USER_SESSION_CACHE
+
token
;
}
else
{
tokenKey2
=
Constants
.
Session
.
USER_SESSION_CACHE
+
tenantId
+
":"
+
token
;
}
// 判断token是否存在
boolean
exist
=
stringRedisTemplate
.
hasKey
(
tokenKey
)
||
stringRedisTemplate
.
hasKey
(
tokenKey2
);
log
.
info
(
"检查token:[{}]有效性[{}],延续生命期[{}]"
,
token
,
exist
,
prolong
);
/* token存在且需要延续时,进一步判断session是否有效,有效时,自动续期 */
if
(
Boolean
.
logicalAnd
(
exist
,
prolong
))
{
// 获取session信息
SessionStruct
sessionStruct
=
XyqbSessionContextHolder
.
getXSessionFromRedis
(
token
);
SessionStruct
sessionStruct
=
XyqbSessionContextHolder
.
getXSessionFromRedis
(
token
,
tenantId
);
if
(
Objects
.
isNull
(
sessionStruct
))
{
log
.
info
(
"延续token:[{}]生命期,result:[{}],SessionStruct:{}"
,
token
,
false
,
sessionStruct
);
/* 如果没有获取到session信息则返回错误信息 */
...
...
src/main/java/cn/quantgroup/xyqb/filter/InnerInterceptor.java
View file @
4af777a0
...
...
@@ -45,7 +45,7 @@ public class InnerInterceptor implements HandlerInterceptor {
throw
new
BizException
(
BizExceptionEnum
.
UN_EXIT_STMS_TOKEN
);
}
SessionStruct
sessionStruct
=
XyqbSessionContextHolder
.
getXSessionFromRedis
(
stmsToken
);
SessionStruct
sessionStruct
=
XyqbSessionContextHolder
.
getXSessionFromRedis
(
stmsToken
,
Integer
.
valueOf
(
tenantId
)
);
if
(
sessionStruct
==
null
)
{
OauthResult
oauthResult
=
stmsRemoteService
.
checkToken
(
stmsToken
);
...
...
src/main/java/cn/quantgroup/xyqb/service/session/impl/SessionServiceImpl.java
View file @
4af777a0
package
cn
.
quantgroup
.
xyqb
.
service
.
session
.
impl
;
import
cn.quantgroup.xyqb.Constants
;
import
cn.quantgroup.xyqb.constant.UserConstant
;
import
cn.quantgroup.xyqb.constant.enums.RecordType
;
import
cn.quantgroup.xyqb.entity.User
;
import
cn.quantgroup.xyqb.entity.UserTag
;
...
...
@@ -64,10 +65,10 @@ public class SessionServiceImpl implements ISessionService {
*/
@Override
public
AuthBean
createSession
(
User
user
,
LoginProperties
properties
,
int
loginType
,
Integer
tenantId
)
{
return
this
.
createSession
(
user
,
properties
,
loginType
,
tenantId
,
true
);
return
this
.
createSession
(
user
,
properties
,
loginType
,
tenantId
,
true
);
}
public
AuthBean
createSession
(
User
user
,
LoginProperties
properties
,
int
loginType
,
Integer
tenantId
,
boolean
send
)
{
public
AuthBean
createSession
(
User
user
,
LoginProperties
properties
,
int
loginType
,
Integer
tenantId
,
boolean
send
)
{
//找到用户
//TODO: 使用userId
String
sessionId
=
findSessionIdByUserIdLoginProperties
(
user
.
getId
(),
properties
,
tenantId
);
...
...
@@ -79,6 +80,7 @@ public class SessionServiceImpl implements ISessionService {
sessionStruct
.
setAttribute
(
"channelId"
,
String
.
valueOf
(
properties
.
getChannelId
()));
sessionStruct
.
setAttribute
(
"createdFrom"
,
String
.
valueOf
(
properties
.
getCreatedFrom
()));
sessionStruct
.
setAttribute
(
"appChannel"
,
String
.
valueOf
(
properties
.
getAppChannel
()));
sessionStruct
.
setTenantId
(
tenantId
);
sessionStruct
.
getValues
().
setLoginProperties
(
properties
);
persistSession
(
sessionStruct
.
getSid
(),
sessionStruct
.
getValues
(),
tenantId
);
}
else
{
...
...
@@ -95,7 +97,7 @@ public class SessionServiceImpl implements ISessionService {
// 添加登陆日志
loginRecordService
.
saveLoginRecord
(
user
.
getId
(),
RecordType
.
LOGINRECORD
.
getName
(),
loginType
);
if
(
send
)
{
if
(
send
)
{
//更新user_tag记录
applicationEventPublisher
.
publishEvent
(
new
UserLoginEvent
(
this
,
UserTag
.
builder
()
.
userId
(
user
.
getId
()).
registeredFrom
(
sessionStruct
.
getRegisteredFrom
()).
tenantId
(
user
.
getTenantId
()).
build
()));
...
...
@@ -142,12 +144,25 @@ public class SessionServiceImpl implements ISessionService {
}
else
if
(
properties
.
getTenantId
().
equals
(
0
)
||
TenantUtil
.
TENANT_DEFAULT
.
equals
(
properties
.
getTenantId
()))
{
return
Constants
.
Session
.
USER_SESSION_ID_CACHE
+
":"
+
userId
+
":"
+
properties
.
getMerchantName
()
+
":"
+
properties
.
getCreatedFrom
();
}
else
{
return
Constants
.
Session
.
USER_SESSION_ID_CACHE
+
":"
+
userId
+
":"
+
properties
.
getMerchantName
()
+
":"
+
properties
.
getCreatedFrom
()
+
":"
+
properties
.
getTenantId
();
Integer
key
;
if
(
tenantId
!=
null
)
{
key
=
tenantId
;
}
else
{
key
=
properties
.
getTenantId
();
}
return
Constants
.
Session
.
USER_SESSION_ID_CACHE
+
":"
+
userId
+
":"
+
properties
.
getMerchantName
()
+
":"
+
properties
.
getCreatedFrom
()
+
":"
+
key
;
}
}
private
String
findSessionValueBySessionId
(
String
sessionId
,
Integer
tenantId
)
{
String
result
=
stringRedisTemplate
.
opsForValue
().
get
(
Constants
.
Session
.
USER_SESSION_CACHE
+
sessionId
);
String
tokenKey2
;
if
(
tenantId
==
null
||
UserConstant
.
defaultTenantId
.
equals
(
tenantId
))
{
tokenKey2
=
Constants
.
Session
.
USER_SESSION_CACHE
+
sessionId
;
}
else
{
tokenKey2
=
Constants
.
Session
.
USER_SESSION_CACHE
+
tenantId
+
":"
+
sessionId
;
}
String
result
=
stringRedisTemplate
.
opsForValue
().
get
(
tokenKey2
);
return
StringUtils
.
defaultString
(
result
,
""
);
}
...
...
@@ -190,24 +205,31 @@ public class SessionServiceImpl implements ISessionService {
sessionValue
=
new
SessionValue
();
}
if
(
sessionValue
.
getLoginProperties
()==
null
)
{
if
(
sessionValue
.
getLoginProperties
()
==
null
)
{
sessionValue
.
setLoginProperties
(
new
LoginProperties
());
}
LoginProperties
loginProperties
=
sessionValue
.
getLoginProperties
();
LoginProperties
loginProperties
=
sessionValue
.
getLoginProperties
();
loginProperties
.
setTenantId
(
tenantId
);
sessionValue
.
setLoginProperties
(
loginProperties
);
sessionValue
.
setLastAccessTime
(
current
);
String
json
=
JSON
.
toJSONString
(
sessionValue
);
stringRedisTemplate
.
opsForValue
().
set
(
Constants
.
Session
.
USER_SESSION_CACHE
+
token
,
json
,
String
key
;
if
(
tenantId
==
null
||
UserConstant
.
defaultTenantId
.
equals
(
tenantId
))
{
key
=
Constants
.
Session
.
USER_SESSION_CACHE
+
token
;
}
else
{
key
=
Constants
.
Session
.
USER_SESSION_CACHE
+
tenantId
+
":"
+
token
;
}
stringRedisTemplate
.
opsForValue
().
set
(
key
,
json
,
time
,
TimeUnit
.
SECONDS
);
if
(
sessionValue
.
getUser
()!=
null
)
{
String
k
ey
=
generateLoginPropertiesKey
(
sessionValue
.
getUser
().
getId
(),
sessionValue
.
getLoginProperties
(),
tenantId
);
stringRedisTemplate
.
opsForValue
().
set
(
k
ey
,
token
,
time
,
TimeUnit
.
SECONDS
);
if
(
sessionValue
.
getUser
()
!=
null
)
{
String
generateLoginPropertiesK
ey
=
generateLoginPropertiesKey
(
sessionValue
.
getUser
().
getId
(),
sessionValue
.
getLoginProperties
(),
tenantId
);
stringRedisTemplate
.
opsForValue
().
set
(
generateLoginPropertiesK
ey
,
token
,
time
,
TimeUnit
.
SECONDS
);
log
.
info
(
"[Session生命期延续],token:{},有效期:[24Hour]"
,
token
);
setUserIdTokenKeys
(
sessionValue
.
getUser
().
getId
(),
k
ey
,
tenantId
);
setUserIdTokenKeys
(
sessionValue
.
getUser
().
getId
(),
generateLoginPropertiesK
ey
,
tenantId
);
}
}
...
...
@@ -272,7 +294,13 @@ public class SessionServiceImpl implements ISessionService {
}
private
String
getUserTokenKey
(
String
token
,
Integer
tenantId
)
{
return
Constants
.
Session
.
USER_SESSION_CACHE
+
token
;
String
tokenKey2
;
if
(
tenantId
==
null
||
UserConstant
.
defaultTenantId
.
equals
(
tenantId
))
{
tokenKey2
=
Constants
.
Session
.
USER_SESSION_CACHE
+
token
;
}
else
{
tokenKey2
=
Constants
.
Session
.
USER_SESSION_CACHE
+
tenantId
+
":"
+
token
;
}
return
tokenKey2
;
}
/**
...
...
@@ -330,8 +358,14 @@ public class SessionServiceImpl implements ISessionService {
return
;
}
stringRedisTemplate
.
delete
(
Constants
.
Session
.
USER_SESSION_CACHE
+
sessionStruct
.
getSid
());
String
tokenKey2
;
if
(
UserConstant
.
defaultTenantId
.
equals
(
tenantId
))
{
tokenKey2
=
Constants
.
Session
.
USER_SESSION_CACHE
+
sessionStruct
.
getSid
();
}
else
{
tokenKey2
=
Constants
.
Session
.
USER_SESSION_CACHE
+
tenantId
+
":"
+
sessionStruct
.
getSid
();
}
stringRedisTemplate
.
delete
(
tokenKey2
);
String
key
=
generateLoginPropertiesKey
(
user
.
getId
(),
values
.
getLoginProperties
(),
tenantId
);
...
...
@@ -347,7 +381,15 @@ public class SessionServiceImpl implements ISessionService {
Timestamp
current
=
new
Timestamp
(
System
.
currentTimeMillis
());
sessionValue
.
setLastAccessTime
(
current
);
String
json
=
JSON
.
toJSONString
(
sessionValue
);
stringRedisTemplate
.
opsForValue
().
set
(
Constants
.
Session
.
USER_SESSION_CACHE
+
token
,
json
,
String
tokenKey2
;
if
(
UserConstant
.
defaultTenantId
.
equals
(
tenantId
))
{
tokenKey2
=
Constants
.
Session
.
USER_SESSION_CACHE
+
token
;
}
else
{
tokenKey2
=
Constants
.
Session
.
USER_SESSION_CACHE
+
tenantId
+
":"
+
token
;
}
stringRedisTemplate
.
opsForValue
().
set
(
tokenKey2
,
json
,
Constants
.
Session
.
SESSION_VALID_TIME
,
TimeUnit
.
SECONDS
);
String
key
=
generateLoginPropertiesKey
(
sessionValue
.
getUser
().
getId
(),
sessionValue
.
getLoginProperties
(),
tenantId
);
stringRedisTemplate
.
opsForValue
().
set
(
key
,
token
,
expire
,
TimeUnit
.
SECONDS
);
...
...
src/main/java/cn/quantgroup/xyqb/session/XyqbSessionContextHolder.java
View file @
4af777a0
package
cn
.
quantgroup
.
xyqb
.
session
;
import
cn.quantgroup.xyqb.Constants
;
import
cn.quantgroup.xyqb.constant.UserConstant
;
import
cn.quantgroup.xyqb.model.LoginProperties
;
import
cn.quantgroup.xyqb.model.session.SessionStruct
;
import
cn.quantgroup.xyqb.model.session.SessionValue
;
...
...
@@ -124,7 +125,12 @@ public class XyqbSessionContextHolder {
public
static
SessionStruct
getXSessionFromRedis
()
{
HttpServletRequest
request
=
((
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
()).
getRequest
();
String
token
=
request
.
getHeader
(
Constants
.
X_AUTH_TOKEN
);
return
getXSessionFromRedis
(
token
);
int
tenantId
=
UserConstant
.
defaultTenantId
;
String
tenantIdString
=
request
.
getHeader
(
Constants
.
X_AUTH_TENANT
);
if
(
StringUtils
.
isNotEmpty
(
tenantIdString
)){
tenantId
=
Integer
.
parseInt
(
tenantIdString
);
}
return
getXSessionFromRedis
(
token
,
tenantId
);
}
public
static
String
getXSessionFromTenantRedis
()
{
...
...
@@ -132,8 +138,14 @@ public class XyqbSessionContextHolder {
return
request
.
getHeader
(
Constants
.
X_AUTH_TENANT
);
}
public
static
SessionStruct
getXSessionFromRedis
(
String
token
)
{
String
linkToken
=
Constants
.
Session
.
USER_SESSION_CACHE
+
token
;
public
static
SessionStruct
getXSessionFromRedis
(
String
token
,
Integer
tenantId
)
{
String
linkToken
;
if
(
UserConstant
.
defaultTenantId
.
equals
(
tenantId
))
{
linkToken
=
Constants
.
Session
.
USER_SESSION_CACHE
+
token
;
}
else
{
linkToken
=
Constants
.
Session
.
USER_SESSION_CACHE
+
tenantId
+
":"
+
token
;
}
String
result
=
redisTemplate
.
opsForValue
().
get
(
linkToken
);
if
(
StringUtils
.
isEmpty
(
result
))
{
log
.
warn
(
"[XyqbSessionContextHolder][getXSessionFromRedis] session data 未找到:Tokekn:{},linkTokekn:{},sessionValue:{}"
,
token
,
linkToken
,
result
);
...
...
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