当前位置: 代码网 > it编程>编程语言>Java > java如何根据HttpServletRequest获取IP地址

java如何根据HttpServletRequest获取IP地址

2025年03月08日 Java 我要评论
java根据httpservletrequest获取ip地址解决办法:以下整理了各个代理服务器自己开发的转发服务请求头,这些请求头都不是标准的http请求头,不一定所有的代理都会带上这些请求头,所以通

java根据httpservletrequest获取ip地址

解决办法:

以下整理了各个代理服务器自己开发的转发服务请求头,这些请求头都不是标准的http请求头,不一定所有的代理都会带上这些请求头,所以通过这方式只能尽可能的获取到真实ip,但不能保证一定可以获取到真实ip,而且代理服务器请求头中获取的ip是可伪造的。

参数:

  • x-forwarded-for:squid 服务代理
  • proxy-client-ip:apache 服务代理
  • wl-proxy-client-ip:weblogic 服务代理
  • http_client_ip:有些代理服务器
  • x-real-ip:nginx服务代理

方法一

public static string getipaddress(httpservletrequest request) {
    string ip = null;    //x-forwarded-for:squid 服务代理
    string ipaddresses = request.getheader("x-forwarded-for");
    if (ipaddresses == null || ipaddresses.length() == 0 || "unknown".equalsignorecase(ipaddresses)) {        //proxy-client-ip:apache 服务代理
        ipaddresses = request.getheader("proxy-client-ip");
    }
    if (ipaddresses == null || ipaddresses.length() == 0 || "unknown".equalsignorecase(ipaddresses)) {        //wl-proxy-client-ip:weblogic 服务代理
        ipaddresses = request.getheader("wl-proxy-client-ip");
    }
    if (ipaddresses == null || ipaddresses.length() == 0 || "unknown".equalsignorecase(ipaddresses)) {        //http_client_ip:有些代理服务器
        ipaddresses = request.getheader("http_client_ip");
    }
    if (ipaddresses == null || ipaddresses.length() == 0 || "unknown".equalsignorecase(ipaddresses)) {        //x-real-ip:nginx服务代理
        ipaddresses = request.getheader("x-real-ip");
    }    //有些网络通过多层代理,那么获取到的ip就会有多个,一般都是通过逗号(,)分割开来,并且第一个ip为客户端的真实ip
    if (ipaddresses != null && ipaddresses.length() != 0) {
        ip = ipaddresses.split(",")[0];
    }    //还是不能获取到,最后再通过request.getremoteaddr();获取
    if (ip == null || ip.length() == 0 || "unknown".equalsignorecase(ipaddresses)) {
        ip = request.getremoteaddr();
    }
    return ip;
}

方法二

public static string getipaddr(httpservletrequest request)
    {
        if (request == null)
        {
            return "unknown";
        }
        string ip = request.getheader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsignorecase(ip))
        {
            ip = request.getheader("proxy-client-ip");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsignorecase(ip))
        {
            ip = request.getheader("x-forwarded-for");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsignorecase(ip))
        {
            ip = request.getheader("wl-proxy-client-ip");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsignorecase(ip))
        {
            ip = request.getheader("x-real-ip");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsignorecase(ip))
        {
            ip = request.getremoteaddr();
        }
        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : escapeutil.clean(ip);
    }

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com