之前项目的springboot自定义拦截器使用的是继承WebMvcConfigurerAdapter重写常用方法的方式来实现的。
以下WebMvcConfigurerAdapter 比较常用的重写接口
/** 解决跨域问题 **/
public void addCorsMappings(CorsRegistry registry) ;
/** 添加拦截器 **/
void addInterceptors(InterceptorRegistry registry);
/** 这里配置视图解析器 **/
void configureViewResolvers(ViewResolverRegistry registry);
/** 配置内容裁决的一些选项 **/
void configureContentNegotiation(ContentNegotiationConfigurer configurer);
/** 视图跳转控制器 **/
void addViewControllers(ViewControllerRegistry registry);
/** 静态资源处理 **/
void addResourceHandlers(ResourceHandlerRegistry registry);
/** 默认静态资源处理器 **/
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);
但是在新项目中,使用的是springboot 2.0.3,在使用时提示该类已经过时(在类上加这个注解@Deprecated,就会提示这个类已经过时),这个果断不能忍啊,去网上百度了一下,说是springboot 2.0.0以上和spring 5.0以上的版本这个类会被废弃,将使用新的类(WebMvcConfigurationSupport)来代替,这个类是WebMvcConfigurerAdapter的扩展和替代。
这是我代码中的使用案例:
package com.*.ding.configs; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 自定义拦截器 * @Auther: Tt(yehuawei) * @Date: 2018/7/11 14:43 */ @Component public class MyInterceptor implements HandlerInterceptor { //实现原生拦截器的接口 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //进行逻辑判断,如果ok就返回true,不行就返回false,返回false就不会处理改请求 return true; } @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 { } }
package com.*.ding.configs; 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.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; /** * 拦截器配置 * * * @Auther: Tt(yehuawei) * @Date: 2018/7/11 15:05 */ @Configuration public class MvcConfig extends WebMvcConfigurationSupport { // 以下WebMvcConfigurerAdapter 比较常用的重写接口 // /** 解决跨域问题 **/ // public void addCorsMappings(CorsRegistry registry) ; // /** 添加拦截器 **/ // void addInterceptors(InterceptorRegistry registry); // /** 这里配置视图解析器 **/ // void configureViewResolvers(ViewResolverRegistry registry); // /** 配置内容裁决的一些选项 **/ // void configureContentNegotiation(ContentNegotiationConfigurer configurer); // /** 视图跳转控制器 **/ // void addViewControllers(ViewControllerRegistry registry); // /** 静态资源处理 **/ // void addResourceHandlers(ResourceHandlerRegistry registry); // /** 默认静态资源处理器 **/ // void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer); @Autowired private MyInterceptor myInterceptor; /** * * 功能描述: * 配置静态资源,避免静态资源请求被拦截 * @auther: Tt(yehuawei) * @date: * @param: * @return: */ public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**") .addResourceLocations("classpath:/static/"); registry.addResourceHandler("/templates/**") .addResourceLocations("classpath:/templates/"); super.addResourceHandlers(registry); } public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(myInterceptor) //addPathPatterns 用于添加拦截规则 .addPathPatterns("/**") //excludePathPatterns 用于排除拦截 //注意content-path:ding是不用填写的 //项目启动测试接口 .excludePathPatterns("/") //钉钉回调事件 .excludePathPatterns("/callback/**") //检查验证码 .excludePathPatterns("/check_auth_code") //发送验证码 .excludePathPatterns("/send_auth_code") //获取用户基本信息 .excludePathPatterns("/get_user_base_info") //获取用户详情 .excludePathPatterns("/get_user_detail_info"); super.addInterceptors(registry); } }