我正在使用 vert.x-openAPI 模块,并且我的 petstore.yaml 具有以下安全性:
/pets/{petId}:
get:
summary: Info for a specific pet
operationId: showPetById
security:
- ApiKeyAuth: []
这是我的服务器端(Verticle)上使用 JWT Auth Handler 的 operationId 的相关部分(我将在我的代码中省略 JWT Auth 提供程序创建部分):
routerBuilder.setOptions(routerBuilderOptions)
.securityHandler("ApiKeyAuth", JWTAuthHandler.create(jwtAuthProvider))
.operation("showPetById")
.handler(routingContext -> {
RequestParameters params = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
Integer id = params.pathParameter("petId").getInteger();
Optional<JsonObject> pet = getAllPets()
.stream()
.filter(p -> p.getInteger("id").equals(id))
.findFirst();
if(pet.isPresent()){
routingContext
.response()
.setStatusCode(200)
.putHeader(HttpHeaders.CONTENT_TYPE,"application/json")
.end(pet.get().encode());
}else {
routingContext.fail(404, new Exception("Pet not found"));
}
});
然后,我尝试从我的测试访问该端点并获得成功(并且授权正常)响应:
@Test
public void breakingTheAuth(Vertx vertx, VertxTestContext vertxTestContext){
Checkpoint checkFlag = vertxTestContext.checkpoint(2);
//Checkpoint jwtCheck = vertxTestContext.checkpoint();
vertxTestContext.verify(()->{
JWTAuth jwt = loadJWTAuthprovider(vertx);
checkFlag.flag();
RouterBuilder.create(vertx,"petstore.yaml")
.onSuccess(routerBuilder -> {
routerBuilder.setOptions(FACTORY_OPTIONS)
.securityHandler("ApiKeyAuth", JWTAuthHandler.create(jwt))
.operation("showPetById")
.handler(routingContext -> {
given()
.port(8080)
.get("/pets/2")
.then()
.statusCode(200);
});
checkFlag.flag();
}).onFailure(throwable -> {
vertxTestContext.failNow(throwable);
});
});
}
I would like to access to this endpoint http://localhost:8080/pets/2 with a 200 status code as a response but I always get the 401 Unauthorized (Maybe a JWTAuth problem, I've read about using oAUTH it could be a better option).
Maybe I am missing anything, should I use the vertx-webclient to access to that endpoint?