Springfox swagger-ui.html 无法推断基本 URL - 由缺少 cookie 引起

标签 spring spring-boot swagger swagger-ui springfox

我们在 API 网关后面有我们的 Spring Boot 服务。使用较早版本的 Springfox - 2.1.2,我们在加载 swagger-ui.html 时没有问题。页。这适用于 Spring Boot 1.4.3.RELEASE。从那时起,我们升级到 Boot 1.5.7,将 Springfox 升级到 2.8.0。

现在,如果我们加载页面,我们会收到一个带有以下长消息的警告框。

Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually



我在网上搜索得到了一些提示,但这些情况似乎不适用于我们。一方面,如果我简单地恢复版本,它会通过相同的 API 网关再次开始工作。

跟踪流量,似乎由 .html 页面对三个 XHR 资源的调用导致了问题。这些从我们的 API 网关返回 401。他们返回 401 的原因是因为没有传递 cookie。

这三个调用是:
  • https://base_address/base_context/swagger-resources/configuration/ui
  • https://base_address/base_context/swagger-resources/configuration/security
  • https://base_address/base_context/swagger-resources

  • 如果我将这些 URL 作为纯浏览器请求加载 - 它们会起作用 - 因为发送了 cookie。

    我怀疑 CORS 是否适用,因为 HTML 是从与 swagger JSON 和实际服务调用相同的地址提供的。

    知道为什么会发生这种情况吗?有人遇到过类似的问题吗?解决方法的建议?非常感谢。

    最佳答案

    添加安全配置——以下 URLS 被跳过以进行身份​​验证::

    private static final String[] AUTH_WHITELIST = {
            "/swagger-resources/**",
            "/swagger-ui.html",
            "/v2/api-docs",
            "/webjars/**"
    };
    
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(AUTH_WHITELIST);
    }
    

    关于Springfox swagger-ui.html 无法推断基本 URL - 由缺少 cookie 引起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49155420/

    相关文章:

    java - 延迟加载和 Hibernate.initialize() 的问题;

    java - 如果 application.properties 中缺少一个键,如何初始化另一个键的值

    java - Swagger UI 错误请求 URL 生成

    php - Swagger 不在 laravel 5.6 中发布 Assets 文件

    api - 在 swagger 2.0 (openapi) 中,如何在 POST 和 PUT 请求之间有不同的资源定义?

    spring - 应用程序上下文加载两次

    java - 启动前休息功能失败

    java - @RequestMapping 在 Spring 版本 1.5.2.RELEASE 中不起作用

    java - 在另一台机器上运行 Spring Boot 应用程序

    java - 在 tomcat 上部署为 WAR 的 Spring boot REST 应用程序无法正常工作