Home > Back-end >  How to add Custom Token Granter to the new Spring Authorization Server
How to add Custom Token Granter to the new Spring Authorization Server

Time:10-24

Hello I am currently using and old Authorization Server with th end of life dependency spring-security-oauth2-autoconfigure and now i would like to migrate to the new Spring Authorization Server

My questions is how can i intercept/override the default Token Granter of the new Spring Authorization Service. In the old version i just extended the AbstractTokenGranter SsoTokenGranter extends AbstractTokenGranter.

I would like to call other services during the token generation and add custom claims/authorities to the JWT Token with user information(Roles, Name, etc..).

Any tipps how i can do this?

CodePudding user response:

I think an OAuth2TokenCustomizer can fit nicely in your use case.

@Bean
public OAuth2TokenCustomizer<JwtEncodingContext> tokenCustomizer(
            OidcUserInfoService userInfoService) {
        return (context) -> {
            if (OidcParameterNames.ID_TOKEN.equals(context.getTokenType().getValue())) {
                OidcUserInfo userInfo = userInfoService.loadUser( // <2>
                        context.getPrincipal().getName());
                context.getClaims().claims(claims ->
                        claims.putAll(userInfo.getClaims()));
            }
        };
}

There is a section on the reference docs and a sample that you can use as reference.

  • Related