SpringBoot2中多個(gè)攔截器配置和使用場景的示例分析

這篇文章主要為大家展示了“SpringBoot2中多個(gè)攔截器配置和使用場景的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“SpringBoot2中多個(gè)攔截器配置和使用場景的示例分析”這篇文章吧。

成都創(chuàng)新互聯(lián)公司專注于通道網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供通道營銷型網(wǎng)站建設(shè),通道網(wǎng)站制作、通道網(wǎng)頁設(shè)計(jì)、通道網(wǎng)站官網(wǎng)定制、微信平臺小程序開發(fā)服務(wù),打造通道網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供通道網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

一、攔截器簡介

1、攔截器定義

攔截器,請求的接口被訪問之前,進(jìn)行攔截然后在之前或之后加入某些操作。攔截是AOP的一種實(shí)現(xiàn)策略。 攔截器主要用來按照指定規(guī)則拒絕請求。

2、攔截器中應(yīng)用

Token令牌驗(yàn)證
請求數(shù)據(jù)校驗(yàn)
用戶權(quán)限校驗(yàn)
放行指定接口

二、SpringBoot2.0攔截器用法

1、編寫兩個(gè)攔截器

自定義類實(shí)現(xiàn)HandlerInterceptor接口
1)OneInterceptor 攔截器

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 攔截器一
 */
public class OneInterceptor implements HandlerInterceptor {
 private static final Logger LOGGER
 = LoggerFactory.getLogger(OneInterceptor.class.getName());
 @Override
 public boolean preHandle(HttpServletRequest request,
 HttpServletResponse response,
 Object o) throws Exception {
 String url =String.valueOf(request.getRequestURL()) ;
 LOGGER.info("1、url=="+url);
 // 放開攔截
 return true;
 }
 @Override
 public void postHandle(HttpServletRequest httpServletRequest,
 HttpServletResponse httpServletResponse,
 Object o, ModelAndView modelAndView) throws Exception {
 LOGGER.info("1、postHandle");
 }
 @Override
 public void afterCompletion(HttpServletRequest httpServletRequest,
 HttpServletResponse httpServletResponse,
 Object o, Exception e) throws Exception {
 LOGGER.info("1、afterCompletion");
 }
}

2)TwoInterceptor 攔截器

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 攔截器二
 */
public class TwoInterceptor implements HandlerInterceptor {
 private static final Logger LOGGER
 = LoggerFactory.getLogger(TwoInterceptor.class.getName());
 @Override
 public boolean preHandle(HttpServletRequest request,
 HttpServletResponse response,
 Object o) throws Exception {
 String url =String.valueOf(request.getRequestURL()) ;
 LOGGER.info("2、url=="+url);
 // 放開攔截
 return true;
 }
 @Override
 public void postHandle(HttpServletRequest httpServletRequest,
 HttpServletResponse httpServletResponse,
 Object o, ModelAndView modelAndView) throws Exception {
 LOGGER.info("2、postHandle");
 }
 @Override
 public void afterCompletion(HttpServletRequest httpServletRequest,
 HttpServletResponse httpServletResponse,
 Object o, Exception e) throws Exception {
 LOGGER.info("2、afterCompletion");
 }
}

2、Web配置文件中注入攔截器

import com.boot.intercept.intercept.OneInterceptor;
import com.boot.intercept.intercept.TwoInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
 * Web配置文件
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
 public void addInterceptors(InterceptorRegistry registry) {
 // 攔截所有路徑
 // 注冊自定義兩個(gè)攔截器
 registry.addInterceptor(new OneInterceptor()).addPathPatterns("/**");
 registry.addInterceptor(new TwoInterceptor()).addPathPatterns("/**");
 }
}

3、編寫測試接口

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class InterceptController {
 @RequestMapping("/reqUrl")
 public String reqUrl (){
 return "success" ;
 }
}

4、訪問測試接口

日志輸出內(nèi)容如下

intercept.OneInterceptor : 1、url==http://127.0.0.1:8005/reqUrl
intercept.TwoInterceptor : 2、url==http://127.0.0.1:8005/reqUrl
intercept.TwoInterceptor : 2、postHandle
intercept.OneInterceptor : 1、postHandle
intercept.TwoInterceptor : 2、afterCompletion
intercept.OneInterceptor : 1、afterCompletionla

攔截器的攔截順序,是按照Web配置文件中注入攔截器的順序執(zhí)行的。

以上是“SpringBoot2中多個(gè)攔截器配置和使用場景的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

當(dāng)前名稱:SpringBoot2中多個(gè)攔截器配置和使用場景的示例分析
網(wǎng)址分享:http://muchs.cn/article2/ihggoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航移動網(wǎng)站建設(shè)、小程序開發(fā)Google、企業(yè)建站網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)