Open yongoe1024 opened 1 year ago
这个确实难搞。让我想想
我使用最新版本时,在/auth/token 接口内返回属性 token_type 后,可以将token 值带入全局header。
我4.1.0
版本依然有这个问题,使用自带的 swagger-ui.html 可以正常的携带认证请求头。
以下设置的代码:
@Bean
public OpenAPI openAPI(Info info) {
log.info("【SpringBean注册】 => io.swagger.v3.oas.models.OpenAPI");
return new OpenAPI()
.info(info)
.schemaRequirement(tokenName, this.securityScheme())
.addSecurityItem(new SecurityRequirement().addList(tokenName));
}
private SecurityScheme securityScheme() {
SecurityScheme scheme = new SecurityScheme();
scheme.setType(SecurityScheme.Type.APIKEY);
scheme.setName(tokenName);
scheme.setIn(SecurityScheme.In.HEADER);
return scheme;
}
把security设置到每个path里就可以了
@Bean
public GlobalOpenApiCustomizer globalOpenApiCustomizer() {
return openApi -> openApi.getPaths().values().stream().flatMap(pathItem -> pathItem.readOperations().stream()).forEach(operation -> operation.security(openApi.getSecurity()));
}
把security设置到每个path里就可以了
@Bean public GlobalOpenApiCustomizer globalOpenApiCustomizer() { return openApi -> openApi.getPaths().values().stream().flatMap(pathItem -> pathItem.readOperations().stream()).forEach(operation -> operation.security(openApi.getSecurity())); }
it works for me~ (seems need openapi.schemaRequirement and operation.security both) @xiaoymin
首先不是knife4j的问题,就看看能不能有啥办法 springboot3,
springdoc官网中,添加权限信息,需要配置文件
同时在接口添加security = { @SecurityRequirement(name = "Authorization") },只有添加的接口在能携带请求头,非常麻烦