一、前言

在当今互联网应用的开发中,IP 白名单功能是保障系统安全性和稳定性的重要手段之一。本文将详细介绍如何使用 Spring Boot 框架结合 ip2region 库来打造高效的 IP 白名单功能,并通过详细的代码示例和配置文件进行深入讲解。

二、ip2region框架介绍
  1. 高性能:能够快速地将 IP 地址转换为对应的地理区域信息,即使在处理大量 IP 数据时也能保持出色的性能。
  2. 数据精准:其数据来源准确可靠,能够提供精确的 IP 地址定位结果,有助于实现精准的区域控制和访问策略。
  3. 轻量级:框架本身占用资源少,易于集成到各种应用中,不会给系统带来过多的负担。
  4. 易于使用:提供了简单直观的接口和方法,方便开发者进行调用和集成。
三、项目集成

1.引入依赖

<dependency>
	<groupId>org.lionsoul</groupId>
	<artifactId>ip2region</artifactId>
	<version>2.6.6</version>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

2.添加配置

#ip白名单相关配置
ip-white-list.enable=true
ip-white-list.ips=127.0.0.1,192.168.1.28
  • 1.
  • 2.
  • 3.

3.工具类

package com.example.dataproject.utils;

import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.util.StringUtils;

import javax.persistence.Cache;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author qx
 * @date 2024/8/12
 * @des
 */
public class Ip2RegionUtil {

    private static final ConcurrentHashMap<String, Searcher> CACHE = new ConcurrentHashMap<>();

    /**
     * 获取IP所在地区
     *
     * @param ip IP地址
     * @return
     */
    public static String getRegion(String ip) {
        if (!StringUtils.hasText(ip)) {
            return "ip不存在";
        }
        //xdb文件路径
        String dbPath = "D:" + File.separator + "ip2region" + File.separator + "ip2region.xdb";
        Searcher searcher;
        try {
            if (CACHE.containsKey(dbPath)) {
                searcher = CACHE.get(dbPath);
            } else {
                searcher = Searcher.newWithFileOnly(dbPath);
                CACHE.put(dbPath, searcher);
            }
            return searcher.search(ip);
        } catch (Exception e) {
            e.printStackTrace();
            return "获取失败";
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.

大家可以从以下地址获取 ip2region 的 xdb 文件。

 https://gitee.com/lionsoul/ip2region/blob/master/data/ip2region.xdb

4.服务类

package com.example.dataproject.service;

import com.example.dataproject.utils.Ip2RegionUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author qx
 * @date 2024/8/12
 * @des
 */
@Service
public class IpWhiteListService {

    @Value("${ip-white-list.ips}")
    private List<String> whiteIpList;

    public boolean isWhiteIp(String ip) {
        String region = Ip2RegionUtil.getRegion(ip);
        //根据区域和ip地址判断ip白名单
        return whiteIpList.contains(ip) || region.contains("广西");
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.

5.控制层

package com.example.dataproject.controller;

import com.example.dataproject.service.IpWhiteListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author qx
 * @date 2024/8/12
 * @des
 */
@RestController
public class IpWhiteController {

    @Autowired
    private IpWhiteListService whiteListService;

    @GetMapping("/checkIp")
    public String checkIp(String ip) {
        return whiteListService.isWhiteIp(ip) ? ip + "是白名单" : ip + "是黑名单";
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

6.启动程序进行测试

先测试配置里面的白名单ip地址

SpringBoot集成Ip2region实现IP白名单_SpringBoot

再测试属于广西区域的ip地址

SpringBoot集成Ip2region实现IP白名单_ip2region_02

最后输入一个其他地区的ip地址

SpringBoot集成Ip2region实现IP白名单_IP_03

这样我们就使用ip2region实现了判断ip白名单的功能。