Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
platform
/
platform-process-server
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 3d93b66c
authored
Sep 20, 2023
by
chizw
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
test add code
1 parent
a567564e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
106 additions
and
0 deletions
platform-process/src/main/java/cn/com/glsx/process/util/IpUtils.java
platform-process/src/main/java/cn/com/glsx/process/util/IpUtils.java
0 → 100644
View file @
3d93b66
package
cn
.
com
.
glsx
.
process
.
util
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.server.reactive.ServerHttpRequest
;
import
java.net.InetAddress
;
import
java.net.UnknownHostException
;
import
java.util.Map
;
import
java.util.Objects
;
/**
* @author throwable
* @version v1
* @description IP提取工具
* @since 2020/12/26 12:58
*/
public
enum
IpUtils
{
/**
* 单例
*/
X
;
private
static
final
String
UNKNOWN_VALUE
=
"unknown"
;
private
static
final
String
LOCALHOST_V0
=
"0.0.0.0"
;
private
static
final
String
LOCALHOST_V4
=
"127.0.0.1"
;
private
static
final
String
LOCALHOST_V6
=
"0:0:0:0:0:0:0:1"
;
private
static
final
String
X_FORWARDED_FOR_KEY
=
"X-Forwarded-For"
;
private
static
final
String
PROXY_CLIENT_IP_KEY
=
"Proxy-Client-IP"
;
private
static
final
String
WL_PROXY_CLIENT_IP_KEY
=
"WL-Proxy-Client-IP"
;
private
static
final
String
HTTP_CLIENT_IP_KEY
=
"HTTP_CLIENT_IP"
;
private
static
final
String
HTTP_X_FORWARDED_FOR_KEY
=
"HTTP_X_FORWARDED_FOR"
;
/**
* 提取客户端IP
*
* @return String
*/
public
String
extractClientIp
(
Map
<
String
,
String
>
headers
,
String
remoteHostName
)
{
String
clientIp
=
headers
.
get
(
X_FORWARDED_FOR_KEY
);
if
(
StringUtils
.
isBlank
(
clientIp
)
||
UNKNOWN_VALUE
.
equalsIgnoreCase
(
clientIp
))
{
clientIp
=
headers
.
get
(
PROXY_CLIENT_IP_KEY
);
}
if
(
StringUtils
.
isBlank
(
clientIp
)
||
UNKNOWN_VALUE
.
equalsIgnoreCase
(
clientIp
))
{
clientIp
=
headers
.
get
(
WL_PROXY_CLIENT_IP_KEY
);
}
if
(
StringUtils
.
isBlank
(
clientIp
)
||
UNKNOWN_VALUE
.
equalsIgnoreCase
(
clientIp
))
{
clientIp
=
headers
.
get
(
HTTP_CLIENT_IP_KEY
);
}
if
(
StringUtils
.
isBlank
(
clientIp
)
||
UNKNOWN_VALUE
.
equalsIgnoreCase
(
clientIp
))
{
clientIp
=
headers
.
get
(
HTTP_X_FORWARDED_FOR_KEY
);
}
if
(
StringUtils
.
isBlank
(
clientIp
)
||
UNKNOWN_VALUE
.
equalsIgnoreCase
(
clientIp
))
{
if
(
StringUtils
.
isNotBlank
(
remoteHostName
))
{
clientIp
=
remoteHostName
;
}
}
if
(
Objects
.
equals
(
LOCALHOST_V4
,
clientIp
)
||
Objects
.
equals
(
LOCALHOST_V6
,
clientIp
))
{
try
{
clientIp
=
InetAddress
.
getLocalHost
().
getHostAddress
();
}
catch
(
UnknownHostException
unknownhostexception
)
{
clientIp
=
LOCALHOST_V0
;
}
}
return
clientIp
;
}
/**
* 通过ServerHttpRequest提取客户端IP
*
* @param request request
* @return String
*/
public
String
extractClientIp
(
ServerHttpRequest
request
)
{
HttpHeaders
headers
=
request
.
getHeaders
();
String
clientIp
=
headers
.
getFirst
(
X_FORWARDED_FOR_KEY
);
if
(
StringUtils
.
isBlank
(
clientIp
)
||
UNKNOWN_VALUE
.
equalsIgnoreCase
(
clientIp
))
{
clientIp
=
headers
.
getFirst
(
PROXY_CLIENT_IP_KEY
);
}
if
(
StringUtils
.
isBlank
(
clientIp
)
||
UNKNOWN_VALUE
.
equalsIgnoreCase
(
clientIp
))
{
clientIp
=
headers
.
getFirst
(
WL_PROXY_CLIENT_IP_KEY
);
}
if
(
StringUtils
.
isBlank
(
clientIp
)
||
UNKNOWN_VALUE
.
equalsIgnoreCase
(
clientIp
))
{
clientIp
=
headers
.
getFirst
(
HTTP_CLIENT_IP_KEY
);
}
if
(
StringUtils
.
isBlank
(
clientIp
)
||
UNKNOWN_VALUE
.
equalsIgnoreCase
(
clientIp
))
{
clientIp
=
headers
.
getFirst
(
HTTP_X_FORWARDED_FOR_KEY
);
}
if
(
StringUtils
.
isBlank
(
clientIp
)
||
UNKNOWN_VALUE
.
equalsIgnoreCase
(
clientIp
))
{
if
(
Objects
.
nonNull
(
request
.
getRemoteAddress
()))
{
clientIp
=
request
.
getRemoteAddress
().
getHostName
();
}
}
if
(
Objects
.
equals
(
LOCALHOST_V4
,
clientIp
)
||
Objects
.
equals
(
LOCALHOST_V6
,
clientIp
))
{
try
{
clientIp
=
InetAddress
.
getLocalHost
().
getHostAddress
();
}
catch
(
UnknownHostException
unknownhostexception
)
{
clientIp
=
LOCALHOST_V0
;
}
}
return
clientIp
;
}
}
Write
Preview
Markdown
is supported
Attach a file
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 post a comment