解决SpringBoot2.x添加拦截器后Swagger无法访问

一、配置拦截器

(1)SpringBoot 1.x配置方式

@Configuration
public class AppConfig extends WebMvcConfigurerAdapter {
	@Resource
	private FRInterceptor fRInterceptor;
 
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		//自定义拦截器,添加拦截路径和排除拦截路径 
	registry.addInterceptor(fRInterceptor)
            .addPathPatterns("api/**")
            .excludePathPatterns("api/login");
}

(2)SpringBoot 2.x配置方式

       但是在spring boot2.x中,WebMvcConfigurerAdapter被deprecated,虽然继承WebMvcConfigurerAdapter这个类虽然有此便利,但在Spring5.0里面已经deprecated了。

     官方文档也说了,WebMvcConfigurer接口现在已经有了默认的空白方法,所以在Springboot2.0(Spring5.0)下更好的做法还是implements WebMvcConfigurer。
 

@Configuration
public class WebConfig implements WebMvcConfigurer {
	@Resource
	private FileUploadInterceptor fileUploadInterceptor;
 
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 自定义拦截器,添加拦截路径和排除拦截路径
		registry.addInterceptor(fileUploadInterceptor).addPathPatterns("/**");
	}
}

(3)自定义拦截器

@Component
public class FileUploadInterceptor implements HandlerInterceptor {
	/*
	 * 视图渲染之后的操作
	 */
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
 
	}
 
	/*
	 * 处理请求完成后视图渲染之前的处理操作
	 */
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
 
	}
 
	/*
	 * 进入controller层之前拦截请求
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
		System.out.println("getContextPath:" + request.getContextPath());
		System.out.println("getServletPath:" + request.getServletPath());
		System.out.println("getRequestURI:" + request.getRequestURI());
		System.out.println("getRequestURL:" + request.getRequestURL());
		System.out.println("getRealPath:" + request.getSession().getServletContext().getRealPath("image"));
		return true;
	}
 
}

 

二、添加拦截器后Swagger无法访问

在配置拦截器的时候,配置swagger资源路径

@Configuration
public class InteceptorConfig implements WebMvcConfigurer {
    @Autowired
    EnvironmentConfig environmentConfig;
    @Resource
    private AccessCheckInteceptor accessCheckInteceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 自定义拦截器,添加拦截路径和排除拦截路径
        String excludePathStr = environmentConfig.getConfigEnvironment();
        String[] excludePathArr = excludePathStr.split(",");
        registry.addInterceptor(accessCheckInteceptor)
                // 排除配置
                .excludePathPatterns(excludePathArr)
                // 拦截配置
                .addPathPatterns("/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
        // 放行swagger
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

 

 

 

 

 

你可能感兴趣的:(SpringBoot)