如何在生产环境禁用Swagger

SpringMVC集成springfox-swagger2和springfox-swagger-ui很简单,只需要两步:
(1)pom中添加依赖
[java] view plain copy
<dependency>  
            <groupId>io.springfox</groupId>  
            <artifactId>springfox-swagger-ui</artifactId>  
            <version>${springfox-swagger.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>io.springfox</groupId>  
            <artifactId>springfox-swagger2</artifactId>  
            <version>${springfox-swagger.version}</version>  
        </dependency>  
(2)添加Swagger的配置类:
[java] view plain copy
@Configuration   
@EnableSwagger2   
@EnableWebMvc  
@ComponentScan("com.XXX.controller")  
public class SwaggerConfig{  
  
}  
然后就可以通过http://localhost/swagger-ui.html看到项目中所有的接口信息了,通过http://localhost/v2/api-docs就能看到json数据。
转载请标明出处:http://blog.csdn.net/goldenfish1919/article/details/78280051
但是,如何在生产环境禁用这些api文档呢?试了很多种方式,最终找到一个简单实用的办法:
[java] view plain copy
@Configuration   
@EnableSwagger2   
@EnableWebMvc  
@ComponentScan("com.XXX.controller")  
public class SwaggerConfig{  
      
    @Autowired  
    ConfigService configService;  
      
    @Bean  
    public Docket customDocket() {  
        if(configService.getServerEnv() == ServerEnvEnum.ONLINE) {  
            return new Docket(DocumentationType.SWAGGER_2)  
            .apiInfo(apiInfoOnline())  
        .select()  
                .paths(PathSelectors.none())//如果是线上环境,添加路径过滤,设置为全部都不符合  
        .build();  
        }else {  
            return new Docket(DocumentationType.SWAGGER_2)  
            .apiInfo(apiInfo());  
        }  
    }  
  
    private ApiInfo apiInfo() {  
        return new ApiInfoBuilder()  
                .title("XXX系统")  
                .description("XXX系统接口")  
                .license("")  
                .licenseUrl("")  
                .termsOfServiceUrl("")  
                .version("1.0.0")  
                .contact(new Contact("","", ""))  
                .build();  
    }  
    private ApiInfo apiInfoOnline() {  
        return new ApiInfoBuilder()  
                .title("")  
                .description("")  
                .license("")  
                .licenseUrl("")  
                .termsOfServiceUrl("")  
                .version("")  
                .contact(new Contact("","", ""))  
                .build();  
    }  
}  
现在http://localhost/swagger-ui.html这个页面虽然还能访问,那是却看不到任何内容了,包括http://localhost/v2/api-docs也是一样。
应该还有更好的办法!
--------------------------再进一步
修改一下依赖,为了编译通过,单独把swagger-annotations拿出来:
[html] view plain copy
<dependency>  
            <groupId>io.swagger</groupId>  
            <artifactId>swagger-annotations</artifactId>  
            <version>${springfox-swagger-anno.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>io.springfox</groupId>  
            <artifactId>springfox-swagger2</artifactId>  
            <version>${springfox-swagger.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>io.springfox</groupId>  
            <artifactId>springfox-swagger-ui</artifactId>  
            <scope>${swagger.scope}</scope>  
            <version>${springfox-swagger.version}</version>  
        </dependency>  
[html] view plain copy
<profile>  
            <id>online</id>  
            <properties>  
                <profiles.active>online</profiles.active>  
                <swagger.scope>provided</swagger.scope>  
            </properties>  
        </profile>  
线上环境把springfox-swagger-ui的依赖配置成是provided,这样可以实现http://localhost/swagger-ui.html访问404。但是:http://localhost/v2/api-docs和http://localhost/swagger-resources等接口依然能够访问,尽管是空的!
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值