Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
common
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
曲欣亮
common
Commits
3bf651e3
Commit
3bf651e3
authored
May 22, 2024
by
张永
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改取IP方法
parent
8c16df69
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
91 additions
and
37 deletions
+91
-37
pom.xml
pom.xml
+26
-21
ServletUtil.java
src/main/java/com/egolm/common/web/ServletUtil.java
+65
-16
No files found.
pom.xml
View file @
3bf651e3
...
...
@@ -9,7 +9,7 @@
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
...
...
@@ -108,6 +108,11 @@
<artifactId>
commons-net
</artifactId>
<version>
3.4
</version>
</dependency>
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-lang3
</artifactId>
<version>
3.12.0
</version>
</dependency>
<dependency>
<groupId>
dom4j
</groupId>
<artifactId>
dom4j
</artifactId>
...
...
@@ -136,28 +141,28 @@
<version>
2.0.10
</version>
</dependency>
<!-- 简体转繁体 start 以下为两种 https://blog.51cto.com/u_16213408/6959812 -->
<dependency>
<groupId>
com.github.houbb
</groupId>
<artifactId>
opencc4j
</artifactId>
<version>
1.8.0
</version>
</dependency>
<!-- 简体转繁体 start 以下为两种 https://blog.51cto.com/u_16213408/6959812 -->
<dependency>
<groupId>
com.github.houbb
</groupId>
<artifactId>
opencc4j
</artifactId>
<version>
1.8.0
</version>
</dependency>
<dependency>
<groupId>
com.ibm.icu
</groupId>
<artifactId>
icu4j
</artifactId>
<version>
74.1
</version>
</dependency>
<!-- 简体转繁体end -->
<!-- 图片压缩 https://www.cnblogs.com/yinjing/p/12157562.html -->
<dependency>
<groupId>
net.coobird
</groupId>
<artifactId>
thumbnailator
</artifactId>
<version>
0.4.20
</version>
</dependency>
<dependency>
<groupId>
com.ibm.icu
</groupId>
<artifactId>
icu4j
</artifactId>
<version>
74.1
</version>
</dependency>
<!-- 简体转繁体end -->
<!-- 图片压缩 https://www.cnblogs.com/yinjing/p/12157562.html-->
<dependency>
<groupId>
net.coobird
</groupId>
<artifactId>
thumbnailator
</artifactId>
<version>
0.4.20
</version>
</dependency>
</dependencies>
...
...
src/main/java/com/egolm/common/web/ServletUtil.java
View file @
3bf651e3
...
...
@@ -13,29 +13,78 @@ import java.util.Map.Entry;
import
javax.servlet.http.HttpServletRequest
;
import
org.apache.commons.lang3.StringUtils
;
import
com.egolm.common.StringUtil
;
import
com.egolm.common.exception.PluginException
;
public
class
ServletUtil
{
public
static
String
remoteIp
(
HttpServletRequest
request
)
{
String
ip
=
request
.
getHeader
(
"X-Real_IP"
);
if
(
ip
==
null
||
ip
.
length
()
==
0
||
ip
.
equalsIgnoreCase
(
"unknown"
))
{
ip
=
request
.
getHeader
(
"X-Forwarded-For"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
ip
.
equalsIgnoreCase
(
"unknown"
))
{
ip
=
request
.
getHeader
(
"Proxy-Client-IP"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
ip
.
equalsIgnoreCase
(
"unknown"
))
{
ip
=
request
.
getHeader
(
"WL-Proxy-Client-IP"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
ip
.
equalsIgnoreCase
(
"unknown"
))
{
ip
=
request
.
getRemoteAddr
();
}
String
[]
ips
=
ip
.
trim
().
split
(
", "
);
return
ips
[
ips
.
length
-
1
].
trim
();
}
if
(
request
==
null
)
{
return
"unknown"
;
}
String
ip
=
request
.
getHeader
(
"x-forwarded-for"
);
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
ip
=
request
.
getHeader
(
"Proxy-Client-IP"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
ip
=
request
.
getHeader
(
"X-Forwarded-For"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
ip
=
request
.
getHeader
(
"WL-Proxy-Client-IP"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
ip
=
request
.
getHeader
(
"X-Real-IP"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
ip
=
request
.
getRemoteAddr
();
}
return
"0:0:0:0:0:0:0:1"
.
equals
(
ip
)
?
"127.0.0.1"
:
getMultistageReverseProxyIp
(
ip
);
}
/**
* 从多级反向代理中获得第一个非unknown IP地址
*
* @param ip 获得的IP地址
* @return 第一个非unknown IP地址
*/
public
static
String
getMultistageReverseProxyIp
(
String
ip
)
{
// 多级反向代理检测
if
(
ip
!=
null
&&
ip
.
indexOf
(
","
)
>
0
)
{
final
String
[]
ips
=
ip
.
trim
().
split
(
","
);
for
(
String
subIp
:
ips
)
{
if
(
false
==
isUnknown
(
subIp
))
{
ip
=
subIp
;
break
;
}
}
}
return
StringUtils
.
substring
(
ip
,
0
,
255
);
}
/**
* 检测给定字符串是否为未知,多用于检测HTTP请求相关
*
* @param checkString 被检测的字符串
* @return 是否未知
*/
public
static
boolean
isUnknown
(
String
checkString
)
{
return
StringUtil
.
isBlank
(
checkString
)
||
"unknown"
.
equalsIgnoreCase
(
checkString
);
}
public
static
String
getLocalIP
()
{
try
{
return
InetAddress
.
getLocalHost
().
getHostAddress
();
...
...
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