-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.xwolf.boot.utils; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
/** | ||
* IP工具 | ||
* @author xwolf | ||
* @since 1.8 | ||
*/ | ||
public class IPUtil { | ||
|
||
/** | ||
* 获取IP地址 | ||
* @param request | ||
* @return | ||
*/ | ||
public static String getIP(HttpServletRequest request) { | ||
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 ip; | ||
} | ||
|
||
|
||
|
||
} |