配置跨域后发现swagger2不能访问问题(After configuring cross domain, it is found that swagger2 cannot access)

配置跨域后发现swagger2不能访问问题

原因分析:可能是swagger2的内置接口和静态资源拦截了

原springboot的全局跨域配置如下

/**
* 解决跨域问题
*/
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路劲
        registry.addMapping("/**")
                //设置允许跨域请求的域名
//              .allowedOrigins("*")
                .allowedOriginPatterns("*")
                //设置允许的方法
                .allowedMethods("*")
               //允许的请求头
                .allowedHeaders("*")
                //跨域允许时间
                .maxAge(3600)
                 //是否允许证书 不在默认开启
                .allowCredentials(true);
    }
}

配置跨域后发现swagger2不能继续访问,需要在上面的类中添加如下代码:

 /**
     * 跨域配置后swagger2可能不能访问,需要增加如下配置
     * @param registry
     * 对静态资源的配置
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

总配置类代码如下:

/**
* 解决跨域问题
*/
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路劲
        registry.addMapping("/**")
                //设置允许跨域请求的域名
//              .allowedOrigins("*")
                .allowedOriginPatterns("*")
                //设置允许的方法
                .allowedMethods("*")
               //允许的请求头
                .allowedHeaders("*")
                //跨域允许时间
                .maxAge(3600)
                 //是否允许证书 不在默认开启
                .allowCredentials(true);
    }
    
     /**
     * 跨域配置后swagger2可能不能访问,需要增加如下配置
     * @param registry
     * 对静态资源的配置
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
————————

After configuring cross domain, it is found that swagger2 cannot access

Cause analysis: it may be that the built-in interface and static resources of swagger2 are intercepted

The global cross domain configuration of the original springboot is as follows

/**
* 解决跨域问题
*/
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路劲
        registry.addMapping("/**")
                //设置允许跨域请求的域名
//              .allowedOrigins("*")
                .allowedOriginPatterns("*")
                //设置允许的方法
                .allowedMethods("*")
               //允许的请求头
                .allowedHeaders("*")
                //跨域允许时间
                .maxAge(3600)
                 //是否允许证书 不在默认开启
                .allowCredentials(true);
    }
}

After cross domain configuration, it is found that swagger2 cannot continue to access. You need to add the following code to the above class:

 /**
     * 跨域配置后swagger2可能不能访问,需要增加如下配置
     * @param registry
     * 对静态资源的配置
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

The total configuration code is as follows:

/**
* 解决跨域问题
*/
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路劲
        registry.addMapping("/**")
                //设置允许跨域请求的域名
//              .allowedOrigins("*")
                .allowedOriginPatterns("*")
                //设置允许的方法
                .allowedMethods("*")
               //允许的请求头
                .allowedHeaders("*")
                //跨域允许时间
                .maxAge(3600)
                 //是否允许证书 不在默认开启
                .allowCredentials(true);
    }
    
     /**
     * 跨域配置后swagger2可能不能访问,需要增加如下配置
     * @param registry
     * 对静态资源的配置
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}