0

when i use

@Inject
JsonWebToken jwt;

in function that returning Uni, it always show this error

Cannot call getIdentity() from the IO thread when lazy authentication is in use, as resolving the identity may block the thread. Instead you should inject the CurrentIdentityAssociation, call CurrentIdentityAssociation#getDeferredIdentity() and subscribe to the Uni.

is there any example how to use JWT in quarkus resteasy reactive ?

the error goes away if i use @Blocking in the function. thanks.

1 Answers1

0

I think there is 2 possibilities here.

If you have disabled proactive authentication, you could enable it back to get non blocking access to your token. See quarkus proactive auth docs, it says :

Note that if proactive authentication is in use accessing the SecurityIdentity is a blocking operation

BUT, the real solution IMHO would be to do as stacktrace suggest :

// Inject this instead of your JsonWebToken
@Inject
CurrentIdentityAssociation identities;

...

// And retrieve your token this way
Uni<JsonWebToken> getJwt() { 
    return identities.getDeferredIdentity()
                     .onItem()
                     .transform(identity -> (JsonWebToken) identity.getPrincipal());
}
Jacouille
  • 951
  • 8
  • 14
  • Hello @jacouille! I get an error from your code because this is an illegal cast `(JsonWebToken) identity.getPrincipal()`. Can you clarify? Thank you! – micha Sep 13 '22 at 09:39
  • Hello @micha ! Please write a new question and link it to me so i can take a look – Jacouille Sep 13 '22 at 09:43
  • Thank you @jacouille! I created a new question here, please have a look! https://stackoverflow.com/questions/73701617/how-to-access-jwt-in-quarkus-when-proactive-auth-is-disabled – micha Sep 13 '22 at 10:45