Created
August 11, 2017 10:28
-
-
Save frankies/179e50455c4318cb3a30df1810e13b6f to your computer and use it in GitHub Desktop.
获取Web请求客户端的IP,
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
public abstract class RestfulWsHelper { | |
private final static String HEADER_NAME_REMOTE_IP = RestfulWsHelper.class.getSimpleName() + ".__REMOTE_CLIENT_ID__"; | |
public final static String UNKNOW_CLIENT_IP = "UNKNOWN"; | |
final static String[] SEARCHING_HEADERS = new String[] { | |
"x-forwarded-for", | |
"Proxy-Client-IP", | |
"WL-Proxy-Client-IP" | |
}; | |
final static String[] DEFAULT_HOST_IPS = new String[] {"127.0.0.1", "0:0:0:0:0:0:0:1"}; | |
/** | |
* @param request | |
*/ | |
public static Headers<String> getAdditionalHeaders(HttpServletRequest request) { | |
Headers<String> additionals = new Headers<String>(); | |
additionals.add(HEADER_NAME_REMOTE_IP, getRemoteClientIpAddressFromRequest(request)); | |
return additionals; | |
} | |
/** | |
* @param request | |
* @return | |
*/ | |
public static String getRemoteHostIp(HttpRequest request) { | |
return getFirstHeaderValue(request, HEADER_NAME_REMOTE_IP ); | |
} | |
/** | |
* @param request | |
* @param headerName | |
* @return | |
*/ | |
private static String getFirstHeaderValue(HttpRequest request, String headerName) { | |
String value = null; | |
if(request.getHttpHeaders() !=null) { | |
List<String> hs = request.getHttpHeaders().getRequestHeader(HEADER_NAME_REMOTE_IP); | |
if(hs !=null && !hs.isEmpty()) { | |
value = hs.iterator().next(); | |
} | |
} | |
return value ; | |
} | |
/** | |
* @param request | |
* @return | |
*/ | |
public static String getRemoteClientIpAddressFromRequest(HttpServletRequest request) { | |
if(request == null) { | |
return UNKNOW_CLIENT_IP; | |
} | |
String ipAddress = null; | |
for (String sh : SEARCHING_HEADERS) { | |
if(isNotEmpty(request.getHeader(sh))) { | |
ipAddress = sh; | |
break; | |
} | |
} | |
if(!isNotEmpty(ipAddress)) { | |
ipAddress = request.getRemoteAddr(); | |
for (String hip : DEFAULT_HOST_IPS) { | |
if(ipAddress.equals(hip)) { //It's from local machine. | |
try { | |
ipAddress = InetAddress.getLocalHost().getHostAddress(); | |
} catch (UnknownHostException e) { | |
//Ignore! | |
} | |
break; | |
} | |
} | |
} | |
//Return ipAddress. | |
if(ipAddress ==null) { | |
ipAddress = UNKNOW_CLIENT_IP; | |
}else if(ipAddress!=null){ | |
if(ipAddress.length()>15 && ipAddress.indexOf(",")>0){ //If there're multiple proxy servers. | |
ipAddress = ipAddress.substring(0,ipAddress.indexOf(",")); | |
} | |
} | |
return ipAddress; | |
} | |
/** | |
* @param input | |
* @return | |
*/ | |
private static boolean isNotEmpty(String input) { | |
return input!=null && input.trim().length()>0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment