Swagger自定义配置导致其无妨正常访问
今天在折腾swagger的时候,一直没法正常访问,是因为我有些自定义配置,按照博客里写的,在启动类上加上了@swagger2注解,可以正常访问了,但是这个不是我自定义配置里面的内容。下面是我自定义配置。
@Profile({"dev","test"})
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
//扫描所有有注解的api
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build().apiInfo(apiInfo());
}
@Bean
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("DSAAS 后端服务 RESTful APIs")
.description("接口统一返回对象定义说明:code:返回码 (200-成功 "
+ "500-错误),message:返回状态数据,data:返回业务数据对象")
.termsOfServiceUrl("http://www.tydic.com")
.version("1.0.0")
.license("LICENSE")
.licenseUrl("http://www.tydic.com")
.build();
}
}
后来分析过后,发现和@profile注解有关系,这个注解意思是只有dev和test配置可以访问其配置,也就是避免了在生产上可以直接访问swagger,在生产上访问swagger肯定是不安全的,接口信息全部会暴露等等问题。
问题产生原因
自己加的@profile注解,但是pom文件里却没有对应的创建这两个文件,导致无法读取对应的配置
如下文所示,需要在pom文件里面配置对饮的profile才可以!
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profileActive>dev</profileActive>
</properties>
</profile>
</profiles>
解决途径
正确配置pom中的profile