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
c6d041d2
Commit
c6d041d2
authored
Mar 29, 2023
by
shenjunjie
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'release' into 'master'
Release See merge request
!288
parents
018304d6
981cbd0f
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
61 additions
and
57 deletions
+61
-57
src/main/java/com/zhiwei/brandkbs2/aop/AopLogRecord.java
+57
-50
src/main/java/com/zhiwei/brandkbs2/auth/AuthAspect.java
+1
-1
src/main/java/com/zhiwei/brandkbs2/interceptor/MainAuthInterceptor.java
+1
-1
src/main/java/com/zhiwei/brandkbs2/util/Tools.java
+2
-5
No files found.
src/main/java/com/zhiwei/brandkbs2/aop/AopLogRecord.java
View file @
c6d041d2
...
...
@@ -16,6 +16,8 @@ import io.swagger.annotations.Api;
import
io.swagger.annotations.ApiOperation
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.Logger
;
import
org.aspectj.lang.JoinPoint
;
import
org.aspectj.lang.annotation.AfterReturning
;
import
org.aspectj.lang.annotation.Aspect
;
...
...
@@ -31,7 +33,6 @@ import javax.servlet.ServletRequest;
import
javax.servlet.http.HttpServletRequest
;
import
java.lang.reflect.Method
;
import
java.net.InetAddress
;
import
java.net.UnknownHostException
;
import
java.util.*
;
import
static
java
.
util
.
Objects
.
nonNull
;
...
...
@@ -45,6 +46,8 @@ import static java.util.Objects.nonNull;
@Aspect
@Component
public
class
AopLogRecord
{
public
static
final
Logger
log
=
LogManager
.
getLogger
(
AopLogRecord
.
class
);
@Value
(
"${jwt.key}"
)
private
String
jwtKey
;
@Resource
(
name
=
"userServiceImpl"
)
...
...
@@ -58,57 +61,61 @@ public class AopLogRecord {
private
static
final
List
<
String
>
URL_PATTERNS
=
Arrays
.
asList
(
"/getNewAll"
,
"/getNew"
,
"/schedule"
);
@AfterReturning
(
value
=
"execution(public * com..controller..admin..*Controller.*(..)) || execution(* com..controller..app..*Controller.*(..)) || execution(* com..controller..LoginController.checkBind())"
,
returning
=
"ResponseResult"
)
private
void
beforeLog
(
JoinPoint
joinPoint
,
ResponseResult
ResponseResult
)
throws
UnknownHostException
,
ClassNotFoundException
{
long
now
=
System
.
currentTimeMillis
();
// RequestContextHolder 顾名思义 持有 request 上下文的容器
ServletRequestAttributes
requestAttributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
// 获取request对象
HttpServletRequest
request
=
Objects
.
requireNonNull
(
requestAttributes
).
getRequest
();
// webRequest拿路径的uri和request拿的uri有区别
NativeWebRequest
webRequest
=
new
ServletWebRequest
(
request
);
Object
webRequestAttribute
=
webRequest
.
getAttribute
(
HandlerMapping
.
BEST_MATCHING_PATTERN_ATTRIBUTE
,
RequestAttributes
.
SCOPE_REQUEST
);
if
(
filterUrl
(
webRequestAttribute
))
{
// controller接口信息
String
httpMethod
=
request
.
getMethod
();
String
uri
=
request
.
getRequestURI
();
String
methodName
=
joinPoint
.
getSignature
().
getName
();
// 访问页面
String
className
=
joinPoint
.
getTarget
().
getClass
().
getName
();
Class
<?>
clazz
=
Class
.
forName
(
className
);
String
controller
=
clazz
.
getAnnotation
(
Api
.
class
).
tags
()[
0
];
// 操作模块
MethodSignature
methodSignature
=
(
MethodSignature
)
joinPoint
.
getSignature
();
String
method
=
methodSignature
.
getMethod
().
getAnnotation
(
ApiOperation
.
class
).
value
();
// 前后台,false:前台,true:后台
boolean
backstage
=
uri
.
contains
(
"admin"
);
if
(!(
Tools
.
isNullOrUndefined
(
request
.
getHeader
(
jwtKey
))
||
Tools
.
isNullOrUndefined
(
request
.
getHeader
(
"pid"
))))
{
Map
<
String
,
Object
>
map
=
JwtUtil
.
unsign
(
request
.
getHeader
(
jwtKey
),
Map
.
class
);
if
(
null
==
map
)
{
return
;
}
Object
userIdObj
=
map
.
get
(
GenericAttribute
.
USER_ID
);
// 无用户信息不做记录
if
(
null
==
userIdObj
)
{
return
;
}
String
userId
=
userIdObj
.
toString
();
String
projectId
=
request
.
getHeader
(
"pid"
);
UserInfo
userInfo
=
userService
.
queryUserInfo
(
userId
,
projectId
);
String
nickName
=
userInfo
.
getNickname
();
// 请求地址和服务器地址
String
severAddress
=
InetAddress
.
getLocalHost
().
getHostAddress
();
String
ipAddress
=
Tools
.
getIpAddress
(
request
);
// 接口传参信息
List
<
Map
<
String
,
Object
>>
arguments
=
getRequestArguments
(
joinPoint
);
Behavior
behavior
=
new
Behavior
(
userId
,
projectId
,
nickName
,
ipAddress
,
now
,
controller
,
method
,
backstage
,
uri
,
methodName
,
httpMethod
,
arguments
,
now
,
severAddress
,
null
);
UserLogRecord
userLogRecord
=
userLogRecord
(
projectId
,
userInfo
,
joinPoint
,
methodSignature
,
ResponseResult
);
if
(
Objects
.
nonNull
(
userLogRecord
))
{
userLogRecordDao
.
insertOne
(
userLogRecord
);
private
void
beforeLog
(
JoinPoint
joinPoint
,
ResponseResult
ResponseResult
){
try
{
long
now
=
System
.
currentTimeMillis
();
// RequestContextHolder 顾名思义 持有 request 上下文的容器
ServletRequestAttributes
requestAttributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
// 获取request对象
HttpServletRequest
request
=
Objects
.
requireNonNull
(
requestAttributes
).
getRequest
();
// webRequest拿路径的uri和request拿的uri有区别
NativeWebRequest
webRequest
=
new
ServletWebRequest
(
request
);
Object
webRequestAttribute
=
webRequest
.
getAttribute
(
HandlerMapping
.
BEST_MATCHING_PATTERN_ATTRIBUTE
,
RequestAttributes
.
SCOPE_REQUEST
);
if
(
filterUrl
(
webRequestAttribute
))
{
// controller接口信息
String
httpMethod
=
request
.
getMethod
();
String
uri
=
request
.
getRequestURI
();
String
methodName
=
joinPoint
.
getSignature
().
getName
();
// 访问页面
String
className
=
joinPoint
.
getTarget
().
getClass
().
getName
();
Class
<?>
clazz
=
Class
.
forName
(
className
);
String
controller
=
clazz
.
getAnnotation
(
Api
.
class
).
tags
()[
0
];
// 操作模块
MethodSignature
methodSignature
=
(
MethodSignature
)
joinPoint
.
getSignature
();
String
method
=
methodSignature
.
getMethod
().
getAnnotation
(
ApiOperation
.
class
).
value
();
// 前后台,false:前台,true:后台
boolean
backstage
=
uri
.
contains
(
"admin"
);
if
(!(
Tools
.
isNullOrUndefined
(
request
.
getHeader
(
jwtKey
))
||
Tools
.
isNullOrUndefined
(
request
.
getHeader
(
"pid"
))))
{
Map
<
String
,
Object
>
map
=
JwtUtil
.
unsign
(
request
.
getHeader
(
jwtKey
),
Map
.
class
);
if
(
null
==
map
)
{
return
;
}
Object
userIdObj
=
map
.
get
(
GenericAttribute
.
USER_ID
);
// 无用户信息不做记录
if
(
null
==
userIdObj
)
{
return
;
}
String
userId
=
userIdObj
.
toString
();
String
projectId
=
request
.
getHeader
(
"pid"
);
UserInfo
userInfo
=
userService
.
queryUserInfo
(
userId
,
projectId
);
String
nickName
=
userInfo
.
getNickname
();
// 请求地址和服务器地址
String
severAddress
=
InetAddress
.
getLocalHost
().
getHostAddress
();
String
ipAddress
=
Tools
.
getIpAddress
(
request
);
// 接口传参信息
List
<
Map
<
String
,
Object
>>
arguments
=
getRequestArguments
(
joinPoint
);
Behavior
behavior
=
new
Behavior
(
userId
,
projectId
,
nickName
,
ipAddress
,
now
,
controller
,
method
,
backstage
,
uri
,
methodName
,
httpMethod
,
arguments
,
now
,
severAddress
,
null
);
UserLogRecord
userLogRecord
=
userLogRecord
(
projectId
,
userInfo
,
joinPoint
,
methodSignature
,
ResponseResult
);
if
(
Objects
.
nonNull
(
userLogRecord
))
{
userLogRecordDao
.
insertOne
(
userLogRecord
);
}
String
collectionName
=
behaviorDao
.
generateCollectionName
();
behaviorDao
.
insertOneWithoutId
(
behavior
,
collectionName
);
}
String
collectionName
=
behaviorDao
.
generateCollectionName
();
behaviorDao
.
insertOneWithoutId
(
behavior
,
collectionName
);
}
}
catch
(
Exception
e
)
{
log
.
error
(
"beforeLog"
,
e
);
}
}
...
...
src/main/java/com/zhiwei/brandkbs2/auth/AuthAspect.java
View file @
c6d041d2
...
...
@@ -70,7 +70,7 @@ public class AuthAspect {
}
String
token
=
request
.
getHeader
(
jwtKey
);
// 存在有效token
if
(
null
!=
token
&&
!
Tools
.
tokenEmpty
(
token
))
{
if
(
null
!=
token
&&
!
Tools
.
isNullOrUndefined
(
token
))
{
Object
proceed
;
try
{
Map
<
String
,
Object
>
tokenInfo
=
JwtUtil
.
unsign
(
token
,
Map
.
class
);
...
...
src/main/java/com/zhiwei/brandkbs2/interceptor/MainAuthInterceptor.java
View file @
c6d041d2
...
...
@@ -53,7 +53,7 @@ public class MainAuthInterceptor implements HandlerInterceptor {
String
token
=
request
.
getHeader
(
"Token"
);
try
{
// 不存在token 重新获取token
if
(
Tools
.
tokenEmpty
(
token
))
{
if
(
Tools
.
isNullOrUndefined
(
token
))
{
Tools
.
responseMessage
(
response
,
HttpServletResponse
.
SC_UNAUTHORIZED
,
new
ResponseResult
(
CommonCodeEnum
.
UNAUTHENTICATED
,
null
));
return
false
;
}
...
...
src/main/java/com/zhiwei/brandkbs2/util/Tools.java
View file @
c6d041d2
...
...
@@ -649,10 +649,6 @@ public class Tools {
}
}
public
static
boolean
tokenEmpty
(
String
token
)
{
return
null
==
token
||
Objects
.
equals
(
"undefined"
,
token
)
||
Objects
.
equals
(
"null"
,
token
);
}
/**
* 获得区段时间的最大最小值
*
...
...
@@ -1027,6 +1023,6 @@ public class Tools {
* @return
*/
public
static
boolean
isNullOrUndefined
(
Object
o
)
{
return
(
isEmpty
(
o
)
||
Objects
.
equals
(
"undefined"
,
o
));
return
(
isEmpty
(
o
)
||
Objects
.
equals
(
"undefined"
,
o
)
||
Objects
.
equals
(
"null"
,
o
)
);
}
}
\ No newline at end of file
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