1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
| package com.service.taobao.util;
import org.springframework.context.annotation.Configuration;
import javax.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.regex.Pattern;
public class WhiteIPInterceptor { private static Pattern pattern = Pattern .compile("(1\\d{1,2}|2[0-4]\\d|25[0-5]|\\d{1,2})\\." + "(1\\d{1,2}|2[0-4]\\d|25[0-5]|\\d{1,2})\\." + "(1\\d{1,2}|2[0-4]\\d|25[0-5]|\\d{1,2})\\." + "(1\\d{1,2}|2[0-4]\\d|25[0-5]|\\d{1,2})"); private static final String DEFAULT_ALLOW_ALL_FLAG = "*"; private static final String DEFAULT_DENY_ALL_FLAG = "0";
public static String getIpAddr(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.getHeader("HTTP_CLIENT_IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } if ("0:0:0:0:0:0:0:1".equals(ip)) { ip = "127.0.0.1"; } return ip; }
private static Set<String> getAvaliIpList(String allowIp) { String[] splitRex = allowIp.split(";"); Set<String> ipList = new HashSet<String>(splitRex.length); for (String allow : splitRex) { allow = allow.trim(); if (allow.contains("*")) { String[] ips = allow.split("\\."); String[] from = new String[] { "0", "0", "0", "0" }; String[] end = new String[] { "255", "255", "255", "255" }; List<String> tem = new ArrayList<String>(); for (int i = 0; i < ips.length; i++) if (ips[i].indexOf("*") > -1) { tem = complete(ips[i]); from[i] = null; end[i] = null; } else { from[i] = ips[i]; end[i] = ips[i]; }
StringBuilder fromIP = new StringBuilder(); StringBuilder endIP = new StringBuilder(); for (int i = 0; i < 4; i++) if (from[i] != null) { fromIP.append(from[i]).append("."); endIP.append(end[i]).append("."); } else { fromIP.append("[*]."); endIP.append("[*]."); } fromIP.deleteCharAt(fromIP.length() - 1); endIP.deleteCharAt(endIP.length() - 1);
for (String s : tem) { String ip = fromIP.toString().replace("[*]", s.split(";")[0]) + "-" + endIP.toString().replace("[*]", s.split(";")[1]); if (validate(ip)) { ipList.add(ip); } } } else if (allow.contains("/")) { ipList.add(allow); } else { if (validate(allow)) { ipList.add(allow); } } } return ipList; }
private static List<String> complete(String arg) { List<String> com = new ArrayList<String>(); int len = arg.length(); if (len == 1) { com.add("0;255"); } else if (len == 2) { String s1 = complete(arg, 1); if (s1 != null) com.add(s1); String s2 = complete(arg, 2); if (s2 != null) com.add(s2); } else { String s1 = complete(arg, 1); if (s1 != null) com.add(s1); } return com; }
private static String complete(String arg, int length) { String from = ""; String end = ""; if (length == 1) { from = arg.replace("*", "0"); end = arg.replace("*", "9"); } else { from = arg.replace("*", "00"); end = arg.replace("*", "99"); } if (Integer.valueOf(from) > 255) return null; if (Integer.valueOf(end) > 255) end = "255"; return from + ";" + end; }
private static boolean validate(String ip) { String[] temp = ip.split("-"); for (String s : temp) if (!pattern.matcher(s).matches()) { return false; } return true; }
private static boolean isPermited(String ip, Set<String> ipList) { if (ipList.isEmpty() || ipList.contains(ip)) return true; for (String allow : ipList) { if (allow.indexOf("-") > -1) { String[] tempAllow = allow.split("-"); String[] from = tempAllow[0].split("\\."); String[] end = tempAllow[1].split("\\."); String[] tag = ip.split("\\."); boolean check = true; for (int i = 0; i < 4; i++) { int s = Integer.valueOf(from[i]); int t = Integer.valueOf(tag[i]); int e = Integer.valueOf(end[i]); if (!(s <= t && t <= e)) { check = false; break; } } if (check) return true; } else if (allow.contains("/")) { int splitIndex = allow.indexOf("/"); String ipSegment = allow.substring(0, splitIndex); String netmask = allow.substring(splitIndex + 1); long ipLong = ipToLong(ip); long maskLong=(2L<<32 -1) -(2L << Integer.valueOf(32-Integer.valueOf(netmask))-1); String calcSegment = longToIP(ipLong & maskLong); if(ipSegment.equals(calcSegment))return true; } } return false; }
public static boolean isPermitedByRequest(HttpServletRequest request, String ipWhiteConfig) { String ip = getIpAddr(request); if (null == ip || "".equals(ip) || null == ipWhiteConfig) return false; if(!pattern.matcher(ip).matches()) return false; if (DEFAULT_ALLOW_ALL_FLAG.equals(ipWhiteConfig)) return true; if (DEFAULT_DENY_ALL_FLAG.equals(ipWhiteConfig)) return false; Set<String> ipList = getAvaliIpList(ipWhiteConfig.replaceAll(";", ";")); return isPermited(ip, ipList); }
public static boolean isPermited(String ip, String ipWhiteConfig) { if (null == ip || "".equals(ip) || null == ipWhiteConfig) return false; if(!pattern.matcher(ip).matches()) return false; if (DEFAULT_ALLOW_ALL_FLAG.equals(ipWhiteConfig)) return true; if (DEFAULT_DENY_ALL_FLAG.equals(ipWhiteConfig)) return false; Set<String> ipList = getAvaliIpList(ipWhiteConfig.replaceAll(";", ";")); return isPermited(ip, ipList); }
private static long ipToLong(String strIP) { long[] ip = new long[4]; int position1 = strIP.indexOf("."); int position2 = strIP.indexOf(".", position1 + 1); int position3 = strIP.indexOf(".", position2 + 1); ip[0] = Long.parseLong(strIP.substring(0, position1)); ip[1] = Long.parseLong(strIP.substring(position1 + 1, position2)); ip[2] = Long.parseLong(strIP.substring(position2 + 1, position3)); ip[3] = Long.parseLong(strIP.substring(position3 + 1)); return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3]; }
private static String longToIP(long longIP) { StringBuilder sb = new StringBuilder(""); sb.append(String.valueOf(longIP >>> 24)); sb.append("."); sb.append(String.valueOf((longIP & 0x00FFFFFF) >>> 16)); sb.append("."); sb.append(String.valueOf((longIP & 0x0000FFFF) >>> 8)); sb.append("."); sb.append(String.valueOf(longIP & 0x000000FF)); return sb.toString(); }
public static void main(String[] args) { String ipWhilte = "1.168.1.1;" + "192.*; " + "192.168.1.1-192.168.1.3; " + "25.168.4.0/24 "; System.out.println(WhiteIPInterceptor.isPermited("192.168.1.1",ipWhilte)); } }
|