zoukankan      html  css  js  c++  java
  • 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址;

     1 package com.utils;
     2 
     3 import org.slf4j.Logger;
     4 import org.slf4j.LoggerFactory;
     5 
     6 import javax.servlet.http.HttpServletRequest;
     7 import java.io.IOException;
     8 
     9 /**
    10  * 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址;
    11  * 
    12  */
    13 public final class NetworkUtil {
    14 
    15     private static final Logger LOG = LoggerFactory.getLogger(NetworkUtil.class);
    16 
    17     public final static String getIpAddress(HttpServletRequest request)
    18             throws IOException {
    19 
    20         String ip = request.getHeader("X-Forwarded-For");
    21 
    22 //        LOG.info("getIpAddress(HttpServletRequest) - X-Forwarded-For - String ip= {}" ,ip);
    23 
    24         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
    25             if (ip == null || ip.length() == 0
    26                     || "unknown".equalsIgnoreCase(ip)) {
    27                 ip = request.getHeader("Proxy-Client-IP");
    28 //                LOG.info("getIpAddress(HttpServletRequest) - Proxy-Client-IP - String ip= {}" , ip);
    29             }
    30             if (ip == null || ip.length() == 0
    31                     || "unknown".equalsIgnoreCase(ip)) {
    32                 ip = request.getHeader("WL-Proxy-Client-IP");
    33 //                LOG.info("getIpAddress(HttpServletRequest) - WL-Proxy-Client-IP - String ip= {}" , ip);
    34             }
    35             if (ip == null || ip.length() == 0
    36                     || "unknown".equalsIgnoreCase(ip)) {
    37                 ip = request.getHeader("HTTP_CLIENT_IP");
    38 //                LOG.info("getIpAddress(HttpServletRequest) - HTTP_CLIENT_IP - String ip= {}" , ip);
    39             }
    40             if (ip == null || ip.length() == 0
    41                     || "unknown".equalsIgnoreCase(ip)) {
    42                 ip = request.getHeader("HTTP_X_FORWARDED_FOR");
    43 //                LOG.info("getIpAddress(HttpServletRequest) - HTTP_X_FORWARDED_FOR - String ip= {}" , ip);
    44             }
    45             if (ip == null || ip.length() == 0
    46                     || "unknown".equalsIgnoreCase(ip)) {
    47                 ip = request.getRemoteAddr();
    48 //                LOG.info("getIpAddress(HttpServletRequest) - getRemoteAddr - String ip= {}" , ip);
    49             }
    50         } else if (ip.length() > 15) {
    51             String[] ips = ip.split(",");
    52             for (int index = 0; index < ips.length; index++) {
    53                 String strIp = (String) ips[index];
    54                 if (!("unknown".equalsIgnoreCase(strIp))) {
    55                     ip = strIp;
    56                     break;
    57                 }
    58             }
    59         }
    60         return ip;
    61     }
    62 }
  • 相关阅读:
    软件开发与定制报价
    C# HttpHelper 1.0正式版发布
    C#仿QQ皮肤-TextBox 控件实现
    HTML5学习笔记第一节(智能提示和视频音频标签)
    C#多线程|匿名委托传参数|测试您的网站能承受的压力|附源代码升级版
    JavascriptHelp
    我的个人博客论坛版建立啦!
    Win7 + VirtualBox安装Mac OS X雪豹操作系统图文详解
    Sql2005性能工具(SQL Server Profiler和数据库引擎优化顾问)使用方法详解
    Webcast 系列课程 NET最全,最权威的学习资源
  • 原文地址:https://www.cnblogs.com/YangshengQuan/p/8416683.html
Copyright © 2011-2022 走看看