SpringBoot攔截器的使用
一、攔截器簡介
攔截器通常通過動(dòng)態(tài)代理的方式來執(zhí)行。
攔截器的生命周期由IoC容器管理,可以通過注入等方式來獲取其他Bean的實(shí)例,使用更方便。
二、攔截器配置使用方式
1、過濾器攔截器作用范圍

2、攔截器的使用
示例代碼如下:
package com.rongrong.wiki.interceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 攔截器:Spring框架特有的,常用于登錄校驗(yàn),權(quán)限校驗(yàn),請(qǐng)求日志打印 /login
*/
@Component
public class LogInterceptor implements HandlerInterceptor {
private static final Logger LOG = LoggerFactory.getLogger(LogInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 打印請(qǐng)求信息
LOG.info("------------- LogInterceptor 開始 -------------");
LOG.info("請(qǐng)求地址: {} {}", request.getRequestURL().toString(), request.getMethod());
LOG.info("遠(yuǎn)程地址: {}", request.getRemoteAddr());
long startTime = System.currentTimeMillis();
request.setAttribute("requestStartTime", startTime);
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
long startTime = (Long) request.getAttribute("requestStartTime");
LOG.info("------------- LogInterceptor 結(jié)束 耗時(shí):{} ms -------------", System.currentTimeMillis() - startTime);
}
}
將攔截器加入到配置中,示例代碼如下:
package com.rongrong.wiki.config;
import com.rongrong.wiki.interceptor.LogInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.annotation.Resource;
@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {
@Resource
LogInterceptor loginInterceptor;
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor)
.addPathPatterns("/**")
.excludePathPatterns("/login");
}
}
重新編譯啟動(dòng),查看結(jié)果如下:

三、知識(shí)點(diǎn)總結(jié)
1、攔截器的使用
- 返回
true會(huì)往后執(zhí)行 - 返回
false會(huì)結(jié)束,可以利用這點(diǎn)來做權(quán)限攔截 addPathPatterns(),要攔截請(qǐng)求excludePathPatterns(),排除請(qǐng)求,不攔截
2、攔截器和過濾器的相同與不同
- 都可以用來統(tǒng)一處理請(qǐng)求,比如:打印日志、權(quán)限控制
- 過濾器依賴于
servlet容器,攔截器依賴Spring框架 - 過濾器不用注入其它類,攔截器可注入其它類,基于這一點(diǎn),建議能用攔截器的都用攔截器
到此這篇關(guān)于SpringBoot攔截器的使用的文章就介紹到這了,更多相關(guān)SpringBoot攔截器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot2.0整合jackson配置日期格式化和反序列化的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot2.0整合jackson配置日期格式化和反序列化的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11
淺析java中String類型中“==”與“equal”的區(qū)別
這篇文章主要介紹了淺析java中String類型中“==”與“equal”的區(qū)別,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
簡單了解Spring Cloud Alibaba相關(guān)知識(shí)
這篇文章主要介紹了簡單了解Spring Cloud Alibaba相關(guān)知識(shí),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
Java8中List轉(zhuǎn)Map(Collectors.toMap) 的技巧分享
在最近的工作開發(fā)之中,慢慢習(xí)慣了很多Java8中的Stream的用法,很方便而且也可以并行的去執(zhí)行這個(gè)流,這篇文章主要給大家介紹了關(guān)于Java8中List轉(zhuǎn)Map(Collectors.toMap) 的相關(guān)資料,需要的朋友可以參考下2021-07-07
SpringBoot 下在 yml 中的 logging 日志配置方法
logging 配置主要用于控制應(yīng)用程序的日志輸出行為,可以通過配置定制日志的格式、級(jí)別、輸出位置等,這篇文章主要介紹了SpringBoot 下在 yml 中的 logging 日志配置,需要的朋友可以參考下2024-06-06
Spring Boot整合tk.mybatis代碼實(shí)例
這篇文章主要介紹了Spring Boot整合tk.mybatis代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11

