博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java中使用HttpRequest获取用户真实IP地址端口
阅读量:2384 次
发布时间:2019-05-10

本文共 2309 字,大约阅读时间需要 7 分钟。

1 import javax.servlet.http.HttpServletRequest;   2    3 /**  4  * 自定义访问对象工具类  5  *   6  * 获取对象的IP地址等信息  7  * @author X-rapido  8  *  9  */  10 public class CusAccessObjectUtil {  11   12     /** 13      * 获取用户真实IP地址,不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址, 14 *  16      * 可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,究竟哪个才是真正的用户端的真实IP呢? 17      * 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。 18      *  19      * 如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130, 20      * 192.168.1.100 21      *  22      * 用户真实IP为: 192.168.1.110 23      *  24      * @param request 25      * @return 26      */  27     public static String getIpAddress(HttpServletRequest request) {  28         String ip = request.getHeader("x-forwarded-for");  29         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  30             ip = request.getHeader("Proxy-Client-IP");  31         }  32         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  33             ip = request.getHeader("WL-Proxy-Client-IP");  34         }  35         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  36             ip = request.getHeader("HTTP_CLIENT_IP");  37         }  38         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  39             ip = request.getHeader("HTTP_X_FORWARDED_FOR");  40         }  41         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  42             ip = request.getRemoteAddr();  43         }  44         return ip;  45     }  46       47 }

 /**

 *获取端口

 */

1 public void doGet(HttpServletRequest request, HttpServletResponse response) 2     throws ServletException, IOException { 3    String uri = request.getRequestURI();//返回请求行中的资源名称 4    String url = request.getRequestURL().toString();//获得客户端发送请求的完整url 5    String ip = request.getRemoteAddr();//返回发出请求的IP地址 6    String params = request.getQueryString();//返回请求行中的参数部分 7    String host=request.getRemoteHost();//返回发出请求的客户机的主机名 8    int port =request.getRemotePort();//返回发出请求的客户机的端口号。 9    System.out.println(ip);10    System.out.println(url);11    System.out.println(uri);12    System.out.println(params);13    System.out.println(host);14    System.out.println(port);15 }

 

posted @
2017-05-15 15:50 阅读(
...) 评论(
...)

转载地址:http://dhcab.baihongyu.com/

你可能感兴趣的文章
Windows资源管理器相关信息获取
查看>>
windows资源管理器及ie监听
查看>>
No module named 'Crypto'
查看>>
常用openstack的镜像下载及密码
查看>>
详解python中的浅拷贝和深拷贝
查看>>
详解python中闭包和装饰器
查看>>
修改openstack云主机的IP地址
查看>>
ubuntu系统的定制裁剪(适用于嵌入式瘦客户端)
查看>>
嵌入式之系统移植详解(linux)
查看>>
openstack之 glance_image和instances存储目录解析
查看>>
centos7(三节点)搭建ceph环境
查看>>
将linux(ubuntu)安装到U盘下面--便携式ubuntu和使用dd制作U盘安装工具
查看>>
linux之强大的find命令
查看>>
python使用变量操作mysql语句
查看>>
linux bridge 网桥详解
查看>>
ceph&openstack发展前景
查看>>
Mysql之主键、外键和各种索引
查看>>
ceph&云计算
查看>>
python main()函数 name == ‘main’:
查看>>
flask一个基本的http响应流程
查看>>