I am using the Vertx framework on my API for authorization with JWT tokens. After the user is authorized and the token is decrypted I want to access the contents of the token, particularly a "userId" field within the token.
Originally I was accessing this with req
as the RoutingContext
as:
req.user().principal().getString("userId");
This works as intended until it is compiled on a different machine. When compiled on a different machine req.user().principal()
only contains the field access_token
, containing the still encrypted JWT.
The solution to this was to access the user ID through
req.user().attributes().getJsonObject("accessToken").getString("userId");
I've tested this on 4 different machines. 2 of them work using principal, and the other 2 require attributes. It seems to only matter which machine it is compiled on, not what it is run on. The code is not being changed between machines. Java, Maven, and Vertx versions are the same each time.
Some solutions I've found online simply check if the needed field is in principal, and if not to use attributes instead. This seems like a bad workaround though. There has to be a proper way to access it.
What is the proper way to access the values within the decoded token? And why does it seem to change depending on the compiling machine?