rsocket / rsocket-java

Java implementation of RSocket
http://rsocket.io
Apache License 2.0
2.35k stars 354 forks source link

About JWT authentication and authorization #1069

Closed sdack-cloud closed 1 year ago

sdack-cloud commented 2 years ago

环境: server:id 'org.springframework.boot' version '2.7.2' implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server' implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.security:spring-security-rsocket' implementation 'org.springframework.security:spring-security-messaging' implementation 'org.springframework.boot:spring-boot-starter-webflux' implementation 'org.springframework.boot:spring-boot-starter-rsocket'

client: implementation 'io.rsocket:rsocket-core:1.1.2' implementation 'io.rsocket:rsocket-transport-netty:1.1.2'

我现在遇到的情况

    @ShellMethod
    fun conn() {
        val tokenMetadata = BearerTokenMetadata(token)
        val uri = URI.create("ws://localhost:7002")
        rSocketRequester = RSocketRequester.builder()
            .setupMetadata(tokenMetadata, SIMPLE_AUTH)
            .setupData("android_1234,333")
            .rsocketStrategies {
                it.encoder(BearerTokenAuthenticationEncoder(),ProtobufEncoder())
                    .decoder( ProtobufDecoder())
            }
            .websocket(uri)
    }

WX20220910-173820@2x


    @ShellMethod
    fun getuser() {
        val tokenMetadata = BearerTokenMetadata(token)
        val newBuilder = UsersPro.Users.newBuilder()
        newBuilder.setAccount("123abc")
        val build = newBuilder.build()
        rSocketRequester!!
            .route("user.get")
            .metadata(tokenMetadata,SIMPLE_AUTH)
            .data(build.toByteArray())
            .retrieveMono(ByteArray::class.java)
            .doOnSuccess {
                println("ok")

            }
            .doOnError {
                it.printStackTrace()
            }
            .subscribe()

    }
   fun connect() {
        val uri = URI.create("ws://localhost:7002")
        val transport = WebsocketClientTransport.create(uri)
        val metadata = ByteBufAllocator.DEFAULT.compositeBuffer()
        val bearerMetadata2 = AuthMetadataCodec.encodeMetadata(
            ByteBufAllocator.DEFAULT,
            WellKnownAuthType.BEARER,
            Unpooled.copiedBuffer(token, CharsetUtil.UTF_8)
        )
        CompositeMetadataCodec.encodeAndAddMetadata(
            metadata, ByteBufAllocator.DEFAULT,
            WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.string,
            bearerMetadata2
        )
        rSocket = RSocketConnector.create()
            .metadataMimeType(WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.string)
            .dataMimeType(WellKnownMimeType.APPLICATION_CBOR.string)
            .setupPayload(DefaultPayload.create("1234,321".toByteArray(),ByteBufUtil.getBytes(metadata)))
            .keepAlive(Duration.ofSeconds(30), Duration.ofMinutes(30))
            .reconnect(Retry.fixedDelay(3, Duration.ofSeconds(5)).doBeforeRetry {
                log.error("Retry {}", "" + it.totalRetries())
            })
            .connect(transport)
}

2022-09-11 15:40:03.991 ERROR 56777 --- [actor-tcp-nio-2] Routing : Error Destination '' does not support REQUEST_RESPONSE. Supported interaction(s): [SETUP, METADATA_PUSH] Destination '' does not support REQUEST_RESPONSE. Supported interaction(s): [SETUP, METADATA_PUSH] Details of the error have been omitted. You can use the stacktrace command to print the full stacktrace.

WX20220911-152950@2x


 val metadata = ByteBufAllocator.DEFAULT.compositeBuffer()
        val routingMetadata = TaggingMetadataCodec.createRoutingMetadata(ByteBufAllocator.DEFAULT, listOf("user.get"))
        CompositeMetadataCodec.encodeAndAddMetadata(
            metadata, ByteBufAllocator.DEFAULT,
            WellKnownMimeType.MESSAGE_RSOCKET_ROUTING,
            routingMetadata.content
        )
        val bearerMetadata2 = AuthMetadataCodec.encodeMetadata(
            ByteBufAllocator.DEFAULT,
            WellKnownAuthType.BEARER,
            Unpooled.copiedBuffer(token, CharsetUtil.UTF_8)
        )
        CompositeMetadataCodec.encodeAndAddMetadata(
            metadata, ByteBufAllocator.DEFAULT,
            WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.string,
            bearerMetadata2
        )

        val metaBuilder = MetaPro.Meta.newBuilder()
        metaBuilder.setAppId(appId).setV(1.0).setPage(0).setPageSize(10)

        val newBuilder = UsersPro.Users.newBuilder()
        newBuilder.setId(id.toLong())
        val build = newBuilder.build()

        val res = rSocket!!
            .requestResponse(DefaultPayload.create(build.toByteArray(), ByteBufUtil.getBytes(metadata)))
            .doOnError {
                log.error("Error {}",it.message)
            }
            .block()

Can you give some examples? No answer found in AuthMetadataCodecTest

  val block = rSocket!!.metadataPush(ByteBufPayload.create("123".toByteArray(), ByteBufUtil.getBytes(metadata))).block()

ERROR 67093 --- [ctor-http-nio-7] o.s.m.h.i.reactive.InvocableHelper : No exception handling method

org.springframework.messaging.handler.invocation.MethodArgumentResolutionException: Could not resolve method parameter at index 1 in public void RSocketRouting.connect(org.springframework.messaging.rsocket.RSocketRequester,java.lang.String): Payload content is missing: public void RSocketRouting.connect(org.springframework.messaging.rsocket.RSocketRequester,java.lang.String) at org.springframework.messaging.handler.annotation.reactive.PayloadMethodArgumentResolver.handleMissingBody(PayloadMethodArgumentResolver.java:281)

sdack-cloud commented 1 year ago

REQUEST_RESPONSE. Supported interaction(s): [SETUP, METADATA_PUSH] Destination '' does not support REQUEST_RESPONSE. Supported interaction(s): [SETUP, METADATA_PUSH]

The problem seems to be that the route does not map the path accurately Even if the security is removed

@MessageMapping("abc")

rsocket-js This problem also occurs v: "rsocket-composite-metadata": "^1.0.0-alpha.3", "rsocket-messaging": "^1.0.0-alpha.3", "rsocket-websocket-client": "^1.0.0-alpha.1",