zoukankan      html  css  js  c++  java
  • 获取真实Ip地址

        /**
         * @author zhoulongqin
         * @see 获取客户端ip
         * @param
         * @return 客户端ip(String) webserve ip不一定获取的到
         */
        public String getRealIp() {
            MessageContext mc = MessageContext.getCurrentContext();
            HttpServletRequest request = (HttpServletRequest) mc.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
    
            String ipAddress = null;
            // ipAddress = request.getRemoteAddr();
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1")) {
                    // 根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
    
            }
    
            // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
                                                                // = 15
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
            return ipAddress;
        }
  • 相关阅读:
    BZOJ2095 [Poi2010]Bridges
    BZOJ3307 雨天的尾巴
    【CSP2020】 T3 动物园
    【CSP2020】 T1儒略日
    洛谷P3455 [POI2007]ZAP-Queries
    【黑科技学习】光速幂
    XJTUOJ #1023 JM的祖传零钱箱
    XJTUOJ #1168 zxh的后宫管理系统
    XJYUOJ #1053 nocriz与队列计算机
    XJTUOJ #1017 JM的完美集合
  • 原文地址:https://www.cnblogs.com/ziq711/p/6196229.html
Copyright © 2011-2022 走看看