如何在生产中关闭swagger-ui

pbwdgjma  于 2022-11-06  发布在  其他
关注(0)|答案(9)|浏览(848)

我已经将swagger插入到我的spring Boot 应用程序中。Spring boot允许您为您所拥有的每个环境都拥有属性文件。有没有办法在生产环境中禁用swagger?

gfttwv5a

gfttwv5a1#

将您的swagger配置放到单独的配置类中,并使用@Profile annotation -〉对其进行注解,这样它将仅在某些配置文件中被扫描到Spring上下文中。
示例:

@Configuration
@EnableSwagger2
@Profile("dev")
public class SwaggerConfig {
    // your swagger configuration
}

然后,您可以通过命令行定义Sping Boot 应用运行的配置文件:--spring.profiles.active=dev或通过配置文件:spring.profiles.active=dev .
Read this section of Spring Boot docs for more info about @Profile

6xfqseft

6xfqseft2#

如果您在多个环境中工作,则还可以使用**@Profile**作为数组

@Configuration
@EnableSwagger2
@Profile({"dev","qa"})
public class SwaggerConfig {
   // your swagger configuration
}
wixjitnu

wixjitnu3#

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

e0uiprwp

e0uiprwp4#

这是我的配置类:

@Configuration
@Profile("swagger")
@EnableSwagger2
public class SwaggerConfig {

    @Value("${info.build.version}")
    private String buildVersion;

    @Bean
    public Docket documentation() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(regex("/rest/.*"))
                .build()
                .pathMapping("/")
                .apiInfo(metadata());
    }

    private ApiInfo metadata() {
        return new ApiInfoBuilder()
                .title("API documentation of our App")
                .description("Use this documentation as a reference how to interact with app's API")
                .version(buildVersion)
                .contact(new Contact("Dev-Team", "https://dev-website", "dev@mailbox"))
                .build();
    }
}

每当我需要Swagger时,我都会将配置文件swagger添加到环境变量SPRING_PROFILES_ACTIVE

fae0ux8s

fae0ux8s5#

除了使用 profile 配置Spring的答案之外,考虑在反向HTTP代理上设置规则,以阻止从LAN外部访问Swagger端点。这将为您提供一些针对Swagger端点攻击的深度防御。

okxuctiv

okxuctiv6#

对于那些使用代码生成器(生成Swagger 2SpringBoot)的用户:
1.编写您自己的Swagger 2SpringBoot(带有@Profile位),并将其定位在与自动生成的包相同的包路径中。
1.编辑swagger-codegen-maven-plugin,将生成的代码放到src/main/java中(这将覆盖第1点中您自己的代码)。
1.编辑.swagger-codegen-ignore以避免覆盖您的Swagger 2SpringBoot
1.注意其他的东西也会被覆盖,比如pom.xml和application.properties。只要把它们也添加到.swagger-codegen-ignore中就可以了。
好了,好了

gywdnpxw

gywdnpxw7#

1.具有env配置
@Configuration
@EnableSwagger2启用 swagger
@Configuration 文件(“devone”)

  1. application.yaml
profiles: 

active:

 ${MY_ENV:devone}

MY_ENV您将从文件中读取,如.env
.env文件内容:MY_ENV=生产
在生产中保留的其他.env文件仅用于生产凭证。

tyky79it

tyky79it8#

一个老问题,但是如果你使用的是SpringDoc v1.2.12+:

springdoc.swagger-ui.enabled=false
springdoc.api-docs.enabled=false

来源:https://github.com/springdoc/springdoc-openapi/issues/191#issuecomment-558809236

mklgxw1f

mklgxw1f9#

**spring.profiles.active=production with @Profile(“!production”)**为我关闭了产品中的昂首阔步而工作。

例如:-

@Profile("!production")
@Component
@EnableSwagger2
public class SwaggerConfig {
       //TODO
}

相关问题