|
| 1 | +package dev.acobano.miprimerproyectoconkeycloak.configuracion; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Value; |
| 4 | +import org.springframework.core.convert.converter.Converter; |
| 5 | +import org.springframework.security.authentication.AbstractAuthenticationToken; |
| 6 | +import org.springframework.security.core.GrantedAuthority; |
| 7 | +import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| 8 | +import org.springframework.security.oauth2.jwt.Jwt; |
| 9 | +import org.springframework.security.oauth2.jwt.JwtClaimNames; |
| 10 | +import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; |
| 11 | +import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter; |
| 12 | +import org.springframework.stereotype.Component; |
| 13 | + |
| 14 | +import java.util.Collection; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Objects; |
| 17 | +import java.util.Set; |
| 18 | +import java.util.stream.Stream; |
| 19 | + |
| 20 | +@Component |
| 21 | +public class JwtAutenticacionConversor implements Converter<Jwt, AbstractAuthenticationToken> |
| 22 | +{ |
| 23 | + private final JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = |
| 24 | + new JwtGrantedAuthoritiesConverter(); |
| 25 | + |
| 26 | + @Value("${jwt.auth.converter.principle-attribute}") |
| 27 | + private String atributoNombreUsuario; |
| 28 | + |
| 29 | + @Value("${jwt.auth.converter.resource-id}") |
| 30 | + private String clienteId; |
| 31 | + |
| 32 | + @Override |
| 33 | + public AbstractAuthenticationToken convert(Jwt jwt) |
| 34 | + { |
| 35 | + Collection<GrantedAuthority> permisos = Stream.concat( |
| 36 | + jwtGrantedAuthoritiesConverter.convert(jwt).stream(), |
| 37 | + extraerRoles(jwt).stream() |
| 38 | + ).toList(); |
| 39 | + return new JwtAuthenticationToken(jwt, permisos, getNombreSolicitud(jwt)); |
| 40 | + } |
| 41 | + |
| 42 | + private Collection<? extends GrantedAuthority> extraerRoles(Jwt jwt) |
| 43 | + { |
| 44 | + Map<String, Object> recursosAcceso; |
| 45 | + Map<String, Object> recurso; |
| 46 | + Collection<String> roles; |
| 47 | + |
| 48 | + if(Objects.isNull(jwt.getClaim("resource_access"))) |
| 49 | + return Set.of(); |
| 50 | + else |
| 51 | + { |
| 52 | + recursosAcceso = jwt.getClaim("resource_access"); |
| 53 | + |
| 54 | + if (Objects.isNull(recursosAcceso.get(clienteId))) |
| 55 | + return Set.of(); |
| 56 | + else |
| 57 | + { |
| 58 | + recurso = (Map<String, Object>) recursosAcceso.get(clienteId); |
| 59 | + |
| 60 | + if (Objects.isNull(recurso.get("roles"))) |
| 61 | + return Set.of(); |
| 62 | + else |
| 63 | + { |
| 64 | + roles = (Collection<String>) recurso.get("roles"); |
| 65 | + return roles.stream() |
| 66 | + .map(rol -> new SimpleGrantedAuthority("ROLE_".concat(rol))) |
| 67 | + .toList(); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private String getNombreSolicitud(Jwt jwt) |
| 74 | + { |
| 75 | + String nombreSolicitud = JwtClaimNames.SUB; |
| 76 | + |
| 77 | + if (!Objects.isNull(atributoNombreUsuario)) |
| 78 | + nombreSolicitud = atributoNombreUsuario; |
| 79 | + |
| 80 | + return jwt.getClaim(nombreSolicitud); |
| 81 | + } |
| 82 | +} |
0 commit comments