I have multiple entities where i used @createdBy, @LastModifiedBy etc or spring data. Now this is saving the username column of user. I need to change it to like firstname.lastname.
I found there is one method using implements AuditorAware {
Attaching the code
@Configuration
public class AuditorAwareImpl implements AuditorAware {
@Override
public Optional<String> getCurrentAuditor() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || !authentication.isAuthenticated()) {
return Optional.empty();
}
User user = (User) authentication.getPrincipal();
String auditorName = user.getFirstName().concat(".").concat(user.getUsername());
return Optional.of(auditorName);
}
}
What i expected is during save the call will reach here to get the value for @createdBy or @LastModifiedBy. But it will reach here only in case of spring data save is what i got from other forums.
Did you have any solution for this
Should we add something in some other places. like some annotaions like @EnableJpaAuditing or something else
Can we use this wihout jpa dependency
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
Thankyou