SpringBoot2.0 基礎案例(05):多個攔截器配置和使用場景

本文源碼
GitHub:知了一笑
https://github.com/cicadasmile/spring-boot-base

1、攔截器簡介

一、攔截器定義

攔截器,請求的接口被訪問以前,進行攔截而後在以前或以後加入某些操做。攔截是AOP的一種實現策略。 攔截器主要用來按照指定規則拒絕請求。java

二、攔截器中應用

Token令牌驗證
請求數據校驗
用戶權限校驗
放行指定接口

2、SpringBoot2.0攔截器用法

一、編寫兩個攔截器

自定義類實現HandlerInterceptor接口
1)OneInterceptor 攔截器git

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("一、url=="+url);
 // 放開攔截
 return true;
 }
 @Override
 public void postHandle(HttpServletRequest httpServletRequest,
 HttpServletResponse httpServletResponse,
 Object o, ModelAndView modelAndView) throws Exception {
 LOGGER.info("一、postHandle");
 }
 @Override
 public void afterCompletion(HttpServletRequest httpServletRequest,
 HttpServletResponse httpServletResponse,
 Object o, Exception e) throws Exception {
 LOGGER.info("一、afterCompletion");
 }
}

2)TwoInterceptor 攔截器github

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("二、url=="+url);
 // 放開攔截
 return true;
 }
 @Override
 public void postHandle(HttpServletRequest httpServletRequest,
 HttpServletResponse httpServletResponse,
 Object o, ModelAndView modelAndView) throws Exception {
 LOGGER.info("二、postHandle");
 }
 @Override
 public void afterCompletion(HttpServletRequest httpServletRequest,
 HttpServletResponse httpServletResponse,
 Object o, Exception e) throws Exception {
 LOGGER.info("二、afterCompletion");
 }
}

二、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) {
 // 攔截全部路徑
 // 註冊自定義兩個攔截器
 registry.addInterceptor(new OneInterceptor()).addPathPatterns("/**");
 registry.addInterceptor(new TwoInterceptor()).addPathPatterns("/**");
 }
}

三、編寫測試接口

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" ;
 }
}

四、訪問測試接口

日誌輸出內容以下web

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

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

3、源代碼地址

GitHub地址:知了一笑
https://github.com/cicadasmile/spring-boot-base
碼雲地址:知了一笑
https://gitee.com/cicadasmile/spring-boot-base


相關文章
相關標籤/搜索