【问题标题】:How to fully disable swagger-ui in spring-boot?(/swagger-ui.html should return 404)如何在 spring-boot 中完全禁用 swagger-ui?(/swagger-ui.html 应该返回 404)
【发布时间】:2017-09-27 18:05:53
【问题描述】:

我已阅读以下主题: Disabling Swagger with Spring MVC

我写道:

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.project.name.controller"))
            .paths(PathSelectors.ant("/api/**"))
            .build()
            .apiInfo(apiInfo())
            .enable(false);
}

但如果我尝试访问 swagger ui:localhost:8080/swagger-ui.html
我懂了

看起来不准确。我可以完全禁用此 URL 吗?例如 404 或类似的东西。

【问题讨论】:

    标签: java spring spring-boot swagger swagger-ui


    【解决方案1】:

    我的答案与之前提供的答案相似,但略有不同。我通常创建一个名为swagger 的单独弹簧配置文件。当我想启用 Swagger 时,我在启动我的应用程序时传递以下 VM 标志 -Dspring.profiles.active=swagger。这是我的 Swagger 配置示例,

    @Profile(value = {"swagger"})
    @Configuration
    @EnableSwagger2
    public class SwaggerConfiguration {
        ...
    }
    

    下次当您尝试在没有 swagger 个人资料的情况下访问 swagger-ui.html 时,您将看到一个空白的 Swagger 屏幕,但不是 404。

    如果你根本不想加载静态的 Swagger UI 页面,你可以写一个简单的控制器,如下图,

    @Profile("!swagger")
    @RestController
    @Slf4j
    public class DisableSwaggerUiController {
    
        @RequestMapping(value = "swagger-ui.html", method = RequestMethod.GET)
        public void getSwagger(HttpServletResponse httpResponse) throws IOException {
            httpResponse.setStatus(HttpStatus.NOT_FOUND.value());
        }
    }
    

    现在,如果您尝试在没有 swagger 个人资料的情况下访问 swagger-ui.html,您将收到 404。

    【讨论】:

    • 您是否尝试访问 swagger-ui?你看到了什么?
    • 我得到一个空的招摇屏幕,但不是 404。更新了我的帖子。
    • 是的,我得到的差不多。实际上浏览器要求我输入基本网址。但我想得到 404
    • 更新了我的帖子。当我尝试使用新控制器时,我得到了 404。
    • 你是如何开始你的服务的?
    【解决方案2】:

    使用 swagger 3.0.0 版本,您可以在相应的环境配置文件application.properties 文件中添加springfox.documentation.enabled=false。 例如,我已将此添加到 application-prod.properties 以在生产中禁用(在运行应用程序时,您必须使用 VM args 指定配置文件,例如 -Dspring.profiles.active=prod

    【讨论】:

    • 这是 Swagger UI 3.0.0 的最佳答案,但并不容易找到(请参阅我的问题:stackoverflow.com/questions/67439123/…)。它甚至没有包含在 SpringFox 的迁移指南或当前文档中。一个非常简单的答案,应该添加到 SpringFox 的文档和迁移指南中。我还想在 SpringFox 的文档中查看 Spring Boot 应用程序属性的完整列表。
    • 这是最好的!
    • 当我尝试这个时,我得到Unable to infer base url. 弹出错误的无限循环,因为 springfox.js 脚本尝试执行 ui、security 和 swagger-resources 脚本,但无法处理404.跨度>
    【解决方案3】:

    您可以将@EnableSwagger2 外部化为它自己的@Configruation,并通过属性或配置文件有条件地加载它。例如

    @Profile("!production")
    @Configuration
    @EnableSwagger2
    public class SwaggerConfiguration{
        //Additional Swagger Beans
    

    }

    这将为任何非生产配置文件激活招摇。

    【讨论】:

      【解决方案4】:

      如果您在控制器中没有 Swagger 注释...只需排除 SwaggerConfig.class 和构建上的 swagger 依赖项

      <build>
          <plugins>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-compiler-plugin</artifactId>
                  <configuration>
                      <excludes>
                          <exclude>com/company/app/SwaggerConfig.java</exclude>
                      </excludes>
                  </configuration>
              </plugin>
              <plugin>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-maven-plugin</artifactId>
                  <configuration>
                      <excludes>
                          <exclude>
                              <groupId>io.springfox</groupId>
                              <artifactId>springfox-swagger-ui</artifactId>
                          </exclude>
                          <exclude>
                              <groupId>io.springfox</groupId>
                              <artifactId>springfox-swagger2</artifactId>
                          </exclude>
                      </excludes>
                  </configuration>
                  <executions>
                      <execution>
                          <goals>
                              <goal>repackage</goal>
                          </goals>
                      </execution>
                  </executions>
              </plugin>
          </plugins>
      </build>
      

      【讨论】:

        【解决方案5】:

        添加到@Hayden 的答案(我没有足够的评论点数..)

        根据 springdoc 文档,您可以使用以下属性禁用 springdoc api 端点和 swagger-ui:

        https://springdoc.org/#disabling-the-springdoc-openapi-endpoints

        # Disabling the /v3/api-docs enpoint
        springdoc.api-docs.enabled=false
        

        https://springdoc.org/#disabling-the-swagger-ui

        # Disabling the swagger-ui
        springdoc.swagger-ui.enabled=false
        

        【讨论】:

          【解决方案6】:

          对于 SpringDoc 用户,请将其添加到您的 application.properties

          springdoc.api-docs.enabled=false
          

          要仅在 prod 配置文件处于活动状态时禁用 Swagger,请将其添加到您的 application-prod.properties

          【讨论】:

            【解决方案7】:

            对于那些使用代码生成的人:

            @Controller
            @Profile({"dev", "staging"})
            public class HomeController {
                @RequestMapping(value = "/")
                public String index() {
                    System.out.println("swagger-ui.html");
                    return "redirect:swagger-ui.html";
                }
            }
            

            并将文件添加到您的 .swagger-codegen-ignore 否则您的更改将在下一个 maven 构建时被覆盖

            【讨论】:

              【解决方案8】:

              当使用 springdoc-openapi-ui 依赖时,可以通过属性禁用 swagger-ui:

              springdoc.swagger-ui.enabled=false
              

              Spring Doc FAQ 中所述。

              【讨论】:

                【解决方案9】:

                只需删除依赖即可。

                <!--<dependency>
                            <groupId>io.springfox</groupId>
                            <artifactId>springfox-swagger-ui</artifactId>
                            <version>2.9.2</version>
                </dependency>-->
                

                不影响编译。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2017-02-19
                  • 2018-09-13
                  • 2019-08-06
                  • 2021-05-18
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2023-01-04
                  相关资源
                  最近更新 更多