Java里面獲取當前服務器(linux環境)的IP地址--與請求者的真實IP


  1 package com.wfd360.Util;
  2 
  3 import javax.servlet.http.HttpServletRequest;
  4 import java.net.Inet4Address;
  5 import java.net.InetAddress;
  6 import java.net.NetworkInterface;
  7 import java.net.SocketException;
  8 import java.util.Enumeration;
  9 import java.util.HashMap;
 10 import java.util.Map;
 11 
 12 /**
 13  * @Copyright (C) 
 14  * @Author: 姿勢帝
 15  * @Date: 6/3 16:37
 16  * @Description:
 17  */
 18 public class HostUtil {
 19     private static String ipstr = "127.0.0.1";
 20     private static Map<String, String> map = new HashMap();
 21 
 22     static {
 23         map.put("ip", getLinuxLocalIp());
 24     }
 25 
 26     public static String getHostIp() {
 27         String ip = map.get("ip");
 28         if (ip == null) {
 29             String ip1 = getLinuxLocalIp();
 30             map.put("ip", ip1);
 31             return ip1;
 32         }
 33         return ip;
 34     }
 35     
 36 
 37     /**
 38      * 獲取Linux下的IP地址
 39      *
 40      * @return IP地址
 41      * @throws SocketException
 42      */
 43     private static String getLinuxLocalIp() {
 44         String ip = "";
 45         try {
 46             for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
 47                 NetworkInterface intf = en.nextElement();
 48                 String name = intf.getName();
 49                 if (!name.contains("docker") && !name.contains("lo")) {
 50                     for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
 51                         InetAddress inetAddress = enumIpAddr.nextElement();
 52                         if (!inetAddress.isLoopbackAddress()) {
 53                             String ipaddress = inetAddress.getHostAddress().toString();
 54                             if (!ipaddress.contains("::") && !ipaddress.contains("0:0:") && !ipaddress.contains("fe80")) {
 55                                 ip = ipaddress;
 56                             }
 57                         }
 58                     }
 59                 }
 60             }
 61         } catch (SocketException ex) {
 62             ip = "127.0.0.1";
 63             ex.printStackTrace();
 64         }
 65         return ip;
 66     }
 67 
 68     /**
 69      * 獲取請求者的ip地址
 70      *
 71      * @param request
 72      * @return
 73      */
 74     public static String getIpAdrress(HttpServletRequest request) {
 75         String ip = null;
 76 
 77         //X-Forwarded-For:Squid 服務代理
 78         String ipAddresses = request.getHeader("X-Forwarded-For");
 79         String unknown = "unknown";
 80         if (ipAddresses == null || ipAddresses.length() == 0 || unknown.equalsIgnoreCase(ipAddresses)) {
 81             //Proxy-Client-IP:apache 服務代理
 82             ipAddresses = request.getHeader("Proxy-Client-IP");
 83         }
 84 
 85         if (ipAddresses == null || ipAddresses.length() == 0 || unknown.equalsIgnoreCase(ipAddresses)) {
 86             //WL-Proxy-Client-IP:weblogic 服務代理
 87             ipAddresses = request.getHeader("WL-Proxy-Client-IP");
 88         }
 89 
 90         if (ipAddresses == null || ipAddresses.length() == 0 || unknown.equalsIgnoreCase(ipAddresses)) {
 91             //HTTP_CLIENT_IP:有些代理服務器
 92             ipAddresses = request.getHeader("HTTP_CLIENT_IP");
 93         }
 94 
 95         if (ipAddresses == null || ipAddresses.length() == 0 || unknown.equalsIgnoreCase(ipAddresses)) {
 96             //X-Real-IP:nginx服務代理
 97             ipAddresses = request.getHeader("X-Real-IP");
 98         }
 99 
100         //有些網絡通過多層代理,那么獲取到的ip就會有多個,一般都是通過逗號(,)分割開來,並且第一個ip為客戶端的真實IP
101         if (ipAddresses != null && ipAddresses.length() != 0) {
102             ip = ipAddresses.split(",")[0];
103         }
104 
105         //還是不能獲取到,最后再通過request.getRemoteAddr();獲取
106         if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ipAddresses)) {
107             ip = request.getRemoteAddr();
108         }
109         return ip;
110     }
111 
112 
113 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM