Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cloud-backend
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
framework
cloud-backend
Commits
1448fccb
Commit
1448fccb
authored
Jul 10, 2021
by
袁伟铭
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1.0.0
parent
b85ecdb6
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
40 additions
and
30 deletions
+40
-30
common-utils/src/main/java/com/zq/common/config/interceptor/UserInfoInterceptor.java
+7
-3
common-utils/src/main/java/com/zq/common/config/limit/LimitAspect.java
+6
-4
common-utils/src/main/java/com/zq/common/config/limit/LimitType.java
+3
-1
common-utils/src/main/java/com/zq/common/config/redis/BaseCacheKeys.java
+12
-0
common-utils/src/main/java/com/zq/common/context/ContextUtils.java
+2
-2
user-server/src/main/java/com/zq/user/manager/UserCacheKeys.java
+8
-19
user-server/src/main/java/com/zq/user/service/UserService.java
+2
-1
No files found.
common-utils/src/main/java/com/zq/common/config/interceptor/UserInfoInterceptor.java
View file @
1448fccb
...
...
@@ -4,6 +4,7 @@ import com.zq.common.config.redis.BaseCacheKeys;
import
com.zq.common.config.redis.RedisUtils
;
import
com.zq.common.config.security.SecurityProperties
;
import
com.zq.common.context.ContextUtils
;
import
com.zq.common.utils.AssertUtils
;
import
com.zq.common.vo.ApiTokenVo
;
import
lombok.RequiredArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
...
...
@@ -30,9 +31,12 @@ public class UserInfoInterceptor extends HandlerInterceptorAdapter {
public
boolean
preHandle
(
HttpServletRequest
request
,
HttpServletResponse
response
,
Object
handler
)
throws
Exception
{
if
(
request
.
getRequestURI
().
contains
(
"/app/"
))
{
String
token
=
getToken
(
request
);
log
.
info
(
">> [UserInfo token] {}"
,
token
);
ApiTokenVo
tokenVo
=
redisUtils
.
getObj
(
BaseCacheKeys
.
appTokenKey
(
token
),
ApiTokenVo
.
class
);
ContextUtils
.
setUserContext
(
tokenVo
);
log
.
debug
(
">> [UserInfo token] {}"
,
token
);
if
(
StringUtils
.
isNotBlank
(
token
))
{
ApiTokenVo
tokenVo
=
redisUtils
.
getObj
(
BaseCacheKeys
.
appTokenKey
(
token
),
ApiTokenVo
.
class
);
AssertUtils
.
notNull
(
tokenVo
,
"登录信息过期"
);
ContextUtils
.
setUserContext
(
tokenVo
);
}
}
return
true
;
}
...
...
common-utils/src/main/java/com/zq/common/config/limit/LimitAspect.java
View file @
1448fccb
...
...
@@ -16,6 +16,7 @@
package
com
.
zq
.
common
.
config
.
limit
;
import
com.zq.common.annotation.Limit
;
import
com.zq.common.context.ContextUtils
;
import
com.zq.common.http.HttpRequestUtils
;
import
com.zq.common.utils.AssertUtils
;
import
org.apache.commons.lang3.StringUtils
;
...
...
@@ -66,16 +67,17 @@ public class LimitAspect {
Limit
limit
=
signatureMethod
.
getAnnotation
(
Limit
.
class
);
LimitType
limitType
=
limit
.
limitType
();
String
key
=
limit
.
key
();
// 构建key
if
(
StringUtils
.
isBlank
(
key
))
{
if
(
limitType
==
LimitType
.
IP
)
{
key
=
HttpRequestUtils
.
getClientIp
(
request
);
}
else
if
(
limitType
==
LimitType
.
USER_ID
)
{
Long
userId
=
ContextUtils
.
getUserUserId
();
key
=
userId
!=
null
?
userId
.
toString
()
:
HttpRequestUtils
.
getClientIp
(
request
);
}
else
{
// 获取方法名
key
=
signatureMethod
.
getName
();
if
(
"sendCode"
.
equals
(
key
))
{
// 获取方法的第一个参数
key
=
(
String
)
joinPoint
.
getArgs
()[
0
];
}
}
}
...
...
common-utils/src/main/java/com/zq/common/config/limit/LimitType.java
View file @
1448fccb
...
...
@@ -24,6 +24,8 @@ package com.zq.common.config.limit;
public
enum
LimitType
{
// 默认
CUSTOMER
,
// by ip addr
// by ip USER_ID
USER_ID
,
// by ip address
IP
}
common-utils/src/main/java/com/zq/common/config/redis/BaseCacheKeys.java
View file @
1448fccb
...
...
@@ -12,6 +12,8 @@ public abstract class BaseCacheKeys {
private
static
final
String
APP_TOKEN
=
PREFIX
+
"app-token."
;
private
static
final
String
ADMIN_TOKEN
=
PREFIX
+
"admin-token."
;
/**
* 构建app端用户token的缓存key
*
...
...
@@ -22,4 +24,14 @@ public abstract class BaseCacheKeys {
return
APP_TOKEN
+
token
;
}
/**
* 构建admin端用户token的缓存key
*
* @param token admin登陆后的token
* @return
*/
public
static
String
adminTokenKey
(
String
token
)
{
return
ADMIN_TOKEN
+
token
;
}
}
common-utils/src/main/java/com/zq/common/context/ContextUtils.java
View file @
1448fccb
...
...
@@ -29,7 +29,7 @@ public class ContextUtils {
public
static
Long
getUserUserId
()
{
ApiTokenVo
apiTokenVo
=
ThreadContext
.
get
(
APP_TOKEN_CONTEXT_KEY
);
return
apiTokenVo
.
getUserId
();
return
apiTokenVo
==
null
?
null
:
apiTokenVo
.
getUserId
();
}
public
static
void
setAdminContext
(
OnlineUserDto
onlineUserDto
)
{
...
...
@@ -44,7 +44,7 @@ public class ContextUtils {
public
static
Long
getAdminUserId
()
{
OnlineUserDto
userDto
=
ThreadContext
.
get
(
ADMIN_TOKEN_CONTEXT_KEY
);
return
userDto
.
getUserId
();
return
userDto
==
null
?
null
:
userDto
.
getUserId
();
}
}
...
...
user-server/src/main/java/com/zq/user/manager/UserCacheKeys.java
View file @
1448fccb
package
com
.
zq
.
user
.
manager
;
public
abstract
class
UserCacheKeys
{
import
com.zq.common.config.redis.BaseCacheKeys
;
public
static
final
long
APP_TOKEN_EXPIRE_MINUTES
=
60
*
24
*
2
;
/**
* @author wilmiam
* @since 2021-07-10 16:38
*/
public
class
UserCacheKeys
extends
BaseCacheKeys
{
public
static
final
String
PREFIX
=
"wine."
;
public
static
final
long
APP_TOKEN_EXPIRE_MINUTES
=
60
*
24
*
2
;
private
static
final
String
AUTH_CODE
=
PREFIX
+
"auth-code."
;
private
static
final
String
APP_TOKEN
=
PREFIX
+
"app-token."
;
private
static
final
String
ADMIN_TOKEN
=
PREFIX
+
"admin-token."
;
private
static
final
String
LIVE_APP_TOKEN
=
PREFIX
+
"live-app-token."
;
private
static
final
String
LIVE_ADMIN_TOKEN
=
PREFIX
+
"live-admin-token."
;
private
static
final
String
LIVE_APP_TOKEN
=
PREFIX
+
"live-app-token."
;
/**
* 构建手机验证码的缓存key
*
...
...
@@ -27,16 +27,6 @@ public abstract class UserCacheKeys {
}
/**
* 构建app端用户token的缓存key
*
* @param token app登陆后的token
* @return
*/
public
static
String
appTokenKey
(
String
token
)
{
return
APP_TOKEN
+
token
;
}
/**
* 用户当前apptoken的缓存key
*
* @param memberId
...
...
@@ -46,5 +36,4 @@ public abstract class UserCacheKeys {
return
LIVE_APP_TOKEN
+
memberId
;
}
}
user-server/src/main/java/com/zq/user/service/UserService.java
View file @
1448fccb
...
...
@@ -128,7 +128,8 @@ public class UserService {
redisUtils
.
setObj
(
UserCacheKeys
.
appTokenKey
(
token
),
tokenVo
,
UserCacheKeys
.
APP_TOKEN_EXPIRE_MINUTES
);
// 重新登录删除前一个token实现单点登录
redisUtils
.
deleteStr
(
UserCacheKeys
.
liveAppTokenKey
(
appUser
.
getId
()));
String
cacheToken
=
redisUtils
.
getStr
(
UserCacheKeys
.
liveAppTokenKey
(
appUser
.
getId
()));
redisUtils
.
deleteObj
(
UserCacheKeys
.
appTokenKey
(
cacheToken
));
// 限制同一时间同一帐号只能在一个设备上登录
redisUtils
.
setStr
(
UserCacheKeys
.
liveAppTokenKey
(
appUser
.
getId
()),
token
,
UserCacheKeys
.
APP_TOKEN_EXPIRE_MINUTES
);
...
...
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