cuando intento crear una nueva cita me aparece este error, Error en el filtro: Request processing failed: org.springframework.dao.InvalidDataAccessApiUsageException: The given id must not be null, pongo en codigo del filtro la entidad usuario y el repositorio package med.voll.api.models;
import jakarta.persistence.; import lombok.; import med.voll.api.dto.UsuarioRegistroDto; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection; import java.util.List;
@Entity(name = "usuarios") @Table(name = "usuario") @NoArgsConstructor @AllArgsConstructor @EqualsAndHashCode(of = "id") @Getter @Setter public class Usuario implements UserDetails { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; private String clave;
public Usuario(UsuarioRegistroDto usuarioRegistroDto) {
this.username = usuarioRegistroDto.username();
this.clave = usuarioRegistroDto.clave();
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of(new SimpleGrantedAuthority("ROLE_USER"));
}
@Override
public String getPassword() {
return this.clave;
}
@Override
public boolean isAccountNonExpired() {
return UserDetails.super.isAccountNonExpired();
}
@Override
public boolean isAccountNonLocked() {
return UserDetails.super.isAccountNonLocked();
}
@Override
public boolean isCredentialsNonExpired() {
return UserDetails.super.isCredentialsNonExpired();
}
@Override
public boolean isEnabled() {
return UserDetails.super.isEnabled();
}
}
package med.voll.api.repository;
import med.voll.api.models.Usuario; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.security.core.userdetails.UserDetails;
public interface IUsuarioReposiroty extends JpaRepository<Usuario, Long> {
UserDetails findByUsername(String username);
}
package med.voll.api.filters;
import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import med.voll.api.auth.TokenService; import med.voll.api.repository.IUsuarioReposiroty; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component; import org.springframework.web.filter.OncePerRequestFilter;
import java.io.IOException;
@Component @RequiredArgsConstructor public class FilterSecurity extends OncePerRequestFilter {
private final TokenService tokenService;
private final IUsuarioReposiroty usuarioReposiroty;
@Override
protected void doFilterInternal(
HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
var auth = request.getHeader("Authorization");
try {
if (auth != null) {
var token = auth.replace("Bearer ", "");
var subject = this.tokenService.getSunject(token);
if (subject != null) {
var usuario = this.usuarioReposiroty.findByUsername(subject);
System.out.println("usuario = " + usuario.toString());
if (usuario != null) {
var authUser = new UsernamePasswordAuthenticationToken(usuario, null,
usuario.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authUser);
} else {
System.out.println("Usuario no encontrado");
}
} else {
System.out.println("Token inválido");
}
}
filterChain.doFilter(request, response);
} catch (Exception e) {
System.out.println("Error en el filtro: " + e.getMessage());
}
}
}