欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

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

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

一、攔截器簡(jiǎn)介

1、攔截器定義

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

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

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

二、SpringBoot2.0攔截器用法

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

自定義類(lèi)實(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);
 // 放開(kāi)攔截
 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);
 // 放開(kāi)攔截
 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) {
 // 攔截所有路徑
 // 注冊(cè)自定義兩個(gè)攔截器
 registry.addInterceptor(new OneInterceptor()).addPathPatterns("/**");
 registry.addInterceptor(new TwoInterceptor()).addPathPatterns("/**");
 }
}

3、編寫(xiě)測(cè)試接口

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、訪問(wèn)測(cè)試接口

日志輸出內(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è)攔截器配置和使用場(chǎng)景的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

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

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

廣告

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

微信小程序開(kāi)發(fā)