Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
amos-boot-biz
Project
Project
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
项目统一框架
amos-boot-biz
Commits
6b5ea7b0
Commit
6b5ea7b0
authored
Jun 09, 2021
by
taabe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
+)添加控制器切面:将用户登录信息缓存
parent
2c921e4b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
178 additions
and
0 deletions
+178
-0
GlobalCache.java
...a/com/yeejoin/amos/boot/biz/common/utils/GlobalCache.java
+26
-0
application-dev.properties
...-jcs-system/src/main/resources/application-dev.properties
+3
-0
ControllerAop.java
...m/yeejoin/amos/boot/module/jcs/biz/aop/ControllerAop.java
+149
-0
No files found.
amos-boot-biz-common/src/main/java/com/yeejoin/amos/boot/biz/common/utils/GlobalCache.java
0 → 100644
View file @
6b5ea7b0
package
com
.
yeejoin
.
amos
.
boot
.
biz
.
common
.
utils
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
/**
* @Description: 全局单机缓存
* @Author: duanwei
* @Date: 2020/6/30
*/
public
class
GlobalCache
{
/**
* 全局请求头
*/
public
static
Map
<
String
,
String
>
header
=
new
ConcurrentHashMap
<>();
/**
* 依赖参数容器
*/
public
static
Map
<
String
,
String
>
paramMap
=
new
ConcurrentHashMap
<>(
1000
);
}
amos-boot-jcs-system/src/main/resources/application-dev.properties
View file @
6b5ea7b0
...
@@ -23,4 +23,7 @@ spring.redis.expire.time=300
...
@@ -23,4 +23,7 @@ spring.redis.expire.time=300
security.systemctl.name
=
AMOS-API-SYSTEMCTL
security.systemctl.name
=
AMOS-API-SYSTEMCTL
# 区域选择存放redis失效时间一个月
redis_region_time_second
=
2592000
amos-boot-module/amos-boot-module-biz/amos-boot-module-jcs-biz/src/main/java/com/yeejoin/amos/boot/module/jcs/biz/aop/ControllerAop.java
0 → 100644
View file @
6b5ea7b0
package
com
.
yeejoin
.
amos
.
boot
.
module
.
jcs
.
biz
.
aop
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.yeejoin.amos.boot.biz.common.bo.CompanyBo
;
import
com.yeejoin.amos.boot.biz.common.bo.DepartmentBo
;
import
com.yeejoin.amos.boot.biz.common.bo.ReginParams
;
import
com.yeejoin.amos.boot.biz.common.bo.RoleBo
;
import
com.yeejoin.amos.boot.biz.common.utils.GlobalCache
;
import
com.yeejoin.amos.boot.biz.common.utils.RedisUtils
;
import
com.yeejoin.amos.component.feign.model.FeignClientResult
;
import
com.yeejoin.amos.feign.privilege.Privilege
;
import
com.yeejoin.amos.feign.privilege.model.AgencyUserModel
;
import
com.yeejoin.amos.feign.privilege.model.CompanyModel
;
import
com.yeejoin.amos.feign.privilege.model.DepartmentModel
;
import
com.yeejoin.amos.feign.privilege.model.RoleModel
;
import
org.aspectj.lang.JoinPoint
;
import
org.aspectj.lang.annotation.AfterReturning
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.annotation.Before
;
import
org.aspectj.lang.annotation.Pointcut
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
org.typroject.tyboot.core.foundation.context.RequestContext
;
import
org.typroject.tyboot.core.foundation.utils.Bean
;
import
javax.servlet.http.HttpServletRequest
;
import
java.util.Arrays
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
//import com.yeejoin.equipmanage.common.config.GlobalCache;
//import com.yeejoin.equipmanage.common.utils.RedisUtils;
//import com.yeejoin.equipmanage.remote.CompanyBo;
//import com.yeejoin.equipmanage.remote.DepartmentBo;
//import com.yeejoin.equipmanage.remote.ReginParams;
//import com.yeejoin.equipmanage.remote.RoleBo;
/**
* controller层切面 用于用户数据缓存 供 sql自动填充使用
*
* @author Admin
*/
@Aspect
@Component
public
class
ControllerAop
{
/**
* saveUserRedis设置过期时间
*/
@Value
(
"${redis_region_time_second}"
)
private
Long
redisRegionTimeSecond
;
@Autowired
private
RedisUtils
redisUtils
;
@Pointcut
(
"execution(public * com.yeejoin.amos.boot.module.jcs.biz.controller..*(..))"
)
public
void
userCache
()
{
}
@Before
(
"userCache()"
)
public
void
doBefore
(
JoinPoint
joinPoint
)
throws
Throwable
{
ServletRequestAttributes
attributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
HttpServletRequest
request
=
attributes
.
getRequest
();
String
token
=
request
.
getHeader
(
"token"
);
if
(
token
!=
null
)
{
String
product
=
request
.
getHeader
(
"product"
);
String
appKey
=
request
.
getHeader
(
"appKey"
);
RequestContext
.
setToken
(
token
);
RequestContext
.
setProduct
(
product
);
RequestContext
.
setAppKey
(
appKey
);
FeignClientResult
<
AgencyUserModel
>
feignClientResult
=
Privilege
.
agencyUserClient
.
getme
();
AgencyUserModel
userModel
=
feignClientResult
.
getResult
();
if
(
userModel
==
null
)
{
throw
new
Exception
(
"无法获取用户信息"
);
}
RequestContext
.
setExeUserId
(
userModel
.
getUserId
());
String
key
=
"region_"
+
userModel
.
getUserId
()
+
"_"
+
token
;
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
"user"
,
JSON
.
toJSONString
(
userModel
));
// 不需要添加请求头的接口
String
[]
url
=
new
String
[]{
"/api/user/selectInfo"
,
"/api/user/save/curCompany"
};
System
.
out
.
println
((
Arrays
.
asList
(
url
).
toString
()));
System
.
out
.
println
(
request
.
getRequestURI
());
// 获取请求路径
if
(
Arrays
.
asList
(
url
).
contains
(
request
.
getRequestURI
()))
{
// 暂无需要
}
else
{
if
(
redisUtils
.
hasKey
(
key
))
{
map
.
put
(
"org"
,
redisUtils
.
get
(
key
).
toString
());
GlobalCache
.
paramMap
.
put
(
token
,
JSON
.
toJSONString
(
map
));
}
else
{
saveUserRedis
(
userModel
,
token
);
}
}
}
}
public
void
saveUserRedis
(
AgencyUserModel
user
,
String
token
)
{
CompanyBo
company
=
new
CompanyBo
();
DepartmentBo
department
=
new
DepartmentBo
();
RoleBo
role
=
new
RoleBo
();
CompanyModel
companyM
=
user
.
getCompanys
().
get
(
0
);
Bean
.
copyExistPropertis
(
companyM
,
company
);
Map
<
Long
,
List
<
DepartmentModel
>>
mapDepartments
=
user
.
getCompanyDepartments
();
DepartmentModel
departmentM
=
mapDepartments
.
get
(
companyM
.
getSequenceNbr
()).
get
(
0
);
Bean
.
copyExistPropertis
(
departmentM
,
department
);
Map
<
Long
,
List
<
RoleModel
>>
roles
=
user
.
getOrgRoles
();
Long
sequenceNbr
;
if
(
departmentM
==
null
)
{
sequenceNbr
=
null
;
}
else
{
sequenceNbr
=
departmentM
.
getSequenceNbr
();
}
RoleModel
roleM
=
null
;
if
(
sequenceNbr
==
null
)
{
roleM
=
roles
.
get
(
companyM
.
getSequenceNbr
()).
get
(
0
);
}
else
{
roleM
=
roles
.
get
(
sequenceNbr
).
get
(
0
);
}
Bean
.
copyExistPropertis
(
roleM
,
role
);
ReginParams
reginParams
=
new
ReginParams
();
reginParams
.
setCompany
(
company
);
reginParams
.
setRole
(
role
);
reginParams
.
setDepartment
(
department
);
redisUtils
.
set
(
buildKey
(
user
.
getUserId
(),
token
),
JSONObject
.
toJSONString
(
reginParams
),
redisRegionTimeSecond
);
}
public
String
buildKey
(
String
userId
,
String
token
)
{
return
"region_"
+
userId
+
"_"
+
token
;
}
@AfterReturning
(
returning
=
"ret"
,
pointcut
=
"userCache()"
)
public
void
doAfterReturning
(
Object
ret
)
throws
Throwable
{
ServletRequestAttributes
attributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
HttpServletRequest
request
=
attributes
.
getRequest
();
String
token
=
request
.
getHeader
(
"token"
);
if
(
token
!=
null
)
{
GlobalCache
.
paramMap
.
remove
(
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