1

How to verify auth before handle body request?

I'm using vertx:

vertxVersion = '3.8.3'
implementation "io.vertx:vertx-core:$rootProject.vertxVersion"
implementation "io.vertx:vertx-web:$rootProject.vertxVersion"
implementation "io.vertx:vertx-lang-kotlin:$rootProject.vertxVersion"
implementation "io.vertx:vertx-lang-kotlin-coroutines:$rootProject.vertxVersion"
implementation "io.vertx:vertx-mongo-client:$rootProject.vertxVersion"
implementation "io.vertx:vertx-auth-mongo:$rootProject.vertxVersion"
implementation "io.vertx:vertx-auth-jwt:$rootProject.vertxVersion"

I want to verify auth before handle body request. But I got error java.lang.IllegalStateException: Request has already been read

Reproduce by use delay on suspend function:

router.handler { context ->
            launch {
                context.request().setExpectMultipart(true)
                delay(100) //This line is sample for a verify auth process
                context.next()
            }
        }
        .handler {context ->
            println("2")
            context.request()
                .handler {
                    b -> println("buff ${b.length()}")
                }
                .endHandler {
                    println("end handle")
                    context.success("ok")
                }
        }.baseHandle(
            fn
        ).failureHandler {
            println("fail: ${it.failure()}")
            it.error()
        }

When run delay(100) (this's sample for a verify process), I got the error above. If I comment delay(100), It's will be working fine.

0

2 Answers 2

1

This happens because by the time you auhenticated the request, the content has kept arriving and has been dropped.

You need to invoke context.request().pause() in you first handler and then context.request().resume() when you're ready.

In most cases though it's easier to let the BodyHandler manage payload for you.

6
  • Thanks @tsegismont. I want to verify auth before handle body request. I will try to use pause and resume for to do it.
    – Sinh Phan
    Commented Jan 18, 2020 at 6:48
  • Did you ever get this to work? I am having the same problem. But it doesn't seem to work for me to pause then resume. I am still seeing the problem. Commented May 30, 2020 at 5:10
  • @NavidMitchell It's still error with me :((. Did you resolve this issue?
    – Sinh Phan
    Commented Aug 11, 2020 at 10:17
  • @PhanSinh no I did not. Looks like something that will need to be a feature request for vertx. Commented Aug 12, 2020 at 23:06
  • @NavidMitchell Hi guy, I did solve it. Let's to see my answer. Hope it can be help you.
    – Sinh Phan
    Commented Aug 14, 2020 at 8:36
0

Finally, I did solve it.

My router is working with the flows:

router.post("/api/upload/file")
      .baseHandle { checkAuthorization(it) }
      .handleFileUpload { updateFileOnItem(it) }

And Following step:

fun checkAuthorization(context: RoutingContext) {
    val request = context.request()
    val tkCookie = request.getCookie("user")
    ...do something to verify user permission
    request.pause()
    context.next()
    context.request().resume()
}

Next:

fun updateFileOnItem(context: RoutingContext) {
    val file = context.fileUploads()
    ...do something
}

It's working with me. Hope it can be help you. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.