mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 08:58:48 +00:00
trabajando en guardar presupuestos publicos
This commit is contained in:
@ -0,0 +1,36 @@
|
||||
package com.imprimelibros.erp.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.domain.AuditorAware;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
||||
import com.imprimelibros.erp.users.User;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Configuration
|
||||
@EnableJpaAuditing(auditorAwareRef = "auditorAware")
|
||||
public class JpaAuditConfig {
|
||||
|
||||
@Bean
|
||||
public AuditorAware<User> auditorAware() {
|
||||
return () -> {
|
||||
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (auth == null || !auth.isAuthenticated()) return Optional.empty();
|
||||
|
||||
Object principal = auth.getPrincipal();
|
||||
if (principal instanceof User u) return Optional.of(u);
|
||||
|
||||
if (principal instanceof UserDetails ud) {
|
||||
// Si tu principal es UserDetails y no la entidad User,
|
||||
// aquí podrías cargar User por username si lo necesitas.
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.empty();
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user