private Collection<String> getScopes(Jwt jwt) {
        Collection<String> result = new ArrayList<>();
        for (String attributeName : WELL_KNOWN_SCOPE_ATTRIBUTE_NAMES) {
            // Retrieve realm_access entry from access token
            JSONObject realm_access = (JSONObject) jwt.getClaims().get(attributeName);
            if (Objects.isNull(realm_access)) {
                return Collections.emptyList();
            }

            // Retrieve roles from realm_access
            JSONArray roles = (JSONArray) realm_access.get("roles");
            if (Objects.isNull(roles)) {
                return Collections.emptyList();
            }

            for (Object role : roles) {
                result.add((String) role);
            }
        }
        return result;
    }
private Collection < String > getScopes ( Jwt jwt ) { Collection < String > result = new ArrayList < > ( ) ; for ( String attributeName : WELL_KNOWN_SCOPE_ATTRIBUTE_NAMES ) { JSONObject realm_access = ( JSONObject ) jwt . getClaims ( ) . get ( attributeName ) ; if ( Objects . isNull ( realm_access ) ) { return Collections . emptyList ( ) ; } JSONArray roles = ( JSONArray ) realm_access . get ( "roles" ) ; if ( Objects . isNull ( roles ) ) { return Collections . emptyList ( ) ; } for ( Object role : roles ) { result . add ( ( String ) role ) ; } } return result ; }
Gets the realm access roles from a {@link Jwt} Keycloak access token. This can also be done for any kind of
roles/scopes contained in the token. Adapt this method accordingly.

@param jwt The {@link Jwt} access token
@return The realm access roles from the token