Java 如何在生产中关闭swagger ui
我已将swagger插入我的spring boot应用程序。SpringBoot允许您拥有每个环境的属性文件。有没有一种方法可以禁用生产环境中的swagger?将您的swagger配置放入单独的配置类中,并使用Java 如何在生产中关闭swagger ui,java,spring,spring-boot,swagger,swagger-ui,Java,Spring,Spring Boot,Swagger,Swagger Ui,我已将swagger插入我的spring boot应用程序。SpringBoot允许您拥有每个环境的属性文件。有没有一种方法可以禁用生产环境中的swagger?将您的swagger配置放入单独的配置类中,并使用@Profileannotation->对其进行注释,以便仅在某些配置文件中将其扫描到Spring上下文中 例如: @Configuration @EnableSwagger2 @Profile("dev") public class SwaggerConfig { // your
@Profile
annotation->对其进行注释,以便仅在某些配置文件中将其扫描到Spring上下文中
例如:
@Configuration
@EnableSwagger2
@Profile("dev")
public class SwaggerConfig {
// your swagger configuration
}
您可以通过命令行:--Spring.profiles.active=dev
或通过配置文件:Spring.profiles.active=dev
定义Spring Boot应用程序运行的配置文件
这是我的配置类:
@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的地方,我将profile
Swagger
添加到环境变量SPRING\u PROFILES\u ACTIVE
如果您在多个环境中工作,那么您也可以使用@profile作为数组
@Configuration
@EnableSwagger2
@Profile({"dev","qa"})
public class SwaggerConfig {
// your swagger configuration
}
对于使用code gen(生成Swagger2SpringBoot)的用户:
profiles:
active:
${MY_ENV:devone}
在生产中,仅为生产凭证保留其他.env文件。
除了使用配置文件配置Spring的答案之外,还可以考虑在反向HTTP代理上设置规则,以阻止从LAN外部访问SWAGER端点。这将给您在攻克端点的攻击方面提供一些深度防御。
用Siggg3.0.0版本,可以添加<代码> Spring狐。lse在相应的环境配置文件
application.properties
文件中。例如,我已将其添加到application-prod.properties
中以在生产中禁用(运行应用程序时,必须使用VM参数指定配置文件,如-Dspring.profiles.active=prod
)我们已经这样做了,尽管api的核心没有显示出来,但是扩展->swagger-ui.html仍然显示出来。有没有办法让swagger-ui.html甚至不会产生?@user301693如果你使用Maven,你可以在特定的Maven配置文件中加载swagger依赖项,这应该可以做到s、 @g00glen00b,并且PROD与其他环境有不同的工件?我想QA和OPS人员对此不会很满意。我知道这是一个老问题,但我们使用@Profile(“!PROD”)来避免显式指定大量其他配置文件。希望它能帮助某些人。这本质上是重复的(即“使用配置文件”)/swagger-ui.html仍然可用,但没有方法。有没有办法禁止URL?我认为这是启用swagger on demand的更简洁的方法,而不是禁用某些配置文件。是的,这是有效的,我还想知道为什么否决票…谢谢Jin KwonIt不是我的否决票,但这可能只会禁用json端点,而不是web端点jarui页面?
@Profile("!production")
@Component
@EnableSwagger2
public class SwaggerConfig {
//TODO
}