zoukankan      html  css  js  c++  java
  • request获取ip

    public static String getIp(HttpServletRequest request) {
            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("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();
            }
            if (ip == null) {
                ip = "";
            }
            if ("127.0.0.1".equals(ip) || "localhost".equalsIgnoreCase(ip) || "0:0:0:0:0:0:0:1".equals(ip)) {
                try {
                    InetAddress addr = InetAddress.getLocalHost();
                    ip = addr.getHostAddress();
                } catch (Exception e) {
                    return "";
                }
            }
    
            // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
            if (ip != null && ip.length() > 15) { // "***.***.***.***".length() = 15
                if (ip.indexOf(",") > 0) {
                    ip = ip.substring(0, ip.indexOf(","));
                }
            }
            return ip;
        }
  • 相关阅读:
    05-浮动/css
    04-选择器/css
    03-样式表/css
    02-html标签&表格&表单
    01-html基础&标签
    vue分页组件重置到首页问题
    VUE通过索引值获取数据不渲染的问题
    常见IE8兼容性问题及解决
    Ajax
    sea.js模块化工具
  • 原文地址:https://www.cnblogs.com/ciscoo/p/6286016.html
Copyright © 2011-2022 走看看