如何在springdoc-openapi-ui (OpenAPI 3.0 /swagger-ui.html
) 中为承载令牌身份验证启用“授权”按钮,例如 JWT。
@Controller
Spring和@Configuration
类需要添加哪些注解?
我更喜欢使用 bean 初始化而不是注解。
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
@Configuration
public class OpenApi30Config {
private final String moduleName;
private final String apiVersion;
public OpenApi30Config(
@Value("${module-name}") String moduleName,
@Value("${api-version}") String apiVersion) {
this.moduleName = moduleName;
this.apiVersion = apiVersion;
}
@Bean
public OpenAPI customOpenAPI() {
final String securitySchemeName = "bearerAuth";
final String apiTitle = String.format("%s API", StringUtils.capitalize(moduleName));
return new OpenAPI()
.addSecurityItem(new SecurityRequirement().addList(securitySchemeName))
.components(
new Components()
.addSecuritySchemes(securitySchemeName,
new SecurityScheme()
.name(securitySchemeName)
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT")
)
)
.info(new Info().title(apiTitle).version(apiVersion));
}
}
代码行
.addSecurityItem(new SecurityRequirement().addList(securitySchemeName))
允许添加全局安全模式并摆脱对每个 @Operation 方法的写入安全性。
@io.swagger.v3.oas.annotations.security.SecurityScheme
使用 bean中的注释为 OpenAPI 3.0 定义全局安全方案@Configuration
:
@Configuration
@OpenAPIDefinition(info = @Info(title = "My API", version = "v1"))
@SecurityScheme(
name = "bearerAuth",
type = SecuritySchemeType.HTTP,
bearerFormat = "JWT",
scheme = "bearer"
)
public class OpenApi30Config {
}
通过引用定义的安全方案来注释每个@RestController
需要承载令牌身份验证 (JWT)的方法:@io.swagger.v3.oas.annotations.Operation
@Operation(summary = "My endpoint", security = @SecurityRequirement(name = "bearerAuth"))
如果你想避免用属性注释@Operation
你内部的每一个,你可以在类级别添加它,影响控制器的每个操作。@RestController
security
请不要忘记您的配置 bean 需要与其他示例中的相同:
@Configuration
@OpenAPIDefinition(info = @Info(title = "My API", version = "v1"))
@SecurityScheme(
name = "bearerAuth",
type = SecuritySchemeType.HTTP,
bearerFormat = "JWT",
scheme = "bearer"
)
public class OpenApi30Config {
}
您所要做的就是@SecurityRequirement(name = "bearerAuth")
在那些您想限制 API 调用的类上使用。请注意,这些注释是继承的,因此您也可以将它们添加到任何接口。
使用所需的注释创建一个标记界面:
@SecurityRequirement(name = "bearerAuth")
public interface SecuredRestController {
}
将标记接口添加到您希望将限制应用于所有操作的那些控制器,例如:
@RestController
@RequestMapping("/hello")
public class HelloController implements SecuredController {
@GetMapping
public String hello() {
return "Hello World";
}
@GetMapping("/{name}")
public String helloWithName(@PathVariable String name) {
return "Hello " + name;
}
}
您可以在没有标记界面的情况下执行此操作,只需说:
@RestController
@RequestMapping("/hello")
@SecurityRequirement(name = "bearerAuth")
public class HelloController {
...
}
正如在另一篇文章中所说,您必须在方法@SecurityRequirement
的@Operation
注释中添加 。
@RestController
@RequestMapping("/hello")
public class HelloController {
@GetMapping
@Operation(summary = "My endpoint", security = @SecurityRequirement(name = "bearerAuth"))
public String hello() {
return "Hello World";
}
@GetMapping("/{name}")
public String helloWithName(@PathVariable String name) {
return "Hello " + name;
}
}