Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
cloud-platform
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
youjj
cloud-platform
Commits
82ea6d4d
Commit
82ea6d4d
authored
Jul 06, 2019
by
hezhen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
13
parent
485b9d48
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
82 additions
and
0 deletions
+82
-0
IpUtil.java
.../java/com/github/wxiaoqi/security/common/util/IpUtil.java
+82
-0
No files found.
ace-common/src/main/java/com/github/wxiaoqi/security/common/util/IpUtil.java
0 → 100644
View file @
82ea6d4d
package
com
.
github
.
wxiaoqi
.
security
.
common
.
util
;
import
java.net.InetAddress
;
import
java.net.NetworkInterface
;
import
java.util.Enumeration
;
import
javax.servlet.http.HttpServletRequest
;
public
class
IpUtil
{
/**
* 获取服务器本地的ip地址
*
* @return
*/
public
static
String
getLocalIp
()
{
InetAddress
ip
=
null
;
String
localIp
=
"127.0.0.2"
;
try
{
Enumeration
netInterfaces
=
NetworkInterface
.
getNetworkInterfaces
();
while
(
netInterfaces
.
hasMoreElements
())
{
NetworkInterface
ni
=
(
NetworkInterface
)
netInterfaces
.
nextElement
();
ip
=
(
InetAddress
)
ni
.
getInetAddresses
().
nextElement
();
if
(!
ip
.
isLoopbackAddress
()
&&
ip
.
getHostAddress
().
indexOf
(
":"
)
==
-
1
)
{
localIp
=
ip
.
getHostAddress
();
break
;
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
localIp
;
}
/**
* 获取客户机的ip地址
*
* @return
*/
public
static
String
getClientIp
(
HttpServletRequest
request
)
{
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
(
"WL-Proxy-Client-IP"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
ip
=
request
.
getRemoteAddr
();
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
ip
=
request
.
getHeader
(
"http_client_ip"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
ip
=
request
.
getHeader
(
"HTTP_X_FORWARDED_FOR"
);
}
// 如果是多级代理,那么取第一个ip为客户ip
if
(
ip
!=
null
&&
ip
.
indexOf
(
","
)
!=
-
1
)
{
ip
=
ip
.
substring
(
ip
.
lastIndexOf
(
","
)
+
1
,
ip
.
length
()).
trim
();
}
return
ip
;
}
public
static
long
transIP2Int
(
String
ip
)
{
String
[]
temp
=
ip
.
split
(
"\\."
);
int
[]
ipInt
=
new
int
[
temp
.
length
];
for
(
int
i
=
0
;
i
<
temp
.
length
;
i
++)
{
ipInt
[
i
]
=
new
Integer
(
temp
[
i
]).
intValue
();
}
long
ipNum
=
(
long
)
ipInt
[
0
]
*
256
*
256
*
256
+
ipInt
[
1
]
*
256
*
256
+
ipInt
[
2
]
*
256
+
ipInt
[
3
];
return
ipNum
;
}
}
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