如何启用springdoc-openapi-ui (OpenAPI 3.0 /swagger-ui.html)中的Authorize按钮进行持有者Token鉴权,例如JWT。
必须向Spring @Controller和@Configuration类添加哪些注释?


发布于 2020-01-24 22:56:24
使用@Configuration bean中的注释@io.swagger.v3.oas.annotations.security.SecurityScheme为OpenAPI 3.0定义全局安全方案:
@Configuration
@OpenAPIDefinition(info = @Info(title = "My API", version = "v1"))
@SecurityScheme(
name = "bearerAuth",
type = SecuritySchemeType.HTTP,
bearerFormat = "JWT",
scheme = "bearer"
)
public class OpenApi30Config {
}使用引用定义的安全方案的@io.swagger.v3.oas.annotations.Operation注释每个需要持有者令牌身份验证(JWT)的@RestController方法:
@Operation(summary = "My endpoint", security = @SecurityRequirement(name = "bearerAuth"))发布于 2020-03-13 15:05:20
我更喜欢使用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编写安全性。
发布于 2021-02-04 23:48:17
如果您希望避免使用security属性注释@RestController中的每个@Operation,则可以在类级别添加此属性,以影响控制器的每个操作。
请不要忘记,您的配置bean需要与其他示例中的配置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 {
...
}现在,您已经保护了这两个操作,并且需要一个JWT令牌。

在方法级别添加安全需求
正如另一篇文章中所说,你必须在你的方法的@Operation注解中添加@SecurityRequirement。
@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;
}
}这仅限制第一个操作,而不限制第二个操作。

https://stackoverflow.com/questions/59898874
复制相似问题