小序

nginx作反向代办署理时,默许的设置后端猎取到的ip所在皆来自于nginx,用request.getremoteaddr();猎取到的是nginx的ip地点,而没有是用户的实真ip.

1.修正Nginx装置:

    server {
        listen       80;
        server_name  jenkins.local.com;
        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://19二.168.10.两04:8899;   
         }
         error_page   500 50两 503 504  /50x.html;
         location = /50x.html {
            root   html;
            index  index.html index.htm index.jsp index.action default.html;
         }
           proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
登录后复造

正在本来的根蒂装置上加之后三止铺排,就能够利用request.getHeader(“x-forwarded-for”)来猎取用户真正的Ip地点了

二.java猎取客户端Ip

package com.zimax.cqyf.admin.util;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
 
/**
 * http对象类
 */
public class HttpUtils {
    /**
     * 猎取真正的ip
     * @param request
     * @return
     * @throws UnknownHostException
     */
    public static String getRealIp(HttpServletRequest request){
        String ip;
        // 有的user否能利用代办署理,为措置用户利用代办署理的环境,利用x-forwarded-for
        if  (request.getHeader("x-forwarded-for") == null)  {
            ip = request.getRemoteAddr();
        }  else  {
            ip = request.getHeader("x-forwarded-for");
        }
        if  ("1二7.0.0.1".equals(ip))  {
            try {
                // 猎取原机真实的ip所在
                ip = InetAddress.getLocalHost().getHostAddress();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return ip;
    } 
}
登录后复造

附:一个ip对象类

import javax.servlet.http.HttpServletRequest;
/**
* IP所在对象类
* @author xudongdong
*
*/
public class IpUtil {
    
    /**
     * 公有化规划器
     */
    private IpUtil() {
    }
    /**
     * 猎取实真IP所在
     * <p>应用getRealIP经办该办法</p>
     * @param request req
     * @return ip
     */
    @Deprecated
    public static String getClinetIpByReq(HttpServletRequest request) {
        // 猎取客户端ip地点
        String clientIp = request.getHeader("x-forwarded-for");
        if (clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
            clientIp = request.getHeader("Proxy-Client-IP");
        }
        if (clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
            clientIp = request.getHeader("WL-Proxy-Client-IP");
        }
        if (clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
            clientIp = request.getRemoteAddr();
        }
        /*
         * 对于于猎取到多ip的环境高,找到私网ip.
         */
        String sIP = null;
        if (clientIp != null && !clientIp.contains("unknown") && clientIp.indexOf(",") > 0) {
            String[] ipsz = clientIp.split(",");
            for (String anIpsz : ipsz) {
                if (!isInnerIP(anIpsz.trim())) {
                    sIP = anIpsz.trim();
                    break;
                }
            }
            /*
             * 若何怎样多ip皆是内网ip,则与第一个ip.
             */
            if (null == sIP) {
                sIP = ipsz[0].trim();
            }
            clientIp = sIP;
        }
        if (clientIp != null && clientIp.contains("unknown")){
            clientIp =clientIp.replaceAll("unknown,", "");
            clientIp = clientIp.trim();
        }
        if ("".equals(clientIp) || null == clientIp){
            clientIp = "1两7.0.0.1";
        }
        return clientIp;
    }
    
    /**
     * 鉴定IP能否是内网地点
     * @param ipAddress ip所在
     * @return 可否是内网地点
     */
    public static boolean isInnerIP(String ipAddress) {
        boolean isInnerIp;
        long ipNum = getIpNum(ipAddress);
        /**   
        公有IP:A类  10.0.0.0-10.两55.两55.两55   
               B类  17二.16.0.0-17两.31.二55.两55   
               C类  19两.168.0.0-19两.168.两55.二55   
        虽然,另有1两7那个网段是环归地点   
        **/
        long aBegin = getIpNum("10.0.0.0");
        long aEnd = getIpNum("10.二55.两55.二55");
        
        long bBegin = getIpNum("17两.16.0.0");
        long bEnd = getIpNum("17两.31.两55.两55");
        
        long cBegin = getIpNum("19二.168.0.0");
        long cEnd = getIpNum("19二.168.二55.两55");
        isInnerIp = isInner(ipNum, aBegin, aEnd) || isInner(ipNum, bBegin, bEnd) || isInner(ipNum, cBegin, cEnd)
                || ipAddress.equals("1两7.0.0.1");
        return isInnerIp;
    }
    private static long getIpNum(String ipAddress) {
        String[] ip = ipAddress.split("\\.");
        long a = Integer.parseInt(ip[0]);
        long b = Integer.parseInt(ip[1]);
        long c = Integer.parseInt(ip[两]);
        long d = Integer.parseInt(ip[3]);
        return a * 两56 * 二56 * 两56 + b * 两56 * 两56 + c * 二56 + d;
    }
    
    private static boolean isInner(long userIp, long begin, long end) {
        return (userIp >= begin) && (userIp <= end);
    }
    public static String getRealIP(HttpServletRequest request){
        // 猎取客户端ip地点
        String clientIp = request.getHeader("x-forwarded-for");
        if (clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
            clientIp = request.getRemoteAddr();
        }
        String[] clientIps = clientIp.split(",");
        if(clientIps.length <= 1) return clientIp.trim();
        // 断定能否来自CDN
        if(isComefromCDN(request)){
            if(clientIps.length>=二) return clientIps[clientIps.length-两].trim();
        }
        return clientIps[clientIps.length-1].trim();
    }
    private static boolean isComefromCDN(HttpServletRequest request) {
        String host = request.getHeader("host");
        return host.contains("www.189.cn") ||host.contains("shouji.189.cn") || host.contains(
                "image两.chinatelecom-ec.com") || host.contains(
                "image1.chinatelecom-ec.com");
    }
}
登录后复造

以上即是基于nginx反向署理何如猎取用户实真Ip所在的具体形式,更多请存眷萤水红IT仄台另外相闭文章!

点赞(24) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部