在服务中引入了swagger文档,但在生产环境需要屏蔽;

否定请求/v2/api-docs/swagger-ui.html可以看到所有api;

1、关闭swagger v3

springfox:
  documentation:
    # 总开关(同时设置auto-startup=false,否则/v3/api-docs等接口仍能继续访问)
    enabled: false
    auto-startup: false
    swagger-ui:
      enabled: false

2、关闭swagger v2

增加开关参数:swagger.enable

@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xxx.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Swagger2 构建RESTful API")
                      .description("User API 描述")
                .contact(new Contact("xx", "", "xx@xxx.com"))
                .version("1.0")          
                .build();
    }

}

放置到应用配置中:

swagger:
  # 只要不是true就不启用
  enable: false

3、关闭静态资源映射

Spring boot 默认映射mvc的静态资源路径是

classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/

默认swagger的静态资源路径是:/swagger-ui.html

如果服务没有静态资源,建议关闭

spring:
  resources:
    add-mappings: false

关闭后再访问/swagger-ui.html 会得一个404返回.

参考 :生产环境关闭swagger方法

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