SpringBoot拦截器的使用

定义拦截器

拦截器:所谓拦截器,就是能够在进行某个操作之前拦截请求,如果请求符合条件就允许在往下执行。比如说,海关就是一个拦截器,他拦截进出口的货物,如果货物满足进出口条件,则放行,否则就拦截,退回处理。

定义拦截器的几种方式:

实现HandleInterceptor接口

  1. 自定义拦截器类实现HandleInterceptor接口,并使用@Component注解标注为一个组件。
public class MySelfInterceptor implements HandlerInterceptor {
	@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    	System.out.println("在业务处理器处理请求之前被调用");
    	return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		System.out.println("在业务处理器处理请求执行完成后,生成视图之前执行");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		System.out.println("在DispatcherServlet完全处理完请求后被调用");
    }
} 

根据三种情况,可以在不同的方法针对请求进行额外的处理。
在preHandle中,可以进行权限校验,安全控制。
在postHandle中,可以对返回来的ModelAndView进行处理,这个时候还未渲染视图。
在afterCompletion中,请求已经完成,页面已经渲染,数据已经返回。这个时候可以做一些资源清理,或者记录请求调用时间,做性能监控

继承HandleInterceptorAdapter类

  1. 自定义拦截器类继承HandleInterceptor接口的实现类HandleInterceptorAdapter来定义,并使用@Component注解标注为一个组件。建议使用此方式可以根据需要覆盖一些方法
@Component
public class MyInterceptor extends HandlerInterceptorAdapter {

    public SingleLoginInterceptor() {
        super();
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        super.afterCompletion(request, response, handler, ex);
    }

    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        super.afterConcurrentHandlingStarted(request, response, handler);
    }
}

可以看到HandlerInterceptorAdapter类底层是实现了HandlerInterceptor接口,多了两个方法,要比
实现HandlerInterceptor接口的方式功能强大。
这两个方法都是HandlerInterceptorAdapter类实现的org.springframework.web.servlet.AsyncHandlerInterceptor接口提供的,而AsyncHandlerInterceptor接口又继承了HandlerInterceptor接口,所以HandlerInterceptorAdapter底层是实现类HandlerInterceptor接口。

实现WebRequestInterceptor接口

  1. 自定义拦截器类实现WebRequestInterceptor接口,并使用@Component注解标注为一个组件。
@Component
public class MyInterceptor implements WebRequestInterceptor {
    
    @Override
    public void preHandle(WebRequest webRequest) throws Exception {
        
    }

    @Override
    public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception {

    }

    @Override
    public void afterCompletion(WebRequest webRequest, Exception e) throws Exception {

    }
}

两个实现接口方式的异同点 相同点 都可以实现controller层的拦截请求 不同点
1.WebRequestInterceptor的入参WebRequest是包装了HttpServletRequest 和HttpServletResponse的,通过WebRequest获取Request中的信息更简便。
2.WebRequestInterceptor的preHandle是没有返回值的,说明该方法中的逻辑并不影响后续的方法执行,所以这个接口实现就是为了获取Request中的信息,或者预设一些参数供后续流程使用。
3.HandlerInterceptor的功能更强大也更基础,可以在preHandle方法中就直接拒绝请求进入controller方法。
4.使用场景:这个在上条已经说了,如果想更方便获取HttpServletRequest的信息就使用WebRequestInterceptor,当然这些HandlerInterceptor都能做,只不过要多写点代码

实现RequestInterceptor接口

  1. 自定义类实现RequestInterceptor接口,此方式为微服务Feign调用的自定义拦截器,实现各个微服务之间的参数传递。
@Configuration
public class CenterinsRequestInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {

    }
}

小插曲 @Configuration和@Component区别

这里使用了@Configuration注解而不是@Component,其实@Configuration本质上也是一个@Component,只不过前者描述的类中所有用@Bean标记的方法都会由CGLIB动态代理执行,在首次调用的时候会执行,然后把执行结果放到Spring上下文,之后对该方法的调用都是从Spring上下文上取的结果,所以都是指同一个实例。而使用了@Component,所有用@Bean标记的方法都是纯Java调用,每次都是生成不同的实例对象。如果要让使用了@Compnent注解的类中其@Bean标记的方法生成都是同一个实例,只需要使用@AutoWired标记属性,自动注入即可。

@Configuration
public class MyBeanConfig {
 
    @Bean
    public Country country(){
        return new Country();
    }
 
    @Bean
    public UserInfo userInfo(){
        return new UserInfo(country());
    }
 
}

 // 以上代码 等同于以下代码 
 // 以下代码去掉Autowired注入,则country方法是不同的实例对象。
 
