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
Show 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
...
@@ -108,6 +108,11 @@
...
@@ -108,6 +108,11 @@
<artifactId>
commons-net
</artifactId>
<artifactId>
commons-net
</artifactId>
<version>
3.4
</version>
<version>
3.4
</version>
</dependency>
</dependency>
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-lang3
</artifactId>
<version>
3.12.0
</version>
</dependency>
<dependency>
<dependency>
<groupId>
dom4j
</groupId>
<groupId>
dom4j
</groupId>
<artifactId>
dom4j
</artifactId>
<artifactId>
dom4j
</artifactId>
...
@@ -151,7 +156,7 @@
...
@@ -151,7 +156,7 @@
</dependency>
</dependency>
<!-- 简体转繁体end -->
<!-- 简体转繁体end -->
<!-- 图片压缩 https://www.cnblogs.com/yinjing/p/12157562.html
-->
<!-- 图片压缩 https://www.cnblogs.com/yinjing/p/12157562.html
-->
<dependency>
<dependency>
<groupId>
net.coobird
</groupId>
<groupId>
net.coobird
</groupId>
<artifactId>
thumbnailator
</artifactId>
<artifactId>
thumbnailator
</artifactId>
...
...
src/main/java/com/egolm/common/web/ServletUtil.java
View file @
3bf651e3
...
@@ -13,29 +13,78 @@ import java.util.Map.Entry;
...
@@ -13,29 +13,78 @@ import java.util.Map.Entry;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
org.apache.commons.lang3.StringUtils
;
import
com.egolm.common.StringUtil
;
import
com.egolm.common.StringUtil
;
import
com.egolm.common.exception.PluginException
;
import
com.egolm.common.exception.PluginException
;
public
class
ServletUtil
{
public
class
ServletUtil
{
public
static
String
remoteIp
(
HttpServletRequest
request
)
{
public
static
String
remoteIp
(
HttpServletRequest
request
)
{
String
ip
=
request
.
getHeader
(
"X-Real_IP"
);
if
(
request
==
null
)
if
(
ip
==
null
||
ip
.
length
()
==
0
||
ip
.
equalsIgnoreCase
(
"unknown"
))
{
{
ip
=
request
.
getHeader
(
"X-Forwarded-For"
)
;
return
"unknown"
;
}
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
ip
.
equalsIgnoreCase
(
"unknown"
))
{
String
ip
=
request
.
getHeader
(
"x-forwarded-for"
);
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
ip
=
request
.
getHeader
(
"Proxy-Client-IP"
);
ip
=
request
.
getHeader
(
"Proxy-Client-IP"
);
}
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
ip
.
equalsIgnoreCase
(
"unknown"
))
{
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"
);
ip
=
request
.
getHeader
(
"WL-Proxy-Client-IP"
);
}
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
ip
.
equalsIgnoreCase
(
"unknown"
))
{
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
();
ip
=
request
.
getRemoteAddr
();
}
}
String
[]
ips
=
ip
.
trim
().
split
(
", "
);
return
ips
[
ips
.
length
-
1
].
trim
();
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
()
{
public
static
String
getLocalIP
()
{
try
{
try
{
return
InetAddress
.
getLocalHost
().
getHostAddress
();
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