如何在 spring-boot 中完全禁用 swagger-ui?(/swagger-ui.html should return 404)
How to fully disable swagger-ui in spring-boot?(/swagger-ui.html should return 404)
我已阅读以下主题:
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 或类似的东西。
您可以将 @EnableSwagger2
外部化为它自己的 @Configruation
并通过 属性 或配置文件有条件地加载它。例如
@Profile("!production")
@Configuration
@EnableSwagger2
public class SwaggerConfiguration{
//Additional Swagger Beans
}
这将为任何非生产配置文件激活 swagger。
我的答案与之前提供的答案类似,但略有不同。我通常创建一个名为 swagger
的单独 spring 配置文件。当我想启用 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 注释...只需排除 SwaggerConfig.class 和 swagger 对 build
的依赖
<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>
对于使用代码 gen 的用户:
@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 构建中被覆盖
对于 swagger 3.0.0 版本,您可以在相应的环境配置文件 application.properties
文件中添加 springfox.documentation.enabled=false
。
例如,我已将此添加到 application-prod.properties
以在生产中禁用(而 运行 您必须使用 VM 参数指定配置文件的应用程序,如 -Dspring.profiles.active=prod
)
只需删除依赖项。
<!--<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>-->
不影响编译
对于 SpringDoc 用户,请将其添加到您的 application.properties
springdoc.api-docs.enabled=false
要仅在 prod
配置文件处于活动状态时禁用 Swagger,请改为将其添加到您的 application-prod.properties
加入@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
当使用 springdoc-openapi-ui 依赖项时,可以通过 属性:
禁用 swagger-ui
springdoc.swagger-ui.enabled=false
如Spring Doc FAQ所述。
我已阅读以下主题: 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 或类似的东西。
您可以将 @EnableSwagger2
外部化为它自己的 @Configruation
并通过 属性 或配置文件有条件地加载它。例如
@Profile("!production")
@Configuration
@EnableSwagger2
public class SwaggerConfiguration{
//Additional Swagger Beans
}
这将为任何非生产配置文件激活 swagger。
我的答案与之前提供的答案类似,但略有不同。我通常创建一个名为 swagger
的单独 spring 配置文件。当我想启用 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 注释...只需排除 SwaggerConfig.class 和 swagger 对 build
的依赖<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>
对于使用代码 gen 的用户:
@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 构建中被覆盖
对于 swagger 3.0.0 版本,您可以在相应的环境配置文件 application.properties
文件中添加 springfox.documentation.enabled=false
。
例如,我已将此添加到 application-prod.properties
以在生产中禁用(而 运行 您必须使用 VM 参数指定配置文件的应用程序,如 -Dspring.profiles.active=prod
)
只需删除依赖项。
<!--<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>-->
不影响编译
对于 SpringDoc 用户,请将其添加到您的 application.properties
springdoc.api-docs.enabled=false
要仅在 prod
配置文件处于活动状态时禁用 Swagger,请改为将其添加到您的 application-prod.properties
加入@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
当使用 springdoc-openapi-ui 依赖项时,可以通过 属性:
禁用 swagger-uispringdoc.swagger-ui.enabled=false
如Spring Doc FAQ所述。