@Component
public class MyBeanConfig {
 
    @Autowired
    private Country country;
 
    @Bean
    public Country country(){
        return new Country();
    }
 
    @Bean
    public UserInfo userInfo(){
        return new UserInfo(country);
    }
 
}

注册拦截器

注册拦截器在springboot中直接使用注解便可实现。

继承WebMvcConfigurerAdapter类

 1.创建一个自定义类,继承WebMvcConfigurerAdapter类重写addInterceptors方法。

@Configuration
public class WebCofiguration extends WebMvcConfigurerAdapter {
    public void addInterceptors(InterceptorRegistry registry) {
    		// 将自己定义的拦截器注入进来进行拦截操作
        registry.addInterceptor(new MySelfInterceptor ())
                .addPathPatterns("/**")
                .excludePathPatterns("/logout");
                //过滤器可以添加多个,这里的addPathPatterns的/**是对所有的请求都做拦截。
                //excludePathPatterns代表排除url的拦截路径,即不拦截
    }
}

此类在SpringBoot2.0以后已经废除,但仍可使用。推荐使用以下两种方式来代替此方式。

继承WebMvcConfigurationSupport类

 2.创建一个自定义类继承WebMvcConfigurationSupport类,实现addInterceptors。

@Configuration
public class MyInterceptorConfig extends WebMvcConfigurationSupport {

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }
}

此方式会导致默认的静态资源被拦截,这就需要我们手动将静态资源放开。
除了重写方法外还需要重写addResourceHandlers方法来释放静态资源

@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    super.addResourceHandlers(registry);
}

实现WebMvcConfigurer接口

 3.创建一个自定义类实现WebMvcConfigurer接口,重写addInterceptors方法。

@Configuration
public class MyInterceptorConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 实现WebMvcConfigurer不会导致静态资源被拦截
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
    }
}

此方式会拦截静态资源

拦截器执行流程

单个拦截器

对单个拦截器执行流程解释如下:
程序首先会执行拦截器类中的preHandle()方法,如果该方法返回true,则程序会继续向下执行处理器中的方法,否则程序将不再往下继续执行。在业务处理器(即控制器Controller类)处理完请求后,会执行postHandle()方法,然后会通过DispatcherServlet向客户端返回响应,在DispatcherServlet处理完请求后,才会执行afterCompletion()方法。
因此单个拦截器的执行流程:
prehandle()——Handle(也就是控制器里的方法)——postHandle()——afterCompletion()。

多个拦截器

在大型的企业级项目中,通常会配置多个拦截器来实现不同的功能。例如我们自定义了三个拦截器A、B、C。并将它们都注册到同一个拦截器配置类中,如下图

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AInterceptor())
                .addPathPatterns("/**");

        registry.addInterceptor(new BInterceptor())
                .addPathPatterns("/**");

        registry.addInterceptor(new CInterceptor())
                .addPathPatterns("/**");
    }
}

它们的preHandle()方法会按照配置文件中拦截器的配置顺序执行,而它们的postHandle()和afterCompletion()方法则会按照配置顺序的反序执行。
因此当前我们的三个拦截器执行顺序如下:
preHandleA——preHandleB——preHandleC——Handle——postHandleC——postHandleB——postHandleA——afterCompletionC——afterCompletionB——afterCompletionA

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Spring Boot 中的拦截器可以通过实现 `HandlerInterceptor` 接口来创建,并通过 `WebMvcConfigurer` 配置进行注册。 下面是一个简单的示例: 1. 创建一个实现 `HandlerInterceptor` 接口的拦截器类: ```java public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 在请求处理之前进行拦截,可以进行权限校验等操作 return true; // 返回 true 表示继续执行后续的请求处理,返回 false 则表示拦截该请求 } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // 在请求处理之后、视图渲染之前进行拦截,可以添加一些通用的数据到模型中 } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // 在整个请求完成之后进行拦截,可以进行一些资源清理等操作 } } ``` 2. 创建一个配置类并注册拦截器: ```java @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyInterceptor()) .addPathPatterns("/**") // 添加拦截路径规则 .excludePathPatterns("/login"); // 排除不需要拦截的路径 } } ``` 在上面的示例中,使用 `addInterceptor` 方法将自定义的拦截器注册到拦截器链中,并使用 `addPathPatterns` 方法添加需要拦截的路径规则,使用 `excludePathPatterns` 方法排除不需要拦截的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

changXiaoDong.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值