java 如何在Sping Boot 中的Rsocket客户端中传递元数据中的JWT令牌

6ljaweal  于 2023-04-19  发布在  Java
关注(0)|答案(3)|浏览(159)

我已经在元数据中添加了路由和JWT令牌,但是在将令牌从RSocket客户端传递到RSocket服务器时,它在令牌中添加了额外的字节。
客户端代码----------

ByteBuf simpleAuthentication = AuthMetadataCodec.encodeBearerMetadata(ByteBufAllocator.DEFAULT,
                    TenantContext.getCurrentToken().toCharArray());

            CompositeMetadataCodec.encodeAndAddMetadata(metadata, ByteBufAllocator.DEFAULT,
                    "message/x.rsocket.authentication.bearer.v0", simpleAuthentication.asByteBuf());

尝试声明时服务器端令牌:

"token" : "�eyJhbGciOiJIUzUxMiJ9.xyz.demo",

正如您所看到的,当令牌开始时,有一些额外的字符。
有人知道如何使用SpringBoot和JAVA在RSocket客户端中传递元数据中的JWT令牌吗?

qxgroojn

qxgroojn1#

可以粘贴你的一个完整的演示。

rSocketRequester.route("...")
                .metadata(token,  MimeTypeUtils.parseMimeType(WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.getString()))
vaj7vani

vaj7vani2#

您需要使用 WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION,因为不推荐使用BearerTokenMetadata.BEARER_AUTHENTICATION_MIME_TYPE。

RSocketRequesterbean配置可以是:

@Bean
fun getRsocketRequester(strategies: RSocketStrategies): RSocketRequester {

    val authenticationMimeType =
        MimeTypeUtils.parseMimeType(WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.string)

    //build you bearer token
    val bearerMetadata = BearerTokenMetadata(token)

    // add bearer encoder so you will be able to build auth header properly
    val extendedStrategies = strategies.mutate().encoder(BearerTokenAuthenticationEncoder()).build()

    return RSocketRequester.builder()
        .rsocketConnector { rSocketConnector: RSocketConnector ->
            rSocketConnector.reconnect(
                Retry.fixedDelay(
                    2,
                    Duration.ofSeconds(2)
                )
            )
        }
        // pass updated strategy to rsocket builder
        .rsocketStrategies(extendedStrategies)
        // pass your bearer token with up-to-date auth mime type
        .setupMetadata(bearerMetadata, authenticationMimeType)
        .websocket(URI.create("ws://localhost:8090/rsocket"))
}
6ju8rftf

6ju8rftf3#

Petro Prydorozhnyi的答案的java等价物。重要的是token不应该包含Bearer前缀。

String token = "";

    MimeType authenticationMimeType =
            MimeTypeUtils.parseMimeType(WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.getString());
    
    BearerTokenMetadata bearerMetadata = new BearerTokenMetadata(token);

    RSocketRequester requester = RSocketRequester.builder()
            .rsocketConnector(connector -> {
                connector.reconnect(Retry.fixedDelay(2, Duration.ofSeconds(2)));
            })
            .rsocketStrategies(RSocketStrategies
                    .builder()
                    .encoder(new BearerTokenAuthenticationEncoder())
                    .build())
            .setupMetadata(bearerMetadata, authenticationMimeType)
            .websocket(URI.create("ws://address:7000"));

    requester.rsocketClient().connect(); //Don't forget to connect.

    Thread.sleep(10000);

相关问题