玩!框架 2.5 如何在异步回调中返回 delegate.call(ctx)



我使用Action进行授权,我需要发出令牌验证请求,但是验证成功后如何返回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);
            }
        });

最新更新