swagger在生产环境中禁用

本文介绍如何使用Spring Boot的@ConditionalOnProperty注解控制Swagger在生产环境中的启用与禁用。通过配置application.properties文件中的特定属性值,可以灵活地在不同环境中开关Swagger文档。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 问题

    • 最近在项目上遇到要在生产环境下禁用swagger的问题,找到两种解决办法,另一种在我的前面一篇博客
  • 解决方案

    • 经过一番寻觅,发现了Spring boot中有个注解@ConditionalOnProperty,这个注解能够控制某个configuration是否生效。具体操作是通过其两个属性name以及havingValue来实现的,其中name用来从application.properties中读取某个属性值,如果该值为空,则返回false;如果值不为空,则将该值与havingValue指定的值进行比较,如果一样则返回true;否则返回false。如果返回值为false,则该configuration不生效;为true则生效。
  • 代码

    import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.ParameterBuilder;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.schema.ModelRef;
    import springfox.documentation.service.Parameter;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    import java.util.ArrayList;
    import java.util.List;
     
    @Configuration
    @ConditionalOnProperty(prefix = "swagger2",value = {"enable"},havingValue = "true")
    @EnableSwagger2
    public class SwaggerConfiguration {
     
      @Bean
      public Docket swagger(){
        return new Docket(DocumentationType.SWAGGER_2)
            .groupName("default")
            .apiInfo(new ApiInfoBuilder().title("SSP School API").version("1.0.0").build())
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.pobo.pbsimu.opcontest.controller"))
            .build()
            .globalOperationParameters(globalOperationParameters());
      }
     
     
      private List<Parameter> globalOperationParameters(){
        List<Parameter> parameters = new ArrayList<>();
        parameters.add(new ParameterBuilder().name("ACCESS-TOKEN").description("ACCESS-TOKEN").required(false).parameterType("header").modelRef(new ModelRef("string")).build());
        return parameters;
      
      }
      
    }
### 解决Swagger生产环境中无法正常工作的问题 #### 1. 使用 `@Profile` 注解控制环境 为了防止在生产环境中暴露敏感的API文档,通常会在开发和测试环境下启用Swagger UI。通过使用 `@Profile({"dev", "test"})` 注解来限定Swagger配置类仅在这两个特定模式下加载[^4]。 ```java @Configuration @EnableSwagger2 @Profile({"dev", "test"}) public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } } ``` 这样做的好处是可以有效避免因意外情况而导致生产服务器上的接口被公开访问的风险。 #### 2. 设置主机地址参数 当应用程序部署到不同的域或子域名时,可能会遇到跨源资源共享(CORS)问题或是路径解析错误的情况。可以通过设置 JVM 参数 `-Dspringfox.documentation.swagger.v2.host=yourdomain.com` 来指定正确的主机名[^3]。 这一步骤对于那些采用反向代理(如Nginx)转发请求的应用尤为重要,因为它能确保生成的链接指向正确的服务端口与协议。 #### 3. 调整 Nginx 反向代理配置 如果项目是基于 Spring Boot 并且前端页面通过 Nginx 访问,则需注意调整 Nginx 的 location 块以支持 `/v2/api-docs` 和其他静态资源文件的正确路由: ```nginx location /swagger-ui.html { proxy_pass http://localhost:8080; } location /webjars/ { proxy_pass http://localhost:8080; } location /v2/api-docs { proxy_pass http://localhost:8080/v2/api-docs; } ``` 以上更改有助于解决由于路径映射不当引起的Swagger界面空白或其他显示异常现象。 #### 4. 处理浏览器缓存带来的影响 有时即使解决了上述所有技术层面的问题之后仍然看不到预期效果,可能是因为浏览器内部存储了旧版本的数据所致。尝试清除浏览记录或者禁用开发者工具中的缓存功能后再刷新页面查看结果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值