本文主要介绍如何使用AOP实现IP黑名单功能
主要涉及三个类
- 注解类
- 切面实现类
- Controller类
注解类
在注解中包含了几个检测参数
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface IPBlackList {
int maxRequests() default 10; // 最大请求次数
long timeWindow() default 60000L; // 计数时间窗口,单位:毫秒
long blockTime() default 60000L; // 拉黑时间,单位:毫秒
}
切面实现
在doBefore方法中我调了自己的工具类不过就是一个获取请求ip的方法,还有我过滤了内网ip,如果不需要可以去掉。
@Aspect
@Component
public class IPBlackListAspect {
private final Map<String, List<Long>> requestTimes = new ConcurrentHashMap<>();
private final Map<String, Long> blackList = new ConcurrentHashMap<>();
@Before(value = "@annotation(ipBlackList)")
public void doBefore(IPBlackList ipBlackList) {
String clientIP = ServletUtils.getClientIP();
if (StringUtils.isBlank(clientIP)) {
return;
} // 内网不查询
clientIP = StringUtils.contains(clientIP, "0:0:0:0:0:0:0:1") ? "127.0.0.1" : HtmlUtil.cleanHtmlTag(clientIP);
if (NetUtil.isInnerIP(clientIP)) {
return;
}
int maxRequests = ipBlackList.maxRequests();
long timeWindow = ipBlackList.timeWindow();
long blockTime = ipBlackList.blockTime();
long currentTime = System.currentTimeMillis();
// 检查 IP 是否在黑名单中
if (blackList.containsKey(clientIP)) {
long blacklistedAt = blackList.get(clientIP);
if (currentTime - blacklistedAt < blockTime) {
throw new RuntimeException("IP 已被拉黑,请稍后再试");
} else {
blackList.remove(clientIP); // 移除过期的黑名单记录
blackList.remove(clientIP); // 重置计时
}
}
// 获取该 IP 的访问记录并清除超过时间窗口的记录
List<Long> times = requestTimes.getOrDefault(clientIP, new CopyOnWriteArrayList<>());
times.removeIf(time -> currentTime - time > timeWindow);
times.add(currentTime); // 记录当前访问时间
requestTimes.put(clientIP, times);
// 检查在时间窗口内的请求次数
if (times.size() > maxRequests) {
blackList.put(clientIP, currentTime); // 拉黑 IP
throw new RuntimeException("请求次数过多,IP 已被拉黑");
}
}
}
Controller
只要在Http请求方法上加上上面定义的注解就可以
@RestController()
@RequestMapping("/auth/auth")
public class YunfuAuthController {
@Resource
private IYunfuAuthService yunfuAuthService;
@PostMapping
@SaIgnore
@IPBlackList
public R<YunfuAuthVo> auth(YunfuAuthBo bo){
return R.ok(yunfuAuthService.auth(bo));
}
}
后言
只是一个简单的学习示例,如果有改进的建议可以指出
本文由博客一文多发平台 OpenWrite 发布!