Class SynchronizedJwt2OAuthenticationConverter<T extends Map<String,Object> & Serializable>

java.lang.Object
com.c4_soft.springaddons.security.oauth2.SynchronizedJwt2OAuthenticationConverter<T>
All Implemented Interfaces:
SynchronizedJwt2AuthenticationConverter<OAuthentication<T>>, org.springframework.core.convert.converter.Converter<org.springframework.security.oauth2.jwt.Jwt,OAuthentication<T>>

public class SynchronizedJwt2OAuthenticationConverter<T extends Map<String,Object> & Serializable> extends Object implements SynchronizedJwt2AuthenticationConverter<OAuthentication<T>>

Turn a JWT into a spring-security Authentication instance.

Sample configuration for Keyclkoak, getting roles from "realm_access" claim:
 @Bean
 public SynchronizedJwt2GrantedAuthoritiesConverter authoritiesConverter() {
        return (var jwt) -> {
                final var roles =
                                Optional
                                                .ofNullable((JSONObject) jwt.getClaims().get("realm_access"))
                                                .flatMap(realmAccess -> Optional.ofNullable((JSONArray) realmAccess.get("roles")))
                                                .orElse(new JSONArray());
                return roles.stream().map(Object::toString).map(role -> new SimpleGrantedAuthority("ROLE_" + role)).collect(Collectors.toSet());
        };
 }

 @Bean
 public SynchronizedJwt2OidcIdAuthenticationConverter authenticationConverter(SynchronizedJwt2GrantedAuthoritiesConverter authoritiesConverter) {
        return new SynchronizedJwt2OidcIdAuthenticationConverter(authoritiesConverter);
 }
 
Author:
Jerome Wacongne ch4mp@c4-soft.com
  • Constructor Details

    • SynchronizedJwt2OAuthenticationConverter

      public SynchronizedJwt2OAuthenticationConverter()
  • Method Details