根据HttpServletRequest获取ip地址

public class IPUtil {

    private static final Logger logger = LoggerFactory.getLogger(IPUtil.class);

    public static String getRealIP(HttpServletRequest request){
        String ipAddress;
        try {
            ipAddress = request.getHeader("X-Real-IP");
            if (check(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (check(ipAddress)){
                ipAddress = request.getHeader("X-forwarded-for");
            }
            if (check(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (check(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (check(ipAddress)) {
                    // 根据网卡取本机配置的IP
                    try {
                        ipAddress = InetAddress.getLocalHost().getHostAddress();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                }
            }
            // 通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null) {
                if (ipAddress.contains(",")) {
                    return ipAddress.split(",")[0];
                } else {
                    return ipAddress;
                }
            } else {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getRealAddress(HttpServletRequest request){
        String ip = getRealIP(request);
        if (ip == null || ip.isEmpty() || "0:0:0:0:0:0:0:1".equals(ip) || "127.0.0.1".equals(ip)){
            return null;
        }
        String address = null;
        try {
            address = getRealAddress(ip);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return address;
    }

    public static String getRealAddress(String ip) throws Exception{
        if (ip == null || ip.equals(""))
            return "";
        String forObject = new RestTemplate().getForObject("https://qifu-api.baidubce.com/ip/geo/v1/district?ip=" + ip, String.class);
        JSONObject json = JSON.parseObject(forObject);
        if (json.get("code").equals("Success")) {
            StringBuilder builder = new StringBuilder();
            JSONObject data = JSON.parseObject(json.get("data").toString());
            Object district = data.get("district");
            builder.append(data.get("continent"))
                    .append("-").
                    append(data.get("country"))
                    .append("-").
                    append(data.get("prov"))
                    .append("-").
                    append(data.get("city"))
                    .append(district == null ? "" : "-"+district);
            return builder.toString();
        }
        return "2";
    }

    public static NotPassGatewayRequestException illegal(HttpServletRequest request){
        String realIP = getRealIP(request);
        logger.info("realIP:{}",realIP);
        if ("0:0:0:0:0:0:0:1".equals(realIP)){
            return new NotPassGatewayRequestException(realIP);
        }
        String realAddress = "";
        try {
            realAddress = getRealAddress(realIP);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new NotPassGatewayRequestException(realIP + ":" + realAddress);
    }

    private static boolean check(String ip){
        return ip == null || ip.isEmpty() || "unknown".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip) || "127.0.0.1".equals(ip);
    }

}

getRealAddress方法获取该ip所在的地理位置,使用百度的免费接口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值