swagger3
一,swagger有哪些环节需要注意安全?
1,生产环境中,要关闭swagger
application.properties中配置:
springfox.documentation.swagger-ui.enabled=false
2,swagger使用一台专用的服务器来部署,
可以访问的ip地址要做限制,
外部的防火墙和应用中都做限制,
3,自定义访问swagger的url
4, 可以访问swagger的用户要做权限的验证
使用示例
package net.cnki.aitool.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
@EnableOpenApi
public class Swagger3Config {
@Value("${swagger.enable}")
private boolean enableSwagger;
@Value("${spring.application.name}")
private String applicationName;
/**
* 创建API应用
* apiInfo() 增加API相关信息
* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
* 本例采用指定扫描的包路径来定义指定要建立API的目录。
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30)
.groupName(applicationName)
.apiInfo(apiInfo())
//是否开启 (true 开启 false隐藏。生产环境建议隐藏)
.enable(enableSwagger)
.select()
//扫描的路径包,设置basePackage会将包下的所有被@Api标记类的所有方法作为api
.apis(RequestHandlerSelectors.basePackage("com.api"))
//指定路径处理PathSelectors.any()代表所有的路径
.paths(PathSelectors.any())
.build();
}
/**
* 创建该API的基本信息(这些基本信息会展现在文档页面中)
* 访问地址:http://项目实际地址/swagger-ui/index.html
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//设置文档标题(API名称)
.title(applicationName)
//文档描述
.description("接口说明")
//服务条款URL
.termsOfServiceUrl("")
//版本号
.version("1.0.0")
.build();
}
}
配置文件
server:
port: 8080
servlet:
context-path: /test
spring:
#访问地址:http://项目实际地址/swagger-ui/index.html ()http://127.0.0.1:8080/test/swagger-ui/index.html)
security:
user:
name: swagger #用户名
password: "swagger" #密码
roles: SWAGGER_ADMIN
swagger:
enable: true #//是否开启 (true 开启 false隐藏。生产环境建议隐藏)