# Assistance with Custom JWT Converter in Spring Security for Auth0

**URL:** <https://community.auth0.com/t/assistance-with-custom-jwt-converter-in-spring-security-for-auth0/123735>\
**Category:** Get Help\
**Tags:** other, community-topic\
**Created:** [December 27, 2023, 6:39am UTC](https://community.auth0.com/t/assistance-with-custom-jwt-converter-in-spring-security-for-auth0/123735 "2023-12-27T06:39:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![it19](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@it19](https://community.auth0.com/u/it19)\
**Post date:** [December 27, 2023, 6:39am UTC](https://community.auth0.com/t/assistance-with-custom-jwt-converter-in-spring-security-for-auth0/123735/1 "2023-12-27T06:39:23Z")

</div>

I am currently working on integrating Auth0 with a Spring Security application and have encountered some challenges with customizing JWT conversion. I have attempted to introduce a custom `Converter<Jwt, AbstractAuthenticationToken>` in my Spring Security configuration. However, despite ensuring that it is correctly injected, it never gets invoked — the default `JwtGrantedAuthoritiesConverter` seems to be used instead.

Here is a brief overview of what I’ve tried:

- Created a custom converter implementation and injected it into the `HttpSecurity` OAuth2 resource server configuration.
- Verified that the bean is correctly configured and no other beans are conflicting.
- Enabled debugging to confirm that the custom converter is not being called, with the default one being used each time.

Given this situation, I am considering an alternative approach where I let the default converter process the JWT and then use a custom filter that runs after `BearerTokenAuthenticationFilter` to extract additional details from the JWT and store them as needed.

My questions to the community are:

1. Is this approach acceptable from an Auth0 integration standpoint?
2. Does it comply with best practices for JWT token handling in conjunction with Auth0?
3. Is there a better or more standard way to achieve this functionality that I might have overlooked?

I would greatly appreciate any insights, recommendations, or shared experiences that could guide me toward the best implementation strategy.

Thank you in advance for your support!

---

<div class="post-metadata">

**Author:** ![mraible](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mraible/32/32776_2.png) [@mraible](https://community.auth0.com/u/mraible)\
**Post date:** [January 2, 2024, 6:42pm UTC](https://community.auth0.com/t/assistance-with-custom-jwt-converter-in-spring-security-for-auth0/123735/2 "2024-01-02T18:42:33Z")

</div>

Hi @it19 and welcome to the Auth0 Community!

One of my examples has the following for configuring a custom `Converter<Jwt, AbstractAuthenticationToken>` in its `SecurityConfiguration.java`. This code uses Spring Boot 3.2.0.

```java
@Bean
public SecurityFilterChain filterChain(HttpSecurity http, MvcRequestMatcher.Builder mvc) throws Exception {
    http
        // other configuration
        .oauth2ResourceServer(oauth2 -> oauth2.jwt(jwt -> jwt.jwtAuthenticationConverter(authenticationConverter())))
        .oauth2Client(withDefaults());
    return http.build();
}

Converter<Jwt, AbstractAuthenticationToken> authenticationConverter() {
    JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();
    jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(new JwtGrantedAuthorityConverter());
    jwtAuthenticationConverter.setPrincipalClaimName(PREFERRED_USERNAME);
    return jwtAuthenticationConverter;
}

```

The `JwtGrantedAuthorityConverter` is as follows:

```java
@Component
public class JwtGrantedAuthorityConverter implements Converter<Jwt, Collection<GrantedAuthority>> {

    public JwtGrantedAuthorityConverter() {
        // Bean extracting authority.
    }

    @Override
    public Collection<GrantedAuthority> convert(Jwt jwt) {
        return SecurityUtils.extractAuthorityFromClaims(jwt.getClaims());
    }
}

```

FWIW, this is from a [JHipster](https://www.jhipster.tech)-generated app.

Also, if you’re trying to convert roles to granted authorities or do audience validation, you can use the [Okta Spring Boot starter](https://github.com/okta/okta-spring-boot) to do that for you. See the following blog post for more information.

> **[Get started with Spring Boot and Auth0](https://auth0.com/blog/get-started-with-okta-spring-boot-starter/)**
>
> Learn how to add Auth0 to your Spring Boot application using the Okta Spring Boot Starter.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [November 26, 2024, 11:58pm UTC](https://community.auth0.com/t/assistance-with-custom-jwt-converter-in-spring-security-for-auth0/123735/3 "2024-11-26T23:58:39Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
