gpt4 book ai didi

java - 玩! Framework 2.5 如何在异步回调中返回 delegate.call(ctx)

转载 作者:行者123 更新时间:2023-11-30 03:00:53 25 4
gpt4 key购买 nike

我使用Action进行授权,需要发出token验证请求,但是验证成功后如何返回delegate.call(ctx)。

public class JWTSecureAction extends Action.Simple {

@Inject
WSClient ws;

public CompletionStage<Result> call(Http.Context ctx) {
Result unauthorized = Results.unauthorized("unauthorized");
String token = getTokenFromHeader(ctx);
if (token != null) {
WSRequest request = ws.url("")
.setHeader(Http.HeaderNames.AUTHORIZATION, token);
CompletionStage<JsonNode> jsonResponse = request.get()
.thenApply(WSResponse::asJson);

CompletionStage<Result> ret = jsonResponse.thenApply(jsonNode -> {
if (jsonNode.get("success").equals("true")) {
return delegate.call(ctx); //Error! CompletionStage<Result> cannot be Converted to Result
} else {
return unauthorized;
}
});
return ret;
}
return CompletableFuture.completedFuture(unauthorized);
}

最佳答案

解决了!

CompletionStage<Result> ret = jsonResponse.thenCompose(jsonNode -> {
if (jsonNode.get("success").equals("true")) {
return delegate.call(ctx);
} else {
return CompletableFuture.completedFuture(unauthorized);
}
});

关于java - 玩! Framework 2.5 如何在异步回调中返回 delegate.call(ctx),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36011824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com