spring boot 加入拦截器后swagger不能访问(亲测有效)

时间:2025-03-21 09:35:09
package com.tensquare.user.config; import com.tensquare.user.interceptor.JwtInterceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Configuration public class InterceptorConfig extends WebMvcConfigurationSupport { @Autowired private JwtInterceptor jwtInterceptor; /*** * addPathPatterns("/**"):拦截所有请求 * excludePathPatterns: 不拦截的请求 * @param registry */ protected void addInterceptors(InterceptorRegistry registry){ //注册拦截器要声明拦截器对象和要拦截的请求 registry.addInterceptor(jwtInterceptor) .addPathPatterns("/**") .excludePathPatterns("/**/login/**") .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "//**"); } /*** * 配置静态资源访问拦截 * @param registry */ public void addResourceHandlers(ResourceHandlerRegistry registry){ registry.addResourceHandler("") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } }