1.POM文件只需要引入一个依赖即可
2. swagger配置文件如下
注意:要添加“@EnableWebMvc"注解,详细代码如下:
@Configuration
@EnableWebMvc
public class SwaggerConfig {
@Value("${swagger.enabled}")
boolean enabled;
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
// 是否开启swagger
.enable(enabled)
.select()
// 过滤条件,扫描指定路径下的文件
.apis(RequestHandlerSelectors.basePackage("com.bigdata.demo.controller"))
// 指定路径处理,PathSelectors.any()代表不过滤任何路径
.paths(PathSelectors.any())
.build()
.securitySchemes(Arrays.asList(apiKey()))
.securityContexts(Arrays.asList(securityContext()));
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("数据中台demo接口文档")
.description("数据中台demo Restful APIs")
.version("1.0")
.build();
}
private ApiKey apiKey() {
return new ApiKey("JWT", "Authorization", "header");
}
/**
* 这里设置 swagger 认证的安全上下文
*/
private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.any())
.build();
}
/**
* 这里是写允许认证的scope
*/
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Arrays.asList(new SecurityReference("JWT", authorizationScopes));
}
}
这里采用的规范是“DocumentationType.SWAGGER_2”而不是“DocumentationType.OAS_30”,“DocumentationType.OAS_30”有些问题, 采用“DocumentationType.OAS_30”,在发送请求的时候,token在header里边的key
并不是“ApiKey("JWT", "Authorization", "header");”里边设置的“Authorization”,而是变成了“JWT”,具体原因尚不清楚。 如下图所示:
采用“DocumentationType.SWAGGER_2”就没有上述问题,如下图所示:
标签:swagger,return,springboot,DocumentationType,swagger3.0,JWT,private,new,权限 From: https://www.cnblogs.com/andy1234/p/17852219.html