Swagger是我们常用的API Doc工具,非常便于开发人员调试,后台和前端开发人员协作,以及对外公布API使用。如何在生产环境中禁止swagger了?
@Profile("beta") // 只允许在测试服务器访问Swagger
package com.demo.swagger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author HHM
*/
@Configuration
@EnableSwagger2
@Profile("beta")
public class SwaggerConfig {
@Bean
public Docket buildDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(buildApiInf())
.select()
.apis(RequestHandlerSelectors.basePackage("com.demo.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo buildApiInf() {
return new ApiInfoBuilder()
.title("管理系统接口文档")
.description("后台管理 API 接口文档")
.version(DocumentationType.SWAGGER_2.getVersion())
.build();
}
}
以下是正式环境访问