1

I have added routes and JWT token in metadata but while passing token from RSocket client to RSocket server it is adding extra bytes in token.

Client side code -----------

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

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

Server-side token when trying to claim it :

"token" : "ïż½eyJhbGciOiJIUzUxMiJ9.xyz.demo",

As you can see when the token started there were some extra characters.

Does anyone know how to pass the JWT token in metadata in the RSocket client using SpringBoot and JAVA.

3 Answers3

0

Can paste your a complete demo.

rSocketRequester.route("...")
                .metadata(token,  MimeTypeUtils.parseMimeType(WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.getString()))
echooymxq
  • 56
  • 2
0

you need to use WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION because BearerTokenMetadata.BEARER_AUTHENTICATION_MIME_TYPE is deprecated.

RSocketRequester bean configuration can look like:

@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"))
}
0

The java equivalent of Petro Prydorozhnyi's answer. Important the token should not contain the Bearer prefix.

    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);
Gergo
  • 230
  • 2
  • 11