首页>代码>spring boot集成redis实现接口限流简单实例>/springboot-accesslimit/src/main/java/com/simon/springbootaccesslimit/config/ip/IpUtil.java
package com.simon.springbootaccesslimit.config.ip;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author simon
 */
public class IpUtil {

    public static final String IP_KNOWN = "unknown";
    public static String getIpAddress(HttpServletRequest request) {

        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || IP_KNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || IP_KNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || IP_KNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || IP_KNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || IP_KNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        // 获取到多个ip时取第一个作为客户端真实ip
        if (StringUtils.isNotEmpty(ip) && ip.contains(",")) {
            String[] ipArray = ip.split(",");
            if (ArrayUtils.isNotEmpty(ipArray)) {
                ip = ipArray[0];
            }
        }
        return ip;
    }
}
最近下载更多
ewan007  LV30 2025年7月3日
Yang哈哈  LV6 2023年8月9日
zw5097  LV23 2023年7月15日
最代码官方  LV168 2023年6月18日
最近浏览更多
ewan007  LV30 2025年7月3日
welcome丶  LV9 2025年5月13日
微信网友_6602457430806528  LV6 2023年10月11日
君知否  LV17 2023年9月28日
漫步的海星  LV4 2023年9月26日
微信网友_6658091507814400  LV3 2023年9月20日
eternity2002  LV2 2023年9月5日
crazy11crazy  LV30 2023年8月16日
werh0123  LV1 2023年8月13日
neuwxr2015  LV8 2023年8月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友