-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
4cecce7
commit 4183d40
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
...iption/pianotranscriptioncli/src/main/java/com/pianotranscriptioncli/utils/HttpUtils.java
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,31 @@ | ||
package com.pianotranscriptioncli.utils; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
public class HttpUtils { | ||
|
||
/** | ||
* Get the IP address of client machine. | ||
* @param request | ||
* @return IP address | ||
*/ | ||
public static String getIpAddr(HttpServletRequest request) { | ||
String ipAddress = request.getHeader("x-forwarded-for"); | ||
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { | ||
ipAddress = request.getHeader("Proxy-Client-IP"); | ||
} | ||
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { | ||
ipAddress = request.getHeader("WL-Proxy-Client-IP"); | ||
} | ||
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { | ||
ipAddress = request.getRemoteAddr(); | ||
} | ||
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 | ||
if (ipAddress != null && ipAddress.length() > 15) { | ||
if (ipAddress.indexOf(",") > 0) { | ||
ipAddress = ipAddress.substring(0, ipAddress.indexOf(",")); | ||
} | ||
} | ||
return ipAddress; | ||
} | ||
} |