IpUtils-IP工具类


import com.google.gson.Gson;
import com.shuidihuzhu.razor.client.utils.json.GsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Map;

/**
 * @author yuechao
 * @date 2021/9/16
 */
@Slf4j
public class IpUtils {

    /**
     * 获取 链接请求源地址
     * @param request  请求头
     * @return 源地址
     */
    public static String getReferer(HttpServletRequest request) throws Exception {
        if (request == null) {
            throw new Exception("getReferer method HttpServletRequest Object is null  请求对象是null");
        }
        // 获取请求是从哪里来的
        String referer = request.getHeader("referer");
        if(StringUtils.isNotBlank(referer)){
            return referer;
        }else {
            return null;
        }
    }


    /**
     * 获取真实ID地址,首先从MDC 获取,如果MDC获取失败,再从HttpServletRequest中获取
     * @param request
     * @return
     * @throws Exception
     */
    public static String getIp(HttpServletRequest request) throws Exception {

        Map<String, String> contextMap = MDC.getCopyOfContextMap();
        Gson gson = GsonUtils.getGson();
        log.debug("getIp contextMap = {}", gson.toJson(contextMap));

        //首先从MDC中获取IP ,如果获取不到再从Header中获取
        if(contextMap == null
                || StringUtils.isBlank(contextMap.get("clientIp"))){
            if (request == null) {
                throw new Exception("getIpAddr method HttpServletRequest Object is null  请求对象是null");
            }
            Enumeration<String> headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()){
                String next = headerNames.nextElement();
                log.info("getIpAddr headerNames , next = {}, value = {}",next, request.getHeader(next));
            }
            // 获取ip地址
            String ipAddress = "";
            if (StringUtils.isNotBlank(checkIp(request.getHeader("X-Real-IP")))) {
                ipAddress = request.getHeader("X-Real-IP");
            }else if (StringUtils.isNotBlank(checkIp(request.getHeader("x-real-ip")))) {
                ipAddress = request.getHeader("x-real-ip");
            } else if (StringUtils.isNotBlank(checkIp(request.getHeader("x-forwarded-for")))) {
                ipAddress = request.getHeader("x-forwarded-for");
            }else if (StringUtils.isNotBlank(checkIp(request.getHeader("Proxy-Client-IP")))) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }else if (StringUtils.isNotBlank(checkIp(request.getHeader("proxy-client-ip")))) {
                ipAddress = request.getHeader("proxy-client-ip");
            } else if (StringUtils.isNotBlank(checkIp(request.getHeader("HTTP_CLIENT_IP")))) {
                ipAddress = request.getHeader("HTTP_CLIENT_IP");
            } else if (StringUtils.isNotBlank(checkIp(request.getHeader("http_client_ip")))) {
                ipAddress = request.getHeader("http_client_ip");
            }else if (StringUtils.isNotBlank(checkIp(request.getHeader("WL-Proxy-Client-IP")))) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }else if (StringUtils.isNotBlank(checkIp(request.getHeader("wl-proxy-client-ip")))) {
                ipAddress = request.getHeader("wl-proxy-client-ip");
            }
            if(StringUtils.isBlank(ipAddress)){
                ipAddress = request.getRemoteAddr();
            }
            log.debug("getIp befor ipAddress = {}", gson.toJson(ipAddress));
            // 多个路由时,取第一个非unknown的ip
            final String[] arr = ipAddress.split(",");
            for (final String str : arr) {
                if (!"unknown".equalsIgnoreCase(str)) {
                    ipAddress = str;
                    break;
                }
            }
            log.debug("getIp after ipAddress = {}", gson.toJson(ipAddress));
            return ipAddress;
        }else{
            return contextMap.get("clientIp");
        }
    }

    /**
     * 检测IP内容
     * @param ip ip地址
     * @return 检测后的IP内容,如果为空则代表IP格式错误
     */
    private static String checkIp(String ip) {
        if (StringUtils.isNotBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
            return ip;
        }
        return null;
    }

    /**
     * 获取用户真实IP地址,不使用request.getRemoteAddr()的原因是有可能用户使用了代理软件方式避免真实IP地址,
     * 可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值
     *
     * @return ip
     */
    public static String getRealIP(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值,第一个ip才是真实ip
            if (ip.indexOf(",") != -1) {
                ip = ip.split(",")[0];
            }
        }
        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("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        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 ip;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值