Skip to content
Advertisement

Extend Micronaut CustomJWTClaimsSetGenerator to provide all attributes

I have the following two classes which provide the JWT authentication mechanisem.

CustomDelegatingAuthenticationProvider

JavaScript

CustomJWTClaimsSetGenerator

JavaScript

The default response to the client looks like this:

enter image description here

My question. How can I extend the class to return all user attributes? Besides username I want to have the user id.

UPDATE

HDS user class which gathers the DB id

JavaScript

Advertisement

Answer

To extend the returned data you need to extend (implement custom) TokenRenderer as well as a custom version of the AccessRefreshToken. Just as a simple example see the following code snipped which will extend the default access token payload with userId field.

First, create a custom AccessRefreshToken class with additional fields which are required.

JavaScript

Next, we will need a TokenRenderer which will be used by the underlying subsystem to generate our custom token.

JavaScript

That’s it )) Just implement render() method the way you want and add as many custom fields as needed.

The response from the given example will look like

JavaScript

If you are on the older version of the Micronaut v1.x the TokenRenderer will look like this.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement