- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Java EE 8 应用程序,我在其中使用 OpenAPI 注释来定义我的 REST 端点 并自动生成一个 招摇的用户界面。对于身份验证,我使用 JSON Web Tokens (JWT)。
当我从 Postman 发送请求时一切正常,但是,我不知道如何将不记名 token 的字段添加到我的 Swagger UI。
我正在使用 @SecurityScheme
注释定义我的安全方案:
@SecurityScheme(
securitySchemeName = "JWT",
description = "JWT authentication with bearer token",
type = SecuritySchemeType.HTTP,
scheme = "bearer",
bearerFormat = "Bearer [token]"
)
public class ApplicationConfig extends Application {
}
我尝试将此方案作为 @SecurityRequirement
添加到我的资源的 @OpenAPIDefinition
注释中,并直接添加到我的方法中。
@Path("/items")
@OpenAPIDefinition(
info = @Info(title = "Items resource", version = "v1"),
security = @SecurityRequirement(name = "JWT")
)
@Transactional(value = TxType.REQUIRES_NEW)
@Interceptors({RolesAllowedInterceptor.class})
@SecurityScheme(
securitySchemeName = "JWT",
description = "JWT authentication with bearer token",
type = SecuritySchemeType.HTTP,
scheme = "bearer",
bearerFormat = "Bearer [token]"
)
public class ItemsResource {
(...)
@GET
@Operation(description = "Returns the item list overview")
@APIResponse(responseCode = "200", description = "Valid response")
@APIResponse(responseCode = "401", description = "Authentication required")
@APIResponse(responseCode = "500", description = "Unexpected exception")
@Produces({MediaType.APPLICATION_JSON})
@SecurityRequirement(name ="JWT", scopes = "write: read")
@RolesAllowed({Constants.USER_ROLE_EXPERT})
public Response getItemListOverview() throws TechnicalException {
ItemListOverviewVO itemListOverviewVO = logic.getItemListOverview();
return Response.status(Status.OK).entity(itemListOverviewVO).build();
}
现在我的 OpenAPI JSON 文件中有 security
信息,但 UI 中仍然没有 Authorization 参数字段。
我还发现旧的 Swagger API 中曾经有一个 @ApiImplicitParameter
注解(参见 Swagger UI passing authentication token to API call in header ),但它似乎已从 OpenAPI 中删除。
所以我尝试使用 @HeaderParam
代替(参见 Jersey project Swagger-UI doesn't send @HeaderParam while @PathParam is sent ):
public Response getItemListOverview(@HeaderParam("Authorization") String bearerToken) throws TechnicalException {
现在我的 UI 中有一个 Authorization 字段,但是当我测试端点时,请求没有 Authorization header 。我在浏览器的网络分析中看不到它。
OpenAPI documentation到目前为止帮助不大。我在这里遗漏了什么吗?
最佳答案
关键是将 @SecurityScheme
注释嵌入到 @Components()
中,并将其作为参数传递给 @OpenAPIDefinition
注释:
@OpenAPIDefinition(
info = @Info(title = "My application", version = "1.0.0"),
servers = {@Server(url = "/myapp", description = "localhost") },
security = @SecurityRequirement(name = "JWT"),
components = @Components(securitySchemes = @SecurityScheme(
securitySchemeName = "JWT",
description = "JWT authentication with bearer token",
type = SecuritySchemeType.HTTP,
scheme = "bearer",
bearerFormat = "Bearer [token]"))
)
public class ApplicationConfig extends Application {
}
关于jakarta-ee - 将不记名 token 的字段添加到 Java EE/Jersey 中生成的 Swagger UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59410640/
我是一名优秀的程序员,十分优秀!