900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > WebMvcConfigurer配置HandlerInterceptor拦截器失效

WebMvcConfigurer配置HandlerInterceptor拦截器失效

时间:2023-03-15 19:30:49

相关推荐

WebMvcConfigurer配置HandlerInterceptor拦截器失效

1.前言

Springboot2.0之前,实现拦截器功能的配置类是通过继承(extends)WebMvcConfigurerAdapter类完成的,最近项目把Springboot升级到了Springboot2.X,WebMvcConfigurerAdapter类已经不能使用了,查了资料说现在要通过实现(implements)WebMvcConfigurer接口来完成该功能。

这时候出现问题了,实现(implements)WebMvcConfigurer接口后,启动程序,程序运行过程中根本没走到该配置类和拦截器代码。

2.配置类WebMvcConfig

import com.xq.plugin.SecurityHandlerInterceptor;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configurationpublic class WebMvcConfig implements WebMvcConfigurer {@AutowiredSecurityHandlerInterceptor securityHandlerInterceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(securityHandlerInterceptor).addPathPatterns("/**");}}

3.拦截器SecurityHandlerInterceptor

import mons.logging.Log;import mons.logging.LogFactory;import org.ponent;import org.springframework.web.method.HandlerMethod;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@Componentpublic class SecurityHandlerInterceptor implements HandlerInterceptor {private static final Log log = LogFactory.getLog(SecurityHandlerInterceptor.class);@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {HandlerMethod handlerMethod = null;if (handler instanceof HandlerMethod) {handlerMethod = (HandlerMethod) handler;}if (handlerMethod == null) {return true;}return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception {if (log.isDebugEnabled()) {log.debug(SecurityHandlerInterceptor.class.getSimpleName() + "postHandle方法");}}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {if (log.isDebugEnabled()) {log.debug(SecurityHandlerInterceptor.class.getSimpleName() + "afterCompletion方法");}}}

4.失效原因

原来SpringBoot做了这个限制,只有当WebMvcConfigurationSupport类不存在的时候才会生效WebMvc自动化配置,因为我在写swagger配置类时继承(extends)了WebMvcConfigurationSupport类,所以实现(implements)了WebMvcConfigurer接口的WebMvcConfig配置类没有生效。

原swagger配置类代码:

import mon.constants.AppConstants;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.PathSelectors;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.service.ApiInfo;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration@EnableSwagger2public class Swagger2Config extends WebMvcConfigurationSupport {//因为swagger与静态文件访问配置冲突,所以整合swagger需要@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");// springboot 集成swagger2.2后静态资源404,添加如下两行配置registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");super.addResourceHandlers(registry);}@Beanpublic Docket customDocket() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName("xq").select().apis(RequestHandlerSelectors.basePackage("com.xq.yexiong.controller")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title(AppConstants.app+"的API接口").version("1.0").build();}}

5.解决方法

解决方法很简单,就是让swagger配置类不再继承(extends)WebMvcConfigurationSupport类,也去实现(implements)WebMvcConfigurer接口即可。

新swagger配置类代码:

import mon.constants.AppConstants;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.PathSelectors;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.service.ApiInfo;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration@EnableSwagger2public class Swagger2Config implements WebMvcConfigurer {//因为swagger与静态文件访问配置冲突,所以整合swagger需要@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");// springboot 集成swagger2.2后静态资源404,添加如下两行配置registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");//super.addResourceHandlers(registry);}@Beanpublic Docket customDocket() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName("xq").select().apis(RequestHandlerSelectors.basePackage("com.xq.yexiong.controller")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title(AppConstants.app+"的API接口").version("1.0").build();}}

6.总结

springBoot2.0以上 WebMvcConfigurerAdapter 方法过时,有两种替代方案:

1、继承WebMvcConfigurationSupport

2、实现WebMvcConfigurer

但是继承(extends)WebMvcConfigurationSupport类会让Springboot对WebMvc自动化配置失效。所以项目中不能同时使用。

参考文档:/sueyyyy/p/11611676.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。