Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
sh-ds
Project
Project
Details
Activity
Releases
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
李良停
sh-ds
Commits
43d5f7ef
Commit
43d5f7ef
authored
May 09, 2022
by
wangsong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add log
parent
6022f8e5
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
165 additions
and
2 deletions
+165
-2
AccessInterceptor.java
.../eden/authcenter/agent/interceptor/AccessInterceptor.java
+29
-0
ApiInterceptorConfig.java
...en/authcenter/agent/interceptor/ApiInterceptorConfig.java
+7
-0
pom.xml
eden-authcenter-log/pom.xml
+6
-0
AuthLogServiceImpl.java
.../eden/authcenter/log/service/impl/AuthLogServiceImpl.java
+15
-2
RequestResponseHolder.java
...seall/eden/authcenter/log/util/RequestResponseHolder.java
+108
-0
No files found.
eden-authcenter-agent/src/main/java/com/chineseall/eden/authcenter/agent/interceptor/AccessInterceptor.java
0 → 100644
View file @
43d5f7ef
package
com
.
chineseall
.
eden
.
authcenter
.
agent
.
interceptor
;
import
com.chineseall.eden.authcenter.log.util.RequestResponseHolder
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.servlet.HandlerInterceptor
;
import
org.springframework.web.servlet.ModelAndView
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
@Component
public
class
AccessInterceptor
implements
HandlerInterceptor
{
@Override
public
boolean
preHandle
(
HttpServletRequest
httpServletRequest
,
HttpServletResponse
httpServletResponse
,
Object
o
)
throws
Exception
{
RequestResponseHolder
.
set
(
httpServletRequest
,
httpServletResponse
);
return
true
;
}
@Override
public
void
postHandle
(
HttpServletRequest
httpServletRequest
,
HttpServletResponse
httpServletResponse
,
Object
o
,
ModelAndView
modelAndView
)
throws
Exception
{
RequestResponseHolder
.
remove
();
}
@Override
public
void
afterCompletion
(
HttpServletRequest
httpServletRequest
,
HttpServletResponse
httpServletResponse
,
Object
o
,
Exception
e
)
throws
Exception
{
}
}
eden-authcenter-agent/src/main/java/com/chineseall/eden/authcenter/agent/interceptor/ApiInterceptorConfig.java
View file @
43d5f7ef
...
...
@@ -14,11 +14,18 @@ public class ApiInterceptorConfig extends WebMvcConfigurerAdapter {
@Autowired
ApiInterceptor
apiInterceptor
;
@Autowired
AccessInterceptor
accessInterceptor
;
@Override
public
void
addInterceptors
(
InterceptorRegistry
registry
)
{
//注册TestInterceptor拦截器
InterceptorRegistration
accessRegistration
=
registry
.
addInterceptor
(
accessInterceptor
);
accessRegistration
.
addPathPatterns
(
"/**"
);
InterceptorRegistration
registration
=
registry
.
addInterceptor
(
apiInterceptor
);
registration
.
addPathPatterns
(
"/config/**"
);
//
}
@Override
...
...
eden-authcenter-log/pom.xml
View file @
43d5f7ef
...
...
@@ -94,6 +94,12 @@
<type>
jar
</type>
</dependency>
<dependency>
<groupId>
cn.hutool
</groupId>
<artifactId>
hutool-all
</artifactId>
<version>
5.7.22
</version>
</dependency>
</dependencies>
...
...
eden-authcenter-log/src/main/java/com/chineseall/eden/authcenter/log/service/impl/AuthLogServiceImpl.java
View file @
43d5f7ef
package
com
.
chineseall
.
eden
.
authcenter
.
log
.
service
.
impl
;
import
cn.hutool.core.bean.BeanUtil
;
import
cn.sh.chineseall.framework.core.util.CollectionUtils
;
import
com.alibaba.fastjson.JSON
;
import
com.chineseall.eden.authcenter.log.dao.AuthLogDao
;
...
...
@@ -7,6 +8,7 @@ import com.chineseall.eden.authcenter.log.model.AuthLog;
import
com.chineseall.eden.authcenter.log.model.AuthLogHourCount
;
import
com.chineseall.eden.authcenter.log.service.AuthLogService
;
import
com.chineseall.eden.authcenter.log.util.BusinessLogUtils
;
import
com.chineseall.eden.authcenter.log.util.RequestResponseHolder
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
...
...
@@ -28,13 +30,24 @@ public class AuthLogServiceImpl implements AuthLogService {
if
(
authLog
.
getId
()
!=
null
){
authLog
.
setUpdateTime
(
new
Date
());
authLogDao
.
replace
(
authLog
);
BusinessLogUtils
.
info
(
JSON
.
toJSONString
(
authLog
));
}
else
{
authLog
.
setId
(
UUID
.
randomUUID
().
toString
());
authLog
.
setCreateTime
(
new
Date
());
authLogDao
.
insert
(
authLog
);
BusinessLogUtils
.
info
(
JSON
.
toJSONString
(
authLog
));
}
addBusinessLog
(
authLog
);
}
private
void
addBusinessLog
(
AuthLog
authLog
){
RequestResponseHolder
holder
=
RequestResponseHolder
.
get
();
Map
<
String
,
Object
>
dataMap
=
BeanUtil
.
beanToMap
(
authLog
);
dataMap
.
put
(
"user-agent"
,
holder
.
getRequestUserAgent
());
dataMap
.
put
(
"remote-address"
,
holder
.
getRequestRemoteAddress
());
dataMap
.
put
(
"referer"
,
holder
.
getRequestReferer
());
dataMap
.
put
(
"ip"
,
holder
.
getIpAddress
());
dataMap
.
put
(
"request"
,
holder
.
getRequestMethod
()
+
" "
+
holder
.
getRequestContextPath
());
dataMap
.
put
(
"timestamp"
,
System
.
currentTimeMillis
());
BusinessLogUtils
.
info
(
JSON
.
toJSONString
(
dataMap
));
}
@Override
...
...
eden-authcenter-log/src/main/java/com/chineseall/eden/authcenter/log/util/RequestResponseHolder.java
0 → 100644
View file @
43d5f7ef
package
com
.
chineseall
.
eden
.
authcenter
.
log
.
util
;
import
cn.sh.chineseall.framework.core.repackaged.org.apache.commons.lang3.StringUtils
;
import
lombok.Getter
;
import
lombok.Setter
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.util.Enumeration
;
import
java.util.HashMap
;
import
java.util.Map
;
@Getter
@Setter
public
class
RequestResponseHolder
{
public
static
final
ThreadLocal
<
RequestResponseHolder
>
requestResponseLocal
=
new
ThreadLocal
<>();
private
HttpServletRequest
request
;
private
HttpServletResponse
response
;
public
Map
<
String
,
String
>
getRequestHeaderMap
(){
Map
<
String
,
String
>
requestHeaderMap
=
new
HashMap
<>();
Enumeration
<
String
>
headerNames
=
getRequest
().
getHeaderNames
();
while
(
headerNames
.
hasMoreElements
()){
String
headerName
=
headerNames
.
nextElement
();
String
value
=
getRequest
().
getHeader
(
headerName
);
requestHeaderMap
.
put
(
headerName
,
value
);
}
return
requestHeaderMap
;
}
public
String
getRequestUserAgent
(){
Map
<
String
,
String
>
headerMap
=
getRequestHeaderMap
();
return
headerMap
.
get
(
"user-agent"
);
}
public
String
getRequestRemoteAddress
(){
return
getRequest
().
getRemoteAddr
();
}
public
String
getRequestReferer
(){
Map
<
String
,
String
>
headerMap
=
getRequestHeaderMap
();
return
headerMap
.
get
(
"referer"
);
}
public
String
getRequestMethod
(){
return
getRequest
().
getMethod
();
}
public
String
getRequestContextPath
(){
return
getRequest
().
getContextPath
();
}
public
String
getIpAddress
()
{
Map
<
String
,
String
>
headerMap
=
getRequestHeaderMap
();
String
Xip
=
headerMap
.
get
(
"X-Real-IP"
);
String
XFor
=
headerMap
.
get
(
"X-Forwarded-For"
);
if
(
StringUtils
.
isNotEmpty
(
XFor
)
&&
!
"unKnown"
.
equalsIgnoreCase
(
XFor
)){
//多次反向代理后会有多个ip值,第一个ip才是真实ip
int
index
=
XFor
.
indexOf
(
","
);
if
(
index
!=
-
1
){
return
XFor
.
substring
(
0
,
index
);
}
else
{
return
XFor
;
}
}
XFor
=
Xip
;
if
(
StringUtils
.
isNotEmpty
(
XFor
)
&&
!
"unKnown"
.
equalsIgnoreCase
(
XFor
)){
return
XFor
;
}
if
(
StringUtils
.
isBlank
(
XFor
)
||
"unknown"
.
equalsIgnoreCase
(
XFor
))
{
XFor
=
request
.
getHeader
(
"Proxy-Client-IP"
);
}
if
(
StringUtils
.
isBlank
(
XFor
)
||
"unknown"
.
equalsIgnoreCase
(
XFor
))
{
XFor
=
request
.
getHeader
(
"WL-Proxy-Client-IP"
);
}
if
(
StringUtils
.
isBlank
(
XFor
)
||
"unknown"
.
equalsIgnoreCase
(
XFor
))
{
XFor
=
request
.
getHeader
(
"HTTP_CLIENT_IP"
);
}
if
(
StringUtils
.
isBlank
(
XFor
)
||
"unknown"
.
equalsIgnoreCase
(
XFor
))
{
XFor
=
request
.
getHeader
(
"HTTP_X_FORWARDED_FOR"
);
}
if
(
StringUtils
.
isBlank
(
XFor
)
||
"unknown"
.
equalsIgnoreCase
(
XFor
))
{
XFor
=
request
.
getRemoteAddr
();
}
return
XFor
;
}
public
static
void
set
(
HttpServletRequest
request
,
HttpServletResponse
response
){
RequestResponseHolder
holder
=
new
RequestResponseHolder
();
holder
.
setRequest
(
request
);
holder
.
setResponse
(
response
);
requestResponseLocal
.
set
(
holder
);
}
public
static
RequestResponseHolder
get
(){
return
requestResponseLocal
.
get
();
}
public
static
void
remove
(){
requestResponseLocal
.
remove
();
}
}
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