CORS跨域导致swagger-ui.html 或者doc.html 无法显示问题

SwaggerConfig:

@Configuration
@EnableSwagger2
@Profile({"dev", "test", "local"})
public class Swagger2Config extends WebMvcConfigurerAdapter {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.tencent.iov.open.controller"))
                .paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo() {
        String desc = "公共header参数说明(需URLEncoder编码):\r\n"
                + "参数名:Version-Code \r\t 参数类型: String \r\t 参数说明:版本号 \r\n "
                + "参数名:Platform \r\t 参数类型: String \r\t 参数说明:用于标识平台类型:Android、iOS \r\n "
                + "参数名:Device-Id \r\t 参数类型: String\r\t 参数说明:设备唯一标识 \r\n "
                + "参数名:Device-Type \r\t 参数类型: String \r\t 参数说明:设备型号 \r\n "
                + "参数名:Timestamp \r\t 参数类型: Long \r\t 参数说明:毫秒时间戳 \r\n "
                + "Cookie 参数说明:\r\n"
                + "参数名:token \r\t 参数类型: String \r\t 参数说明:令牌 \r\n ";
        return new ApiInfoBuilder().title("开发平台接口文档").description(desc).termsOfServiceUrl("")
                .contact(new Contact("panqian", "", "18682387045@qq.com")).version("develop_1.5").build();
    }

    /**
     * swagger ui资源映射
     *
     * @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/");
    }

    /**
     * swagger-ui.html路径映射,浏览器中使用/api-docs访问
     *
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/api-docs", "/swagger-ui.html");
    }
}

跨域配置文件 CorsConfig:

@Configuration
@EnableWebMvc
public class CorsConfig extends WebMvcConfigurationSupport {

    /*
     * 这里主要为了解决跨域问题,所以重写addCorsMappings方法
     */
    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .exposedHeaders("access-control-allow-headers",
                        "access-control-allow-methods",
                        "access-control-allow-origin",
                        "access-control-max-age",
                        "X-Frame-Options")
                .allowCredentials(true).maxAge(3600);
        super.addCorsMappings(registry);
    }

    /**
     * 跨域配置后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/");
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值