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 }

 

posted @ 2019-07-17 14:22  李东平|一线码农  阅读(5416)  评论(0编辑  收藏  举报