Java【获取客户端真实IP地址】
Java-Web获取客户端真实IP:
发生的场景:服务器端接收客户端请求的时候,一般需要进行签名验证,客户端IP限定等情况,在进行客户端IP限定的时候,需要首先获取该真实的IP。一般分为两种情况:
方式一、客户端未经过代理,直接访问服务器端(nginx,squid,haproxy);
方式二、客户端通过多级代理,最终到达服务器端(nginx,squid,haproxy);
客户端请求信息都包含在HttpServletRequest中,可以通过方法getRemoteAddr()获得该客户端IP。
方式一形式,可以直接获得该客户端真实IP。
方式二中通过代理的形式,此时经过多级反向的代理,通过方法getRemoteAddr()得不到客户端真实IP,可以通过x-forwarded-for获得转发后请求信息。当客户端请求被转发,IP将会追加在其后并以逗号隔开,例如:10.47.103.13,4.2.2.2,10.96.112.230。
请求中的参数:
request.getHeader("x-forwarded-for") : 10.47.103.13,4.2.2.2,10.96.112.230
request.getHeader("X-Real-IP") : 10.47.103.13
request.getRemoteAddr():10.96.112.230
客户端访问经过转发,IP将会追加在其后并以逗号隔开。最终准确的客户端信息为:
-
- x-forwarded-for 不为空,则为逗号前第一个IP ;
- X-Real-IP不为空,则为该IP ;
- 否则为getRemoteAddr() ;
相关请求头的解释:
- X-Forwarded-For :这是一个 Squid 开发的字段,只有在通过了HTTP代理或者负载均衡服务器时才会添加该项。
- Proxy-Client-IP/WL- Proxy-Client-IP :这个一般是经过apache http服务器的请求才会有,用apache http做代理时一般会加上Proxy-Client-IP请求头,而WL-Proxy-Client-IP是他的weblogic插件加上的头。
- HTTP_CLIENT_IP :有些代理服务器会加上此请求头。
- X-Real-IP :nginx代理一般会加上此请求头。
1 /** 2 * 获取用户真实IP地址,不使用request.getRemoteAddr()的原因是有可能用户使用了代理软件方式避免真实IP地址, 3 * 可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值 4 */ 5 private String getIpAddr(HttpServletRequest request) { 6 String ip = request.getHeader("x-forwarded-for"); 7 System.out.println("x-forwarded-for ip: " + ip); 8 if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) { 9 // 多次反向代理后会有多个ip值,第一个ip才是真实ip 10 if( ip.indexOf(",")!=-1 ){ 11 ip = ip.split(",")[0]; 12 } 13 } 14 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 15 ip = request.getHeader("Proxy-Client-IP"); 16 System.out.println("Proxy-Client-IP ip: " + ip); 17 } 18 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 19 ip = request.getHeader("WL-Proxy-Client-IP"); 20 System.out.println("WL-Proxy-Client-IP ip: " + ip); 21 } 22 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 23 ip = request.getHeader("HTTP_CLIENT_IP"); 24 System.out.println("HTTP_CLIENT_IP ip: " + ip); 25 } 26 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 27 ip = request.getHeader("HTTP_X_FORWARDED_FOR"); 28 System.out.println("HTTP_X_FORWARDED_FOR ip: " + ip); 29 } 30 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 31 ip = request.getHeader("X-Real-IP"); 32 System.out.println("X-Real-IP ip: " + ip); 33 } 34 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 35 ip = request.getRemoteAddr(); 36 System.out.println("getRemoteAddr ip: " + ip); 37 } 38 System.out.println("获取客户端ip: " + ip); 39 return ip; 40 }
1 import javax.servlet.http.HttpServletRequest; 2 3 /** 4 * IP校验 5 */ 6 public class IPUtils { 7 8 public static String getClientAddress(HttpServletRequest request) { 9 if (request == null) { 10 return "unknown"; 11 } 12 String ip = request.getHeader("x-forwarded-for"); 13 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 14 ip = request.getHeader("Proxy-Client-IP"); 15 } 16 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 17 ip = request.getHeader("X-Forwarded-For"); 18 } 19 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 20 ip = request.getHeader("WL-Proxy-Client-IP"); 21 } 22 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 23 ip = request.getHeader("X-Real-IP"); 24 } 25 26 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 27 ip = request.getRemoteAddr(); 28 } 29 return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip; 30 } 31 32 }
1 public String getIpAddr(HttpServletRequest request){ 2 String ipAddress = request.getHeader("x-forwarded-for"); 3 if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { 4 ipAddress = request.getHeader("Proxy-Client-IP"); 5 } 6 if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { 7 ipAddress = request.getHeader("WL-Proxy-Client-IP"); 8 } 9 if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { 10 ipAddress = request.getRemoteAddr(); 11 if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){ 12 //根据网卡取本机配置的IP 13 InetAddress inet=null; 14 try { 15 inet = InetAddress.getLocalHost(); 16 } catch (UnknownHostException e) { 17 e.printStackTrace(); 18 } 19 ipAddress= inet.getHostAddress(); 20 } 21 } 22 //对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 23 if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15 24 if(ipAddress.indexOf(",")>0){ 25 ipAddress = ipAddress.substring(0,ipAddress.indexOf(",")); 26 } 27 } 28 return ipAddress; 29 }
太平洋网络IP地址查询Web接口:http://whois.pconline.com.cn/
1 import java.io.BufferedReader; 2 import java.io.DataOutputStream; 3 import java.io.IOException; 4 import java.io.InputStreamReader; 5 import java.io.UnsupportedEncodingException; 6 import java.net.HttpURLConnection; 7 import java.net.URL; 8 9 /** 10 * 根据IP地址获取详细的地域信息 第一个方法是传入ip获取真实地址 最后一个方法是获取访问者真实ip 即使通过Nginx多层代理也可以获取 11 */ 12 public class AddressUtils { 13 14 public static String getAddresses(String content, String encodingString) throws UnsupportedEncodingException { 15 // 这里调用pconline的接口 16 String urlStr = "http://ip.taobao.com/service/getIpInfo.php"; 17 // 从http://whois.pconline.com.cn取得IP所在的省市区信息 18 String returnStr = getResult(urlStr, content, encodingString); 19 if (returnStr != null) { 20 // 处理返回的省市区信息 21 System.out.println(returnStr); 22 String[] temp = returnStr.split(","); 23 if (temp.length < 3) { 24 return "0";// 无效IP,局域网测试 25 } 26 String country = ""; 27 String area = ""; 28 String region = ""; 29 String city = ""; 30 String county = ""; 31 String isp = ""; 32 for (int i = 0; i < temp.length; i++) { 33 switch (i) { 34 case 1: 35 country = (temp[i].split(":"))[2].replaceAll("\"", ""); 36 country = decodeUnicode(country);// 国家 37 break; 38 // case 3: 39 // area = (temp[i].split(":"))[1].replaceAll("\"", ""); 40 // area =decodeUnicode(area);//地区 41 // break; 42 case 5: 43 region = (temp[i].split(":"))[1].replaceAll("\"", ""); 44 region = decodeUnicode(region);// 省份 45 break; 46 case 7: 47 city = (temp[i].split(":"))[1].replaceAll("\"", ""); 48 city = decodeUnicode(city);// 市区 49 break; 50 case 9: 51 county = (temp[i].split(":"))[1].replaceAll("\"", ""); 52 county = decodeUnicode(county);// 地区 53 break; 54 case 11: 55 isp = (temp[i].split(":"))[1].replaceAll("\"", ""); 56 isp = decodeUnicode(isp);// ISP公司 57 break; 58 } 59 } 60 System.out.println(country + area + "=" + region + "=" + city + "=" + county + "=" + isp); 61 StringBuffer sb = new StringBuffer(country).append(region).append(city).append(county).append(" ") 62 .append(isp); 63 return sb.toString(); 64 } 65 return null; 66 } 67 68 /** 69 * @param urlStr 请求的地址 70 * @param content 请求的参数 格式为:name=xxx&pwd=xxx 71 * @param encoding 服务器端请求编码。如GBK,UTF-8等 72 * @return 73 */ 74 private static String getResult(String urlStr, String content, String encoding) { 75 URL url = null; 76 HttpURLConnection connection = null; 77 try { 78 url = new URL(urlStr); 79 connection = (HttpURLConnection) url.openConnection();// 新建连接实例 80 connection.setConnectTimeout(3000);// 设置连接超时时间,单位毫秒 81 connection.setReadTimeout(3000);// 设置读取数据超时时间,单位毫秒 82 connection.setDoOutput(true);// 是否打开输出流 true|false 83 connection.setDoInput(true);// 是否打开输入流true|false 84 connection.setRequestMethod("POST");// 提交方法POST|GET 85 connection.setUseCaches(false);// 是否缓存true|false 86 connection.connect();// 打开连接端口 87 DataOutputStream out = new DataOutputStream(connection.getOutputStream());// 打开输出流往对端服务器写数据 88 out.writeBytes(content);// 写数据(提交表单) 89 out.flush();// 刷新 90 out.close();// 关闭输出流 91 // 往对端写完数据对端服务器返回数据,以BufferedReader流来读取 92 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), encoding)); 93 StringBuffer buffer = new StringBuffer(); 94 String line = ""; 95 while ((line = reader.readLine()) != null) { 96 buffer.append(line); 97 } 98 reader.close(); 99 return buffer.toString(); 100 } catch (IOException e) { 101 e.printStackTrace(); 102 } finally { 103 if (connection != null) { 104 connection.disconnect();// 关闭连接 105 } 106 } 107 return null; 108 } 109 110 /** 111 * unicode 转换成 中文 112 */ 113 public static String decodeUnicode(String theString) { 114 char aChar; 115 int len = theString.length(); 116 StringBuffer outBuffer = new StringBuffer(len); 117 for (int x = 0; x < len;) { 118 aChar = theString.charAt(x++); 119 if (aChar == '\\') { 120 aChar = theString.charAt(x++); 121 if (aChar == 'u') { 122 int value = 0; 123 for (int i = 0; i < 4; i++) { 124 aChar = theString.charAt(x++); 125 switch (aChar) { 126 case '0': 127 case '1': 128 case '2': 129 case '3': 130 case '4': 131 case '5': 132 case '6': 133 case '7': 134 case '8': 135 case '9': 136 value = (value << 4) + aChar - '0'; 137 break; 138 case 'a': 139 case 'b': 140 case 'c': 141 case 'd': 142 case 'e': 143 case 'f': 144 value = (value << 4) + 10 + aChar - 'a'; 145 break; 146 case 'A': 147 case 'B': 148 case 'C': 149 case 'D': 150 case 'E': 151 case 'F': 152 value = (value << 4) + 10 + aChar - 'A'; 153 break; 154 default: 155 throw new IllegalArgumentException("Malformed encoding."); 156 } 157 } 158 outBuffer.append((char) value); 159 } else { 160 if (aChar == 't') { 161 aChar = '\t'; 162 } else if (aChar == 'r') { 163 aChar = '\r'; 164 } else if (aChar == 'n') { 165 aChar = '\n'; 166 } else if (aChar == 'f') { 167 aChar = '\f'; 168 } 169 outBuffer.append(aChar); 170 } 171 } else { 172 outBuffer.append(aChar); 173 } 174 } 175 return outBuffer.toString(); 176 } 177 178 // 测试 179 public static void main(String[] args) { 180 AddressUtils addressUtils = new AddressUtils(); 181 182 /** 183 * 测试IP:111.121.72.101 中国贵州省贵阳市 电信 184 */ 185 String ip = "111.121.72.101"; 186 String address = ""; 187 try { 188 address = addressUtils.getAddresses("ip=" + ip, "utf-8"); 189 } catch (UnsupportedEncodingException e) { 190 e.printStackTrace(); 191 } catch (Exception e) { 192 e.printStackTrace(); 193 } 194 System.out.println(address);//中国贵州省贵阳市 电信 195 } 196 }