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
4cbc8afb
Commit
4cbc8afb
authored
Jan 05, 2022
by
李健华
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
电商金融用户中心token互换
parent
738586df
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
107 additions
and
2 deletions
+107
-2
UserController.java
...n/quantgroup/xyqb/controller/external/UserController.java
+65
-0
TokenExchange.java
src/main/java/cn/quantgroup/xyqb/model/TokenExchange.java
+27
-0
ISessionService.java
...a/cn/quantgroup/xyqb/service/session/ISessionService.java
+1
-0
SessionServiceImpl.java
...ntgroup/xyqb/service/session/impl/SessionServiceImpl.java
+13
-1
XyqbSessionContextHolder.java
.../cn/quantgroup/xyqb/session/XyqbSessionContextHolder.java
+1
-1
No files found.
src/main/java/cn/quantgroup/xyqb/controller/external/UserController.java
View file @
4cbc8afb
...
@@ -15,6 +15,8 @@ import cn.quantgroup.xyqb.exception.UserNotExistException;
...
@@ -15,6 +15,8 @@ import cn.quantgroup.xyqb.exception.UserNotExistException;
import
cn.quantgroup.xyqb.exception.VerificationCodeErrorException
;
import
cn.quantgroup.xyqb.exception.VerificationCodeErrorException
;
import
cn.quantgroup.xyqb.model.*
;
import
cn.quantgroup.xyqb.model.*
;
import
cn.quantgroup.xyqb.model.session.SessionStruct
;
import
cn.quantgroup.xyqb.model.session.SessionStruct
;
import
cn.quantgroup.xyqb.model.session.SessionValue
;
import
cn.quantgroup.xyqb.service.http.IHttpService
;
import
cn.quantgroup.xyqb.service.merchant.IMerchantService
;
import
cn.quantgroup.xyqb.service.merchant.IMerchantService
;
import
cn.quantgroup.xyqb.service.register.IUserRegisterService
;
import
cn.quantgroup.xyqb.service.register.IUserRegisterService
;
import
cn.quantgroup.xyqb.service.session.ISessionService
;
import
cn.quantgroup.xyqb.service.session.ISessionService
;
...
@@ -27,12 +29,14 @@ import cn.quantgroup.xyqb.util.PasswordUtil;
...
@@ -27,12 +29,14 @@ import cn.quantgroup.xyqb.util.PasswordUtil;
import
cn.quantgroup.xyqb.util.TenantUtil
;
import
cn.quantgroup.xyqb.util.TenantUtil
;
import
cn.quantgroup.xyqb.util.ValidationUtil
;
import
cn.quantgroup.xyqb.util.ValidationUtil
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
io.swagger.annotations.ApiOperation
;
import
io.swagger.annotations.ApiOperation
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.codec.binary.Base64
;
import
org.apache.commons.codec.binary.Base64
;
import
org.apache.commons.lang.StringUtils
;
import
org.apache.commons.lang.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestMethod
;
...
@@ -82,6 +86,15 @@ public class UserController implements IBaseController {
...
@@ -82,6 +86,15 @@ public class UserController implements IBaseController {
@Autowired
@Autowired
private
ITenantService
tenantService
;
private
ITenantService
tenantService
;
@Value
(
"${xyqb.user.service.host}"
)
private
String
userHost
;
@Value
(
"${token.prefix}"
)
private
String
prefix
;
@Autowired
private
IHttpService
httpService
;
/**
/**
* 登录(账号 + 密码)
* 登录(账号 + 密码)
...
@@ -479,6 +492,58 @@ public class UserController implements IBaseController {
...
@@ -479,6 +492,58 @@ public class UserController implements IBaseController {
return
JsonResult
.
buildSuccessResult
(
null
,
result
);
return
JsonResult
.
buildSuccessResult
(
null
,
result
);
}
}
/**
* token 交换
*
*/
@RequestMapping
(
"/tokenExchange"
)
public
TokenExchange
tokenExchange
(
@RequestParam
String
token
,
@RequestParam
(
required
=
false
)
Integer
tenantId
)
{
TokenExchange
tokenExchange
=
new
TokenExchange
();
if
(
StringUtils
.
isEmpty
(
token
))
{
return
null
;
}
if
(
token
.
contains
(
Constants
.
TOKEN_MASTER
))
{
return
null
;
}
SessionStruct
sessionStruct
=
XyqbSessionContextHolder
.
getXSessionFromRedis
(
token
);
if
(
sessionStruct
==
null
||
sessionStruct
.
getValues
()
==
null
)
{
return
null
;
}
User
user
=
sessionStruct
.
getValues
().
getUser
();
tokenExchange
.
setLoginProperties
(
JSONObject
.
toJSONString
(
sessionStruct
.
getValues
().
getLoginProperties
()));
tokenExchange
.
setUserId
(
user
.
getId
());
return
tokenExchange
;
}
/**
* 交换token
* @param token
* @return
*/
private
void
findTokenExchange
(
String
token
)
{
// 请求其他系统信息
HashMap
<
String
,
String
>
parameters
=
new
HashMap
<>();
parameters
.
put
(
"token"
,
token
);
String
resultStr
=
httpService
.
post
(
userHost
+
"/user/tokenExchange"
,
parameters
);
// 如果有返回值
if
(!
resultStr
.
isEmpty
())
{
TokenExchange
tokenExchange
=
JSONObject
.
parseObject
(
resultStr
,
TokenExchange
.
class
);
SessionValue
sessionValue
=
new
SessionValue
();
tokenExchange
.
setUserId
(
65172086L
);
User
user
=
userService
.
findById
(
tokenExchange
.
getUserId
());
LoginProperties
loginProperties
=
JSONObject
.
parseObject
(
tokenExchange
.
getLoginProperties
(),
LoginProperties
.
class
);
// 根据返回值生成token, 返回值包含user:session:token的值
sessionValue
.
setUser
(
user
);
sessionValue
.
setLoginProperties
(
loginProperties
);
sessionService
.
persistSessionExchange
(
token
,
sessionValue
);
}
}
/**
/**
* 用户中心首页,显示用户头像、昵称、姓名
* 用户中心首页,显示用户头像、昵称、姓名
*
*
...
...
src/main/java/cn/quantgroup/xyqb/model/TokenExchange.java
0 → 100644
View file @
4cbc8afb
package
cn
.
quantgroup
.
xyqb
.
model
;
import
lombok.*
;
import
java.io.Serializable
;
/**
* @author mengfan.feng
* @time 2015-10-27 16:15
*/
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Builder
public
class
TokenExchange
implements
Serializable
{
private
static
final
long
serialVersionUID
=
-
1L
;
private
Long
userId
;
private
String
uuid
;
private
String
loginProperties
;
}
src/main/java/cn/quantgroup/xyqb/service/session/ISessionService.java
View file @
4cbc8afb
...
@@ -42,4 +42,5 @@ public interface ISessionService {
...
@@ -42,4 +42,5 @@ public interface ISessionService {
*/
*/
void
deleteSession
(
String
token
);
void
deleteSession
(
String
token
);
void
persistSessionExchange
(
String
token
,
SessionValue
sessionValue
);
}
}
src/main/java/cn/quantgroup/xyqb/service/session/impl/SessionServiceImpl.java
View file @
4cbc8afb
...
@@ -123,7 +123,7 @@ public class SessionServiceImpl implements ISessionService {
...
@@ -123,7 +123,7 @@ public class SessionServiceImpl implements ISessionService {
Timestamp
now
=
new
Timestamp
(
System
.
currentTimeMillis
());
Timestamp
now
=
new
Timestamp
(
System
.
currentTimeMillis
());
SessionStruct
sessionStruct
=
new
SessionStruct
();
SessionStruct
sessionStruct
=
new
SessionStruct
();
SessionValue
sessionValue
=
new
SessionValue
();
SessionValue
sessionValue
=
new
SessionValue
();
sessionStruct
.
setSid
(
UUID
.
randomUUID
().
toString
());
sessionStruct
.
setSid
(
"yxm-"
+
UUID
.
randomUUID
().
toString
());
sessionValue
.
setCreatedAt
(
now
);
sessionValue
.
setCreatedAt
(
now
);
sessionValue
.
setLastAccessTime
(
now
);
sessionValue
.
setLastAccessTime
(
now
);
sessionValue
.
setUser
(
user
);
sessionValue
.
setUser
(
user
);
...
@@ -282,6 +282,18 @@ public class SessionServiceImpl implements ISessionService {
...
@@ -282,6 +282,18 @@ public class SessionServiceImpl implements ISessionService {
}
}
}
}
@Override
public
void
persistSessionExchange
(
String
token
,
SessionValue
sessionValue
)
{
Timestamp
current
=
new
Timestamp
(
System
.
currentTimeMillis
());
sessionValue
.
setLastAccessTime
(
current
);
String
json
=
JSON
.
toJSONString
(
sessionValue
);
stringRedisTemplate
.
opsForValue
().
set
(
Constants
.
Session
.
USER_SESSION_CACHE
+
token
,
json
,
Constants
.
Session
.
SESSION_VALID_TIME
,
TimeUnit
.
SECONDS
);
String
key
=
generateLoginPropertiesKey
(
sessionValue
.
getUser
().
getId
(),
sessionValue
.
getLoginProperties
());
stringRedisTemplate
.
opsForValue
().
set
(
key
,
token
,
Constants
.
Session
.
SESSION_VALID_TIME
,
TimeUnit
.
SECONDS
);
setUserIdTokenKeys
(
sessionValue
.
getUser
().
getId
(),
key
);
}
/**
/**
* 获取用户的会话缓存Set的Redis-Key
* 获取用户的会话缓存Set的Redis-Key
*
*
...
...
src/main/java/cn/quantgroup/xyqb/session/XyqbSessionContextHolder.java
View file @
4cbc8afb
...
@@ -52,7 +52,7 @@ public class XyqbSessionContextHolder {
...
@@ -52,7 +52,7 @@ public class XyqbSessionContextHolder {
}
}
public
static
SessionStruct
getXSessionFromRedis
(
String
token
)
{
public
static
SessionStruct
getXSessionFromRedis
(
String
token
)
{
if
(
StringUtils
.
length
(
token
)
!=
Constants
.
TOKEN_LENGTH
||
Objects
.
isNull
(
redisTemplate
))
{
if
(
Objects
.
isNull
(
redisTemplate
))
{
return
null
;
return
null
;
}
}
String
result
=
redisTemplate
.
opsForValue
().
get
(
Constants
.
Session
.
USER_SESSION_CACHE
+
token
);
String
result
=
redisTemplate
.
opsForValue
().
get
(
Constants
.
Session
.
USER_SESSION_CACHE
+
token
);
...
...
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