SpringBoot3利用AOP實現(xiàn)IP黑名單功能
本文主要介紹如何使用AOP實現(xiàn)IP黑名單功能
主要涉及三個類
- 注解類
- 切面實現(xiàn)類
- Controller類
注解類
在注解中包含了幾個檢測參數(shù)
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface IPBlackList {
int maxRequests() default 10; // 最大請求次數(shù)
long timeWindow() default 60000L; // 計數(shù)時間窗口,單位:毫秒
long blockTime() default 60000L; // 拉黑時間,單位:毫秒
}
切面實現(xiàn)
在doBefore方法中我調(diào)了自己的工具類不過就是一個獲取請求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 已被拉黑,請稍后再試");
} 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);
// 檢查在時間窗口內(nèi)的請求次數(shù)
if (times.size() > maxRequests) {
blackList.put(clientIP, currentTime); // 拉黑 IP
throw new RuntimeException("請求次數(shù)過多,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));
}
}
后言
到此這篇關(guān)于SpringBoot3利用AOP實現(xiàn)IP黑名單功能的文章就介紹到這了,更多相關(guān)SpringBoot3 IP黑名單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot 攔截器 (Interceptor)與切面 (AOP)示例、作用及適用場景分析
- AOP在SpringBoot項目中的使用場景解讀
- SpringBoot整合Jasypt使用自定義注解+AOP實現(xiàn)敏感字段加解密
- Springboot如何正確使用AOP問題
- springboot接口服務,防刷、防止請求攻擊,AOP實現(xiàn)方式
- springbootAOP定義切點獲取/修改請求參數(shù)方式
- SpringBoot實現(xiàn)AOP切面的三種方式
- SpringBoot中使用AOP實現(xiàn)日志記錄功能
- SpringBoot AOP如何配置全局事務
- JAVA中Spring Boot的AOP切面編程是什么,如何使用?(實例代碼)
相關(guān)文章
使用springboot整合mybatis-plus實現(xiàn)數(shù)據(jù)庫的增刪查改示例
這篇文章主要介紹了使用springboot整合mybatis-plus實現(xiàn)數(shù)據(jù)庫的增刪查改示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04
SpringBoot靜態(tài)資源CSS等修改后再運行無效的解決
這篇文章主要介紹了SpringBoot靜態(tài)資源CSS等修改后再運行無效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Java ConcurrentModificationException異常解決案例詳解
這篇文章主要介紹了Java ConcurrentModificationException異常解決案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-09-09
防止未登錄用戶操作—基于struts2攔截器的簡單實現(xiàn)
下面小編就為大家?guī)硪黄乐刮吹卿浻脩舨僮鳌趕truts2攔截器的簡單實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
IDEA連接MySQL提示serverTimezone的問題及解決方法
很多朋友私聊小編,使用IDEA軟件連接MySQL數(shù)據(jù)庫時總是提示Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property manually.的錯誤,小編就不一一回復大家了,下面小編把我的解決方法分享到腳本之家平臺,需要的朋友參考下吧2021-05-05

