@@ -33,20 +33,27 @@ public AbstractAuthenticationToken convert(Jwt source) {
3333 private Collection <? extends GrantedAuthority > extractRoles (Jwt jwt ) {
3434 Set <String > roles = new HashSet <>();
3535
36+ // Extract roles from realm_access (if available)
3637 Map <String , Object > realmAccess = jwt .getClaimAsMap ("realm_access" );
37- if (realmAccess != null && realmAccess .containsKey ("roles" )) {
38+ if (realmAccess != null && realmAccess .containsKey ("roles" )) {
3839 roles .addAll ((Collection <? extends String >) realmAccess .get ("roles" ));
3940 }
4041
41- Map <String , Object > resourceAccess = jwt .getClaim ("resource_access" );
42- if (resourceAccess != null && resourceAccess .containsKey ("demo" )) {
43- Map <String , Object > demoAccess = (Map <String , Object >) resourceAccess .get ("roles" );
44- if (demoAccess != null && demoAccess .containsKey ("roles" )) {
45- roles .addAll ((Collection <? extends String >) demoAccess .get ("roles" ));
42+ // Extract roles from resource_access dynamically
43+ Map <String , Object > resourceAccess = jwt .getClaimAsMap ("resource_access" );
44+ if (resourceAccess != null ) {
45+ for (Map .Entry <String , Object > entry : resourceAccess .entrySet ()) {
46+ Map <String , Object > resource = (Map <String , Object >) entry .getValue ();
47+ if (resource .containsKey ("roles" )) {
48+ roles .addAll ((Collection <? extends String >) resource .get ("roles" ));
49+ }
4650 }
47-
4851 }
49- System .out .println (roles );
50- return roles .stream ().map (role -> new SimpleGrantedAuthority ("ROLE_" + role .toUpperCase ())).collect (Collectors .toSet ());
52+
53+ // Convert to Spring Security GrantedAuthorities with "ROLE_" prefix
54+ return roles .stream ()
55+ .map (role -> new SimpleGrantedAuthority ("ROLE_" + role .toUpperCase ()))
56+ .collect (Collectors .toSet ());
5157 }
58+
5259}
0 commit comments