Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
amos-boot-zx-biz
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
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
Jobs
Commits
Open sidebar
项目统一框架
一体化_户用光伏项目代码
amos-boot-zx-biz
Commits
85c69978
Commit
85c69978
authored
Oct 27, 2022
by
wujiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改aop代码问题
parent
1eba123e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
133 additions
and
122 deletions
+133
-122
ControllerAop.java
...a/com/yeejoin/amos/boot/biz/common/aop/ControllerAop.java
+133
-122
No files found.
amos-boot-biz-common/src/main/java/com/yeejoin/amos/boot/biz/common/aop/ControllerAop.java
View file @
85c69978
...
...
@@ -43,8 +43,8 @@ import com.yeejoin.amos.feign.systemctl.Systemctl;
import
com.yeejoin.amos.feign.systemctl.model.RegionModel
;
/**
* controller层切面 用于用户数据缓存 供 sql自动填充使用
*
(使用粒度过大的Aop会创建大量代理对象,影响性能,占用内存,考虑使用
WebMvcConfigurer#addInterceptors 添加拦截器 )
* controller层切面 用于用户数据缓存 供 sql自动填充使用
(使用粒度过大的Aop会创建大量代理对象,影响性能,占用内存,考虑使用
* WebMvcConfigurer#addInterceptors 添加拦截器 )
*
* @author Admin
*/
...
...
@@ -52,141 +52,152 @@ import com.yeejoin.amos.feign.systemctl.model.RegionModel;
@Component
@Order
(
value
=
0
)
public
class
ControllerAop
{
/**
* saveUserRedis设置过期时间
*/
@Value
(
"${redis.cache.failure.time}"
)
private
Long
redisRegionTimeSecond
;
/**
* saveUserRedis设置过期时间
*/
@Value
(
"${redis.cache.failure.time}"
)
private
Long
redisRegionTimeSecond
;
@Autowired
private
RedisUtils
redisUtils
;
@Autowired
private
RedisUtils
redisUtils
;
@Pointcut
(
"(execution(public * com.yeejoin.amos.boot.module..*.biz.controller..*(..))"
+
"|| execution(public * com.yeejoin.amos.*.business.controller..*(..))"
+
" && !@annotation(org.springframework.scheduling.annotation.Scheduled))"
+
"|| execution(public * com.yeejoin.equipmanage.controller..*.*(..))"
+
"||
execution(public * com.yeejoin.amos.api.openapi.controller..*(..))"
)
public
void
userCache
()
{
@Pointcut
(
"(execution(public * com.yeejoin.amos.boot.module..*.biz.controller..*(..))"
+
"|| execution(public * com.yeejoin.amos.*.business.controller..*(..))"
+
" && !@annotation(org.springframework.scheduling.annotation.Scheduled))"
+
"|| execution(public * com.yeejoin.equipmanage.controller..*.*(..))"
+
"||
execution(public * com.yeejoin.amos.api.openapi.controller..*(..))"
)
public
void
userCache
()
{
}
}
@Before
(
"userCache()"
)
public
void
doBefore
(
JoinPoint
joinPoint
)
{
PermissionInterceptorContext
.
clean
();
ServletRequestAttributes
attributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
HttpServletRequest
request
=
null
;
if
(
ObjectUtils
.
isNotEmpty
(
attributes
)){
request
=
attributes
.
getRequest
();
}
else
{
return
;
}
// 不需要添加请求头的接口
String
[]
url
=
new
String
[]{
"/api/user/save/curCompany"
,
"/jcs/command/lookHtmlText"
,
"/jcs/common/duty-person/findByDutyAreaId"
,
"/tzs/wechatBack"
,
"/tzs/elevator/getElevatorInfo"
,
"/openapi/bizToken/applyToken"
};
// 获取请求路径
for
(
String
uri
:
url
)
{
if
(
request
.
getRequestURI
().
indexOf
(
uri
)
!=
-
1
)
{
return
;
}
}
//TODO tyboot 框架拦截器已缓存数据
String
token
=
RequestContext
.
getToken
();
@Before
(
"userCache()"
)
public
void
doBefore
(
JoinPoint
joinPoint
)
{
PermissionInterceptorContext
.
clean
();
ServletRequestAttributes
attributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
HttpServletRequest
request
=
null
;
if
(
ObjectUtils
.
isNotEmpty
(
attributes
))
{
request
=
attributes
.
getRequest
();
}
else
{
return
;
}
// 不需要添加请求头的接口
String
[]
url
=
new
String
[]
{
"/api/user/save/curCompany"
,
"/jcs/command/lookHtmlText"
,
"/jcs/common/duty-person/findByDutyAreaId"
,
"/tzs/wechatBack"
,
"/tzs/elevator/getElevatorInfo"
,
"/openapi/bizToken/applyToken"
,
"/tzs/reg-unit-info/management-unit/tree"
,
"/tzs/flc-unit-info/region/tree"
,
"/tzs/reg-unit-info/unit-type/list"
};
// 获取请求路径
for
(
String
uri
:
url
)
{
if
(
request
.
getRequestURI
().
indexOf
(
uri
)
!=
-
1
)
{
return
;
}
}
// TODO tyboot 框架拦截器已缓存数据
String
token
=
RequestContext
.
getToken
();
// 不需要校验token的接口直接返回
// 不需要校验token的接口直接返回
if
(
joinPoint
.
getSignature
()
instanceof
MethodSignature
)
{
return
;
}
// 平台studio配置的下载接口token从url里取
if
(
ValidationUtil
.
isEmpty
(
token
))
{
fillRequestContext
(
request
);
token
=
RequestContext
.
getToken
();
}
if
(
token
!=
null
)
{
String
pattern
=
RedisKey
.
buildPatternKey
(
token
);
//验证token有效性,防止token失效
AgencyUserModel
userModel
;
try
{
FeignClientResult
<
AgencyUserModel
>
agencyUserModel
=
Privilege
.
agencyUserClient
.
getme
();
userModel
=
agencyUserModel
.
getResult
();
if
(
userModel
==
null
)
{
throw
new
Exception
(
"无法获取用户信息"
);
}
RequestContext
.
setExeUserId
(
userModel
.
getUserId
());
}
catch
(
Exception
e
)
{
//删除失效token缓存
redisUtils
.
getAndDeletePatternKeys
(
pattern
);
throw
new
RuntimeException
(
e
.
getMessage
());
if
(!((
MethodSignature
)
joinPoint
.
getSignature
()).
getMethod
().
getAnnotation
(
TycloudOperation
.
class
).
needAuth
()
&&
!
request
.
getParameterMap
().
containsKey
(
"token"
)
&&
ValidationUtil
.
isEmpty
(
token
))
{
return
;
}
saveUserRedis
(
userModel
,
token
);
}
else
{
throw
new
AuthException
(
"请求未包含认证信息."
);
}
}
private
void
saveUserRedis
(
AgencyUserModel
user
,
String
token
)
{
String
authToken
=
RedisKey
.
buildReginKey
(
user
.
getUserId
(),
token
);
if
(
redisUtils
.
hasKey
(
authToken
))
{
return
;
}
CompanyBo
company
=
new
CompanyBo
();
DepartmentBo
department
=
new
DepartmentBo
();
RoleBo
role
=
new
RoleBo
();
//平台更新 人员部门可能为NULL 导致redis缓存不到人员信息
Map
<
Long
,
List
<
DepartmentModel
>>
mapDepartments
=
new
HashMap
<>();
DepartmentModel
departmentM
=
new
DepartmentModel
();
CompanyModel
companyM
=
user
.
getCompanys
()
!=
null
?
user
.
getCompanys
().
get
(
0
)
:
null
;
Bean
.
copyExistPropertis
(
companyM
,
company
);
if
(!
ValidationUtil
.
isEmpty
(
user
.
getCompanyDepartments
())){
mapDepartments
=
user
.
getCompanyDepartments
();
departmentM
=
companyM
!=
null
?
mapDepartments
.
get
(
companyM
.
getSequenceNbr
()).
get
(
0
)
:
null
;
}
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
=
companyM
!=
null
?
roles
.
get
(
companyM
.
getSequenceNbr
()).
get
(
0
)
:
null
;
}
else
{
roleM
=
roles
.
get
(
sequenceNbr
).
get
(
0
);
}
if
(
companyM
!=
null
&&
companyM
.
getRegionSeq
()!=
null
)
{
FeignClientResult
<
RegionModel
>
result
=
Systemctl
.
regionClient
.
getRegion
(
Long
.
valueOf
(
companyM
.
getRegionSeq
()));
company
.
setRegionCode
(
String
.
valueOf
(
result
.
getResult
().
getRegionCode
()));
}
Bean
.
copyExistPropertis
(
roleM
,
role
);
ReginParams
reginParams
=
new
ReginParams
();
reginParams
.
setCompany
(
company
);
reginParams
.
setRole
(
role
);
reginParams
.
setDepartment
(
department
);
reginParams
.
setUserModel
(
user
);
redisUtils
.
set
(
authToken
,
JSONObject
.
toJSONString
(
reginParams
),
redisRegionTimeSecond
);
}
// 平台studio配置的下载接口token从url里取
if
(
ValidationUtil
.
isEmpty
(
token
))
{
fillRequestContext
(
request
);
token
=
RequestContext
.
getToken
();
}
if
(
token
!=
null
)
{
String
pattern
=
RedisKey
.
buildPatternKey
(
token
);
// 验证token有效性,防止token失效
AgencyUserModel
userModel
;
try
{
FeignClientResult
<
AgencyUserModel
>
agencyUserModel
=
Privilege
.
agencyUserClient
.
getme
();
userModel
=
agencyUserModel
.
getResult
();
if
(
userModel
==
null
)
{
throw
new
Exception
(
"无法获取用户信息"
);
}
RequestContext
.
setExeUserId
(
userModel
.
getUserId
());
}
catch
(
Exception
e
)
{
// 删除失效token缓存
redisUtils
.
getAndDeletePatternKeys
(
pattern
);
throw
new
RuntimeException
(
e
.
getMessage
());
}
saveUserRedis
(
userModel
,
token
);
}
else
{
throw
new
AuthException
(
"请求未包含认证信息."
);
}
}
private
void
saveUserRedis
(
AgencyUserModel
user
,
String
token
)
{
String
authToken
=
RedisKey
.
buildReginKey
(
user
.
getUserId
(),
token
);
if
(
redisUtils
.
hasKey
(
authToken
))
{
return
;
}
CompanyBo
company
=
new
CompanyBo
();
DepartmentBo
department
=
new
DepartmentBo
();
RoleBo
role
=
new
RoleBo
();
// 平台更新 人员部门可能为NULL 导致redis缓存不到人员信息
Map
<
Long
,
List
<
DepartmentModel
>>
mapDepartments
=
new
HashMap
<>();
DepartmentModel
departmentM
=
new
DepartmentModel
();
CompanyModel
companyM
=
user
.
getCompanys
()
!=
null
?
user
.
getCompanys
().
get
(
0
)
:
null
;
Bean
.
copyExistPropertis
(
companyM
,
company
);
if
(!
ValidationUtil
.
isEmpty
(
user
.
getCompanyDepartments
()))
{
mapDepartments
=
user
.
getCompanyDepartments
();
departmentM
=
companyM
!=
null
?
mapDepartments
.
get
(
companyM
.
getSequenceNbr
()).
get
(
0
)
:
null
;
}
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
=
companyM
!=
null
?
roles
.
get
(
companyM
.
getSequenceNbr
()).
get
(
0
)
:
null
;
}
else
{
roleM
=
roles
.
get
(
sequenceNbr
).
get
(
0
);
}
if
(
companyM
!=
null
&&
companyM
.
getRegionSeq
()
!=
null
)
{
FeignClientResult
<
RegionModel
>
result
=
Systemctl
.
regionClient
.
getRegion
(
Long
.
valueOf
(
companyM
.
getRegionSeq
()));
company
.
setRegionCode
(
String
.
valueOf
(
result
.
getResult
().
getRegionCode
()));
}
Bean
.
copyExistPropertis
(
roleM
,
role
);
ReginParams
reginParams
=
new
ReginParams
();
reginParams
.
setCompany
(
company
);
reginParams
.
setRole
(
role
);
reginParams
.
setDepartment
(
department
);
reginParams
.
setUserModel
(
user
);
redisUtils
.
set
(
authToken
,
JSONObject
.
toJSONString
(
reginParams
),
redisRegionTimeSecond
);
}
@AfterReturning
(
returning
=
"ret"
,
pointcut
=
"userCache()"
)
public
void
doAfterReturning
(
Object
ret
)
throws
Throwable
{
//
统一redis管理
@AfterReturning
(
returning
=
"ret"
,
pointcut
=
"userCache()"
)
public
void
doAfterReturning
(
Object
ret
)
throws
Throwable
{
//
统一redis管理
// ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
// HttpServletRequest request = attributes.getRequest();
// String token = request.getHeader("token");
// if (token != null) {
// GlobalCache.paramMap.remove(token);
// }
}
}
private
void
fillRequestContext
(
HttpServletRequest
request
)
{
String
token
=
request
.
getParameterMap
().
get
(
"token"
)[
0
];
String
product
=
request
.
getParameterMap
().
get
(
"product"
)[
0
];
String
appKey
=
request
.
getParameterMap
().
get
(
"appKey"
)[
0
];
RequestContext
.
setToken
(
token
);
RequestContext
.
setProduct
(
product
);
RequestContext
.
setAppKey
(
appKey
);
}
private
void
fillRequestContext
(
HttpServletRequest
request
)
{
String
token
=
null
;
String
product
=
null
;
String
appKey
=
null
;
if
(
request
!=
null
&&
request
.
getParameterMap
()
!=
null
)
{
if
(
request
.
getParameterMap
().
get
(
"token"
)
!=
null
&&
request
.
getParameterMap
().
get
(
"product"
)
!=
null
&&
request
.
getParameterMap
().
get
(
"appKey"
)
!=
null
)
{
token
=
request
.
getParameterMap
().
get
(
"token"
)[
0
];
product
=
request
.
getParameterMap
().
get
(
"product"
)[
0
];
appKey
=
request
.
getParameterMap
().
get
(
"appKey"
)[
0
];
RequestContext
.
setToken
(
token
);
RequestContext
.
setProduct
(
product
);
RequestContext
.
setAppKey
(
appKey
);
}
}
}
}
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