SpringBoot3利用AOP實(shí)現(xiàn)IP黑名單功能
本文主要介紹如何使用AOP實(shí)現(xiàn)IP黑名單功能
主要涉及三個(gè)類
- 注解類
- 切面實(shí)現(xiàn)類
- Controller類
注解類
在注解中包含了幾個(gè)檢測(cè)參數(shù)
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface IPBlackList {
int maxRequests() default 10; // 最大請(qǐng)求次數(shù)
long timeWindow() default 60000L; // 計(jì)數(shù)時(shí)間窗口,單位:毫秒
long blockTime() default 60000L; // 拉黑時(shí)間,單位:毫秒
}
切面實(shí)現(xiàn)
在doBefore方法中我調(diào)了自己的工具類不過就是一個(gè)獲取請(qǐng)求ip的方法,還有我過濾了內(nèi)網(wǎng)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;
} // 內(nèi)網(wǎng)不查詢
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 已被拉黑,請(qǐng)稍后再試");
} else {
blackList.remove(clientIP); // 移除過期的黑名單記錄
blackList.remove(clientIP); // 重置計(jì)時(shí)
}
}
// 獲取該 IP 的訪問記錄并清除超過時(shí)間窗口的記錄
List<Long> times = requestTimes.getOrDefault(clientIP, new CopyOnWriteArrayList<>());
times.removeIf(time -> currentTime - time > timeWindow);
times.add(currentTime); // 記錄當(dāng)前訪問時(shí)間
requestTimes.put(clientIP, times);
// 檢查在時(shí)間窗口內(nèi)的請(qǐng)求次數(shù)
if (times.size() > maxRequests) {
blackList.put(clientIP, currentTime); // 拉黑 IP
throw new RuntimeException("請(qǐng)求次數(shù)過多,IP 已被拉黑");
}
}
}
Controller
只要在Http請(qǐng)求方法上加上上面定義的注解就可以
@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));
}
}
后言
到此這篇關(guān)于SpringBoot3利用AOP實(shí)現(xiàn)IP黑名單功能的文章就介紹到這了,更多相關(guān)SpringBoot3 IP黑名單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot 攔截器 (Interceptor)與切面 (AOP)示例、作用及適用場(chǎng)景分析
- AOP在SpringBoot項(xiàng)目中的使用場(chǎng)景解讀
- SpringBoot整合Jasypt使用自定義注解+AOP實(shí)現(xiàn)敏感字段加解密
- Springboot如何正確使用AOP問題
- springboot接口服務(wù),防刷、防止請(qǐng)求攻擊,AOP實(shí)現(xiàn)方式
- springbootAOP定義切點(diǎn)獲取/修改請(qǐng)求參數(shù)方式
- SpringBoot實(shí)現(xiàn)AOP切面的三種方式
- SpringBoot中使用AOP實(shí)現(xiàn)日志記錄功能
- SpringBoot AOP如何配置全局事務(wù)
- JAVA中Spring Boot的AOP切面編程是什么,如何使用?(實(shí)例代碼)
相關(guān)文章
詳解Java如何判斷ResultSet結(jié)果集是否為空
ResultSet 表示 select 語句的查詢結(jié)果集。這篇文章主要為大家詳細(xì)介紹了Java如何判斷ResultSet結(jié)果集是否為空,感興趣的可以了解一下2023-02-02
使用Spring boot標(biāo)記一個(gè)方法過時(shí)
這篇文章主要介紹了使用Spring boot標(biāo)記一個(gè)方法過時(shí),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
一文講透為什么遍歷LinkedList要用增強(qiáng)型for循環(huán)
這篇文章主要為大家介紹了為什么遍歷LinkedList要用增強(qiáng)型for循環(huán)的透徹詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Netty分布式固定長(zhǎng)度解碼器實(shí)現(xiàn)原理剖析
這篇文章主要為大家介紹了Netty分布式固定長(zhǎng)度解碼器原理剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
Json轉(zhuǎn)化為Java對(duì)象的實(shí)例詳解
這篇文章主要介紹了Json轉(zhuǎn)化為Java對(duì)象的實(shí)例詳解的相關(guān)資料,前后端數(shù)據(jù)交互的情況經(jīng)常會(huì)遇到Json串與java 對(duì)象的相互轉(zhuǎn)換方便操作,需要的朋友可以參考下2017-08-08
java同步器AQS架構(gòu)AbstractQueuedSynchronizer原理解析下
這篇文章主要為大家介紹了java同步器AQS架構(gòu)AbstractQueuedSynchronizer原理解析下,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
spring boot使用sharding jdbc的配置方式
這篇文章主要介紹了spring boot使用sharding jdbc的配置方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12

