六合彩最高极限

SpringBoot2.0 基础案例(05):多个拦截器配置和使用场景

本文源码
GitHub:知了一笑

https://github.com/cicadasmile/spring-boot-base

一、拦截器简介

1、拦截器定义

拦截器,请求的接口被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。 拦截器主要用来按照指定规则拒绝请求。

2、拦截器中应用

Token令牌验证
请求数据校验
用户权限校验
放行指定接口

二、SpringBoot2.0拦截器用法

1、编写两个拦截器

自定义类实现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) {
 // 拦截所有路径
 // 注册自定义两个拦截器
 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、访问测试接口

日志输出内容如下

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

拦截器的拦截顺序,?#21069;?#29031;Web配置文件中注入拦截器的顺序执行的。

三、源代码地址

GitHub地址:知了一笑

https://github.com/cicadasmile/spring-boot-base

码地址:知了一笑

https://gitee.com/cicadasmile/spring-boot-base

SpringBoot2.0 基础案例(05):多个拦截器配置和使用场景

SpringBoot2.0 基础案例(05):多个拦截器配置和使用场景

原文 

https://segmentfault.com/a/1190000019725139

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版?#25945;?#20986;异议,本站将立即删除。如果您对文章转载有任?#25105;?#38382;请告之我们,以便我们及?#26412;?#27491;。

PS?#21644;?#33616;一个微信公众号: askHarries 或者qq?#28023;?74807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高?#38405;堋?#20998;布式、微服务架构的原理,JVM?#38405;?#20248;化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog? » SpringBoot2.0 基础案例(05):多个拦截器配置和使用场景

赞 (0)
分享到:更多 ()

评论 0

  • 邮箱 (必填)
  • 网址
六合彩最高极限