spring boot攔截器實現(xiàn)IP黑名單實例代碼
前言
最近一直在搞 Hexo+GithubPage 搭建個人博客,所以沒怎么進行 SpringBoot 的學習。所以今天就將上次的”?秒防刷新”進行了一番修改。上次是采用注解加攔截器(@Aspect)來實現(xiàn)功能的。但是,如果需求是一個全局的攔截器對于大部分URL都進行攔截的話,自己一個個加顯然是不可能的。而且上次的攔截器對于Controller的參數(shù)有所要求,在實際他人引用總是顯得不方便。所以,這次使用了繼承HandlerInterceptor來實現(xiàn)攔截器。
功能需求
對于項目中某類URL進行攔截,若用戶在短時間內(nèi)大量訪問該鏈接,則將用戶IP列入黑名單,禁止用戶訪問網(wǎng)頁。(同時,可以使用@Async來創(chuàng)建定時任務幫用戶解禁。)
知識記錄
spring 的攔截器 HandlerInterceptor 的功能跟過濾器類似,但是提供更精細的的控制能力:在request被響應之前、request被響應之后、視圖渲染之前以及request全部結束之后。我們不能通過攔截器修改request內(nèi)容,但是可以通過拋出異常(或者返回false)來暫停request的執(zhí)行。
配置攔截器也很簡單,Spring 為此提供了基礎類WebMvcConfigurerAdapter ,我們只需要重寫addInterceptors 方法添加注冊攔截器。
實現(xiàn)自定義攔截器只需要3步:
1、創(chuàng)建我們自己的攔截器類并實現(xiàn) HandlerInterceptor 接口。
2、創(chuàng)建一個 Java 類繼承 WebMvcConfigurerAdapter,并重寫 addInterceptors 方法。
3、實例化我們自定義的攔截器,然后將對像手動添加到攔截器鏈中(在addInterceptors方法中添加)。
正式開工
IP工具類
由于不清楚用戶代理,最好能使用一個工具類來來獲取用戶真實IP。這個Google就能找到,我就不貼代碼了。
數(shù)據(jù)庫
我使用的是MySQL數(shù)據(jù)庫,持久層框架為MyBatis。具體可參考”準備”步驟。
我在”myboot”數(shù)據(jù)庫中創(chuàng)建一張表”blaclist”,屬性如下:
| 字段名 | 解釋 |
|---|---|
| id | 記錄的id |
| ip | 用戶真實IP |
| iptime | IP被鎖時間 |
實體類
public class BlackList {
private int id;
private String ip;
private Date iptime; // 日期類型,格式:yyyy-MM-dd HH:mm:ss
//構造器
public BlackList() {
}
public BlackList(String ip, Date iptime) {
this.ip = ip;
this.iptime = iptime;
}
// get && set 方法
}
Dao層
注意XML配置與對應實體配置(省略)。
@Mapper
public interface BlackListDao {
// 根據(jù)IP來查找記錄
List<BlackList> findByIp(String ip);
// 添加記錄
int addBlackList(@Param("blackList") BlackList blackList);
}
實現(xiàn) HandlerInterceptor 接口
public class URLInterceptor implements HandlerInterceptor {
@Autowired
BlackListDao blackListDao;
private Map<String, Integer> redisTemplate = new HashMap<String, Integer>();
private static final Logger logger = LoggerFactory.getLogger(URLInterceptor.class);
//在請求處理之前進行調(diào)用(Controller方法調(diào)用之前)
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
return true;
}
//請求處理之后進行調(diào)用,但是在視圖被渲染之前(Controller方法調(diào)用之后)
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
String ip = IPAddressUtil.getClientIpAddress(httpServletRequest);
List<BlackList> blackLists = blackListDao.findByIp(ip);
if (blackLists == null || blackLists.size() == 0){
urlHandle(httpServletRequest, 5000, 10);
} else {
//強制控制跳轉(zhuǎn)
modelAndView.setViewName("/errorpage/error.html");
}
}
//在整個請求結束之后被調(diào)用
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
public void urlHandle(HttpServletRequest request, long limitTime,int limitCount) throws RequestLimitException {
/**
* 省略業(yè)務邏輯部分,參考"準備"步驟
*/
if (count > limitCount){ //符合鎖定條件
Calendar calendar = Calendar.getInstance();
Date iptime=calendar.getTime();
BlackList blackList = new BlackList(ip, iptime);
blackListDao.addBlackList(blackList);
throw new RequestLimitException();
}
}
}
WebMvcConfigurerAdapter類
配置 spring mvc的攔截器 WebMvcConfigurerAdapter。
@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {
@Bean //把我們的攔截器注入為bean
public HandlerInterceptor getMyInterceptor(){
return new URLInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 多個攔截器組成一個攔截器鏈
// addPathPatterns 用于添加攔截規(guī)則, 這里假設攔截 /url 后面的全部鏈接
// excludePathPatterns 用戶排除攔截
registry.addInterceptor(getMyInterceptor()).addPathPatterns("/url/**");
super.addInterceptors(registry);
}
}
Controller類
@RequestMapping("/url/test")
@ResponseBody
public String URLtest() {
return "success";
}
項目參考地址 : https://github.com/FunriLy/springboot-study/tree/master/%E6%A1%88%E4%BE%8B8
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
寶塔面板配置及部署javaweb教程(全網(wǎng)最全)
這篇文章主要介紹了寶塔面板配置及部署javaweb教程(全網(wǎng)最全),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06
SpringCloud GateWay動態(tài)路由用法
網(wǎng)關作為所有項目的入口,不希望重啟,因此動態(tài)路由是必須的,動態(tài)路由主要通過RouteDefinitionRepository接口實現(xiàn),其默認的實現(xiàn)是InMemoryRouteDefinitionRepository,即在內(nèi)存中存儲路由配置,可基于這個map對象操作,動態(tài)路由的實現(xiàn)方案有兩種2024-10-10
mybatis初始化SqlSessionFactory失敗的幾個原因分析
這篇文章主要介紹了mybatis初始化SqlSessionFactory失敗的幾個原因分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
MyBatis-Plus 如何實現(xiàn)連表查詢的示例代碼
這篇文章主要介紹了MyBatis-Plus 如何實現(xiàn)連表查詢的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08
詳解Java Bellman-Ford算法原理及實現(xiàn)
Bellman-Ford算法與Dijkstra算法類似,都是以松弛操作作為基礎,Bellman-Ford算法是對所有邊都進行松弛操作,本文將詳解Bellman-Ford算法原理及實現(xiàn),感興趣的可以了解一下2022-07-07

