由于here描述的问题,我正在迁移到Springdoc。现在,在 Swagger UI 中,我没有每个端点的不记名令牌字段,但这是预期的,因为这些端点是安全的。 我有以下配置:
@Bean
public OpenAPI myAPI() {
return new OpenAPI()
.components(new Components()
.addSecuritySchemes("bearer-key",
new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT")))
.info(new Info()
.title("MY API")
.description("Documentation of API v.1.0")
.version("1.0")
).addSecurityItem(
new SecurityRequirement().addList("bearer-jwt", Arrays.asList("read", "write")));
}
我找到了解决方法 - 用以下方式标记每个单独的端点:
@Operation(summary = "some description", security = { @SecurityRequirement(name = "bearer-key") })
但我不喜欢做这种重复的工作。
有没有办法让它成为每个休息端点的全局?
当我使用 swagger 2 时,我必须提供类似的令牌
承载 eyJhbGciOi....
但现在我必须提供它
eyJhbGciOi....
public OpenAPI myAPI() { return new OpenAPI() .components(new Components() .addSecuritySchemes("bearer-key", new SecurityScheme() .type(SecurityScheme.Type.HTTP) .scheme("bearer") .bearerFormat("JWT") ) ) .info(new Info() .title("My API") .description("Documentation of API v.1.0") .version("1.0") ).addSecurityItem( new SecurityRequirement() .addList("bearer-jwt", Arrays.asList("read", "write")) .addList("bearer-key", Collections.emptyList()) ); }
io.swagger.v3.oas.annotations.OpenAPIDefinition
全局定义
io.swagger.v3.oas.annotations.security.SecurityRequirement
:import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.customizers.OpenApiCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@OpenAPIDefinition(security = {@SecurityRequirement(name = "bearer-key")})
public class OpenApiConfig {
@Bean
public OpenApiCustomizer customerGlobalHeaderOpenApiCustomiser() {
return openApi -> openApi.getComponents()
.addSecuritySchemes("bearer-key",
new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT"));
}
}