zoukankan      html  css  js  c++  java
  • 多级代理下获取客户端真实IP

     1    /**
     2      * 获取当前网络ip
     3      * @param request
     4      * @return
     5      */
     6     public String getIpAddr(HttpServletRequest request){
     7         String ipAddress = request.getHeader("x-forwarded-for");
     8         if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
     9             ipAddress = request.getHeader("Proxy-Client-IP");
    10         }
    11         if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
    12             ipAddress = request.getHeader("WL-Proxy-Client-IP");
    13         }
    14         if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
    15             ipAddress = request.getRemoteAddr();
    16             if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){
    17                 //根据网卡取本机配置的IP
    18                 InetAddress inet=null;
    19                 try {
    20                     inet = InetAddress.getLocalHost();
    21                 } catch (UnknownHostException e) {
    22                     e.printStackTrace();
    23                 }
    24                 ipAddress= inet.getHostAddress();
    25             }
    26         }
    27         //对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
    28         if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15
    29             if(ipAddress.indexOf(",")>0){
    30                 ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
    31             }
    32         }
    33         return ipAddress;
    34     }
  • 相关阅读:
    Dictionaries and lists
    Looping and dictionaries
    Dictionary as a set of counters
    Dictionaries
    List exercise
    Lists and strings
    Copying lists
    Objects and values
    [luoguP1944] 最长括号匹配_NOI导刊2009提高(1)
    [luoguP1868] 饥饿的奶牛(DP)
  • 原文地址:https://www.cnblogs.com/codekey/p/4800537.html
Copyright © 2011-2022 走看看