Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
brandkbs2
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
shenjunjie
brandkbs2
Commits
58ed5aa7
Commit
58ed5aa7
authored
Dec 05, 2024
by
陈健智
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
单账号登录限制
parent
2782ec85
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
1 deletions
+45
-1
src/main/java/com/zhiwei/brandkbs2/auth/AuthAspect.java
+20
-0
src/main/java/com/zhiwei/brandkbs2/common/RedisKeyPrefix.java
+6
-0
src/main/java/com/zhiwei/brandkbs2/service/impl/UserServiceImpl.java
+19
-1
No files found.
src/main/java/com/zhiwei/brandkbs2/auth/AuthAspect.java
View file @
58ed5aa7
...
...
@@ -2,11 +2,13 @@ package com.zhiwei.brandkbs2.auth;
import
com.alibaba.fastjson.JSON
;
import
com.zhiwei.brandkbs2.common.GenericAttribute
;
import
com.zhiwei.brandkbs2.common.RedisKeyPrefix
;
import
com.zhiwei.brandkbs2.enmus.RoleEnum
;
import
com.zhiwei.brandkbs2.model.CommonCodeEnum
;
import
com.zhiwei.brandkbs2.model.ResponseResult
;
import
com.zhiwei.brandkbs2.pojo.UserInfo
;
import
com.zhiwei.brandkbs2.service.UserService
;
import
com.zhiwei.brandkbs2.util.RedisUtil
;
import
com.zhiwei.brandkbs2.util.Tools
;
import
com.zhiwei.middleware.auth.util.JwtUtil
;
import
org.apache.logging.log4j.LogManager
;
...
...
@@ -17,6 +19,7 @@ import org.aspectj.lang.annotation.Around;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.annotation.Pointcut
;
import
org.aspectj.lang.reflect.MethodSignature
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.core.annotation.Order
;
import
org.springframework.stereotype.Component
;
...
...
@@ -26,10 +29,12 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import
javax.annotation.Resource
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpSession
;
import
java.io.PrintWriter
;
import
java.lang.reflect.Method
;
import
java.util.Collections
;
import
java.util.Map
;
import
java.util.Objects
;
/**
* @author sjj
...
...
@@ -49,6 +54,9 @@ public class AuthAspect {
@Resource
(
name
=
"userServiceImpl"
)
private
UserService
UserService
;
@Autowired
private
RedisUtil
redisUtil
;
@Pointcut
(
"execution(com.zhiwei.brandkbs2.model.ResponseResult com.zhiwei.brandkbs2.controller..*.*(..))"
)
// @Pointcut("within(com.zhiwei.brandkbs2.controller..*)")
public
void
auth
()
{
...
...
@@ -82,6 +90,18 @@ public class AuthAspect {
log
.
error
(
"token解析异常,uri:{},methodName:{},token:{}"
,
uri
,
methodName
,
token
);
}
else
{
String
uid
=
tokenInfo
.
get
(
GenericAttribute
.
USER_ID
).
toString
();
// session校验
HttpSession
session
=
request
.
getSession
();
String
sessionId
=
session
.
getId
();
String
cacheSessionId
=
redisUtil
.
get
(
RedisKeyPrefix
.
userSessionKey
(
uid
));
// log.info("userId:{},sessionId:{},cacheSessionId:{}", uid, sessionId, cacheSessionId);
if
(
null
==
cacheSessionId
)
{
redisUtil
.
set
(
RedisKeyPrefix
.
userSessionKey
(
uid
),
sessionId
);
}
// 已登录状态
if
(
null
!=
cacheSessionId
&&
!
Objects
.
equals
(
sessionId
,
cacheSessionId
)){
return
joinPoint
.
proceed
();
}
UserInfo
userInfo
=
UserService
.
queryUserInfo
(
uid
,
request
.
getHeader
(
"pid"
));
if
(
null
==
userInfo
)
{
userInfo
=
new
UserInfo
().
setUserId
(
uid
).
setProjectId
(
request
.
getHeader
(
"pid"
));
...
...
src/main/java/com/zhiwei/brandkbs2/common/RedisKeyPrefix.java
View file @
58ed5aa7
...
...
@@ -128,6 +128,8 @@ public class RedisKeyPrefix {
public
static
final
String
AI_SEARCH_QUESTION
=
"BRANDKBS:AI:SEARCH:QUESTION:"
;
public
static
final
String
USER_SESSION
=
"BRANDKBS:USER:SESSION:"
;
public
static
String
projectWarnHotTopKeyAll
(
String
projectId
,
String
type
)
{
return
RedisKeyPrefix
.
generateRedisKey
(
RedisKeyPrefix
.
PROJECT_WARN_HOT_TOP
,
projectId
,
Tools
.
concat
(
type
,
"*"
));
}
...
...
@@ -168,6 +170,10 @@ public class RedisKeyPrefix {
return
RedisKeyPrefix
.
generateRedisKey
(
RedisKeyPrefix
.
HOT_SUPPLEMENT_WORD
,
projectId
);
}
public
static
String
userSessionKey
(
String
userId
)
{
return
RedisKeyPrefix
.
generateRedisKey
(
RedisKeyPrefix
.
USER_SESSION
,
userId
);
}
private
static
String
generateRedisKey
(
String
...
keys
)
{
Objects
.
requireNonNull
(
keys
);
boolean
contains
=
keys
[
0
].
endsWith
(
":"
);
...
...
src/main/java/com/zhiwei/brandkbs2/service/impl/UserServiceImpl.java
View file @
58ed5aa7
...
...
@@ -3,7 +3,9 @@ package com.zhiwei.brandkbs2.service.impl;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.zhiwei.brandkbs2.auth.UserThreadLocal
;
import
com.zhiwei.brandkbs2.common.GenericAttribute
;
import
com.zhiwei.brandkbs2.common.GlobalPojo
;
import
com.zhiwei.brandkbs2.common.RedisKeyPrefix
;
import
com.zhiwei.brandkbs2.config.Constant
;
import
com.zhiwei.brandkbs2.dao.ProjectDao
;
import
com.zhiwei.brandkbs2.dao.UserDao
;
...
...
@@ -24,8 +26,10 @@ import com.zhiwei.brandkbs2.pojo.vo.PageVO;
import
com.zhiwei.brandkbs2.service.UserCenterService
;
import
com.zhiwei.brandkbs2.service.UserService
;
import
com.zhiwei.brandkbs2.util.MongoUtil
;
import
com.zhiwei.brandkbs2.util.RedisUtil
;
import
com.zhiwei.brandkbs2.util.Tools
;
import
com.zhiwei.middleware.auth.pojo.CenterUser
;
import
com.zhiwei.middleware.auth.util.JwtUtil
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.Logger
;
...
...
@@ -38,8 +42,11 @@ import org.springframework.http.HttpEntity;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.client.RestTemplate
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
javax.annotation.Resource
;
import
javax.servlet.http.HttpServletRequest
;
import
java.util.*
;
import
java.util.concurrent.atomic.AtomicBoolean
;
import
java.util.stream.Collectors
;
...
...
@@ -54,6 +61,9 @@ import java.util.stream.Collectors;
public
class
UserServiceImpl
implements
UserService
{
public
static
final
Logger
log
=
LogManager
.
getLogger
(
UserServiceImpl
.
class
);
@Value
(
"${jwt.key}"
)
private
String
jwtKey
;
@Resource
(
name
=
"userDao"
)
private
UserDao
userDao
;
...
...
@@ -87,6 +97,9 @@ public class UserServiceImpl implements UserService {
@Autowired
private
RestTemplate
restTemplate
;
@Autowired
private
RedisUtil
redisUtil
;
@Override
public
UserInfo
login
()
{
String
uid
=
UserThreadLocal
.
getUserId
();
...
...
@@ -358,7 +371,12 @@ public class UserServiceImpl implements UserService {
@Override
public
boolean
checkUserRoles
()
{
User
user
=
userDao
.
findOneById
(
UserThreadLocal
.
getUserId
());
ServletRequestAttributes
servletRequestAttributes
=
((
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
());
HttpServletRequest
request
=
servletRequestAttributes
.
getRequest
();
Map
<
String
,
Object
>
map
=
JwtUtil
.
unsign
(
request
.
getHeader
(
jwtKey
),
Map
.
class
);
String
userId
=
map
.
get
(
GenericAttribute
.
USER_ID
).
toString
();
redisUtil
.
set
(
RedisKeyPrefix
.
userSessionKey
(
userId
),
request
.
getSession
().
getId
());
User
user
=
userDao
.
findOneById
(
userId
);
if
(
null
==
user
)
{
return
false
;
}
...
...
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