前言
最近在做一个篮球社交网站,根据系统的设计的需求,为防止用户恶意灌水及发布一些不合规言论,需要记录发表评论的用户的真实IP地址信息。本文记录了JAVA获取真实IP地址的实现方法,如果你的系统也有类似的需要获取用户IP地址的需求,可以参考一下这篇的实现方式。
JAVA获取真实IP地址代码如下:
/** * 获取用户真实IP地址 * 堆代码 duidaima.com * @param request * @return * */ public static String getAddress(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(); if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) { // 根据网卡取本机配置的IP ipAddress = getLocalIP(); } } // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length() // = 15 if (ipAddress.indexOf(",") > 0) { ipAddress = ipAddress.substring(0, ipAddress.indexOf(",")); } } return ipAddress; } /** * 获取外网IP * * @return */ public static String getIp() { String ip = ""; String chinaz = "http://ip.chinaz.com"; StringBuilder inputLine = new StringBuilder(); String read = ""; URL url = null; HttpURLConnection urlConnection = null; BufferedReader in = null; try { url = new URL(chinaz); urlConnection = (HttpURLConnection) url.openConnection(); in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8")); while ((read = in.readLine()) != null) { inputLine.append(read + "\r\n"); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } Pattern p = Pattern.compile("\\<dd class\\=\"fz24\">(.*?)\\<\\/dd>"); Matcher m = p.matcher(inputLine.toString()); if (m.find()) { String ipstr = m.group(1); ip = ipstr; } return ip; } /** * 取当前系统站点本地地址 linux下 和 window下可用 * * @return */ public static String getLocalIP() { String sIP = ""; InetAddress ip = null; try { // 如果是Windows操作系统 if (isWindowsOS()) { ip = InetAddress.getLocalHost(); } // 如果是Linux操作系统 else { boolean bFindIP = false; Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { if (bFindIP) { break; } NetworkInterface ni = netInterfaces.nextElement(); // ----------特定情况,可以考虑用ni.getName判断 // 遍历所有ip Enumeration<InetAddress> ips = ni.getInetAddresses(); while (ips.hasMoreElements()) { ip = ips.nextElement(); if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() // 127.开头的都是lookback地址 && ip.getHostAddress().indexOf(":") == -1) { bFindIP = true; break; } } } } } catch (Exception e) { e.printStackTrace(); } if (null != ip) { sIP = ip.getHostAddress(); } return sIP; } /** * 判断当前系统是否是Windows操作系统 * * @return */ public static boolean isWindowsOS() { if ("//".equals(File.separator)) { return true; } else { return false; } }总结:
以上就是JAVA获取真实IP地址的实现方式,这边获取外网IP的地址调用了第三方网站网站的查询系统,通过正则表达式过滤获取外网的IP地址,这边如果你们有第三方的获取外网IP地址的API接口,则直接调用接口更方便。