在2.0以后的版本中WebMvcConfigurerAdapter这个类已经被废弃。采用全新的接口WebMvcConfigurer
如下面代码
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private AuthInterceptor authInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration interceptorRegistration = registry.addInterceptor(authInterceptor);
interceptorRegistration.excludePathPatterns("/error");
interceptorRegistration.excludePathPatterns("/assters/**");
//静态资源的方形不能按照以前的写法进行excludePathPatterns("/static/**"),因为boot的二以上版本自动配置的静态资源的映射路径中不在有static
interceptorRegistration.addPathPatterns("/**").excludePathPatterns("/error","/css/**","/img/**","/js/**");
}
}
拦截器代码如下:
@Component
public class AuthInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//拦截代码的编写
return true;
}
}
发现一篇比较详细的博客:点我