Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
hphy
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
向怀芳
hphy
Commits
062c6d77
Commit
062c6d77
authored
Oct 21, 2022
by
向怀芳
🎱
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1. 代理IP处理03
parent
767a02be
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
10 deletions
+10
-10
WebUtils.java
.../java/cn/sh/stc/sict/cloud/common/core/util/WebUtils.java
+9
-9
RequestGlobalFilter.java
...sict/cloud/common/gateway/filter/RequestGlobalFilter.java
+1
-1
No files found.
cloud-common/cloud-common-core/src/main/java/cn/sh/stc/sict/cloud/common/core/util/WebUtils.java
View file @
062c6d77
...
...
@@ -41,6 +41,7 @@ import java.util.*;
public
class
WebUtils
extends
org
.
springframework
.
web
.
util
.
WebUtils
{
private
final
String
BASIC_
=
"Basic "
;
private
final
String
UNKNOWN
=
"unknown"
;
private
final
String
LOCAL127
=
"127.0.0.1"
;
/**
* 判断是否ajax请求
...
...
@@ -196,33 +197,32 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
public
String
getIP
(
ServerHttpRequest
request
)
{
HttpHeaders
headers
=
request
.
getHeaders
();
String
ip
=
headers
.
getFirst
(
"x-forwarded-for"
);
log
.
error
(
"headers = {}"
,
JSONUtil
.
toJsonStr
(
headers
));
if
(
ip
!=
null
&&
ip
.
length
()
!=
0
&&
!
"unknown"
.
equalsIgnoreCase
(
ip
))
{
if
(
StrUtil
.
isNotBlank
(
ip
)
&&
!
UNKNOWN
.
equalsIgnoreCase
(
ip
))
{
// 多次反向代理后会有多个ip值,第一个ip才是真实ip
if
(
ip
.
indexOf
(
","
)
!=
-
1
)
{
if
(
"127.0.0.1"
.
equals
(
ip
.
split
(
","
)[
0
]))
{
if
(
LOCAL127
.
equals
(
ip
.
split
(
","
)[
0
]))
{
ip
=
ip
.
split
(
","
)[
1
];
}
else
{
ip
=
ip
.
split
(
","
)[
0
];
}
}
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
if
(
StrUtil
.
isBlank
(
ip
)
||
UNKNOWN
.
equalsIgnoreCase
(
ip
))
{
ip
=
headers
.
getFirst
(
"Proxy-Client-IP"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
if
(
StrUtil
.
isBlank
(
ip
)
||
UNKNOWN
.
equalsIgnoreCase
(
ip
))
{
ip
=
headers
.
getFirst
(
"WL-Proxy-Client-IP"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
if
(
StrUtil
.
isBlank
(
ip
)
||
UNKNOWN
.
equalsIgnoreCase
(
ip
))
{
ip
=
headers
.
getFirst
(
"HTTP_CLIENT_IP"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
if
(
StrUtil
.
isBlank
(
ip
)
||
UNKNOWN
.
equalsIgnoreCase
(
ip
))
{
ip
=
headers
.
getFirst
(
"HTTP_X_FORWARDED_FOR"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
if
(
StrUtil
.
isBlank
(
ip
)
||
UNKNOWN
.
equalsIgnoreCase
(
ip
))
{
ip
=
headers
.
getFirst
(
"X-Real-IP"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
if
(
StrUtil
.
isBlank
(
ip
)
||
UNKNOWN
.
equalsIgnoreCase
(
ip
))
{
ip
=
request
.
getRemoteAddress
().
getAddress
().
getHostAddress
();
}
return
ip
.
equals
(
"0:0:0:0:0:0:0:1"
)
?
"127.0.0.1"
:
ip
;
...
...
cloud-common/cloud-common-gateway/src/main/java/cn/sh/stc/sict/cloud/common/gateway/filter/RequestGlobalFilter.java
View file @
062c6d77
...
...
@@ -65,7 +65,7 @@ public class RequestGlobalFilter implements GlobalFilter, Ordered {
// IP白名单
String
ip
=
WebUtils
.
getIP
(
request
);
log
.
error
(
"RemoteAddress = {}, ip = {}"
,
request
.
getRemoteAddress
(),
ip
);
//
log.error("RemoteAddress = {}, ip = {}", request.getRemoteAddress(), ip);
try
{
if
(
whitIPConfig
.
getLimitFlag
()
&&
!
IPStrUtil
.
matches
(
ip
,
whitIPConfig
.
getWhites
()))
{
ServerHttpResponse
response
=
exchange
.
getResponse
();
...
...
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