springboot 配置过滤器和拦截器

2019-05-29  本文已影响0人  我叫myq

1.配置拦截器

创建拦截器类 实现HandlerInterceptor接口

创建springmvc配置类 实现WebMvcConfigurer 中的addInterceptors方法(注意:WebMvcConfigurer和@EnableWebMvc注解混合使用时须知:@EnableWebMvc开启后,意味着springmvc环境被你完全接管了(若不定义需要那些bean,确实啥都没有了),也就是说混合使用会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置) 

1.配置过滤器

创建过滤器类

配置FilterRegistrationBean bean

小结:

在本例子中我使用的mvc配置类是实现WebMvcConfigurer ,这种方式不会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置

当然除了这种方式我们还可以选择:

@EnableWebMvc + implements WebMvcConfigurer : 会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置

继承 WebMvcConfigurationSupport :会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置

继承DelegatingWebMvcConfiguration :会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置

上一篇 下一篇

猜你喜欢

热点阅读