【问题标题】:Play! Framework 2.5 How to return delegate.call(ctx) in asynchronous callback玩! Framework 2.5 如何在异步回调中返回delegate.call(ctx)
【发布时间】:2016-06-30 22:11:50
【问题描述】:

我使用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);
}

【问题讨论】:

    标签: java playframework frameworks delegates


    【解决方案1】:

    解决了!

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-29
      • 2012-03-13
      • 1970-01-01
      • 2017-03-29
      • 2017-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多