casting到kotlin中的jsonobject

xpcnnkqh  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(432)

我有以下java代码:

var jwks = ((List<Object>) keys.getList()).stream()
                            .map(o -> new JsonObject((Map<String, Object>) o))
                            .collect(Collectors.toList());

希望安全地翻译成kotlin代码。
当我将代码复制到intellj中时,它对我的翻译如下:

val jwks = (keys.list as List<Any?>).stream()
        .map { o: Any? -> JsonObject(o as Map<String?, Any?>?) }
        .collect(Collectors.toList())

我能做得更好吗?还是应该让它保持原样。
更新
也许我需要提供更多的背景。我要做的是,在kotlin中使用keydape实现对vert.x的jwt授权https://vertx.io/blog/jwt-authorization-for-vert-x-with-keycloak/. 我正试图重写这个方法

private Future<Startup> setupJwtAuth(Startup startup) {

    var jwtConfig = startup.config.getJsonObject("jwt");
    var issuer = jwtConfig.getString("issuer");
    var issuerUri = URI.create(issuer);

    // derive JWKS uri from Keycloak issuer URI
    var jwksUri = URI.create(jwtConfig.getString("jwksUri", String.format("%s://%s:%d%s",
            issuerUri.getScheme(), issuerUri.getHost(), issuerUri.getPort(), issuerUri.getPath() + "/protocol/openid-connect/certs")));

    var promise = Promise.<JWTAuth>promise();

    // fetch JWKS from `/certs` endpoint
    webClient.get(jwksUri.getPort(), jwksUri.getHost(), jwksUri.getPath())
            .as(BodyCodec.jsonObject())
            .send(ar -> {

                if (!ar.succeeded()) {
                    startup.bootstrap.fail(String.format("Could not fetch JWKS from URI: %s", jwksUri));
                    return;
                }

                var response = ar.result();

                var jwksResponse = response.body();
                var keys = jwksResponse.getJsonArray("keys");

                // Configure JWT validation options
                var jwtOptions = new JWTOptions();
                jwtOptions.setIssuer(issuer);

                // extract JWKS from keys array
                var jwks = ((List<Object>) keys.getList()).stream()
                        .map(o -> new JsonObject((Map<String, Object>) o))
                        .collect(Collectors.toList());

                // configure JWTAuth
                var jwtAuthOptions = new JWTAuthOptions();
                jwtAuthOptions.setJwks(jwks);
                jwtAuthOptions.setJWTOptions(jwtOptions);
                jwtAuthOptions.setPermissionsClaimKey(jwtConfig.getString("permissionClaimsKey", "realm_access/roles"));

                JWTAuth jwtAuth = JWTAuth.create(vertx, jwtAuthOptions);
                promise.complete(jwtAuth);
            });

    return promise.future().compose(auth -> {
        jwtAuth = auth;
        return Future.succeededFuture(startup);
    });
}

变成了Kotlin语。

rur96b6h

rur96b6h1#

您可以使用kotlin的星投影,它基本上以类型安全的方式处理未知泛型。

(keys.list as List<Map<*, *>>).map { JsonObject(it) }

由于没有多重Map,因此不需要流/序列api。
但是,如果要使用延迟求值(每个元素以相同的方式进行所有Map,然后进行下一个元素,而不是Map所有元素,然后运行下一个Map):

(keys.list as List<Map<*, *>>)
        .asSequence()
        .map { JsonObject(it) }
        .map { /* Maybe some other mapping */ }
        .filter { /* Maybe some filter */ }
        .take(5)  // Or some other intermediate operation
        .toList()  // Finally start the operations and collect

编辑:我忘了钥匙是 String ,这样你就可以 List<Map<String, *>> 相反:)

相关问题