2
Here’s a class I used with spring framework version 4.3.4.RELEASE
(I still didn’t use spring boot) and that worked:
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
public class SpringSecurityAuditor implements AuditorAware<String> {
@Override
public String getCurrentAuditor() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || authentication.getPrincipal().equals("anonymousUser")) {
return authentication.getPrincipal().toString();
}
return ((UsuarioLogado) authentication.getPrincipal()).getUsername();
}
}
and here’s the one I’m trying to implement with spring boot 2.0.2:
import java.util.Optional;
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
public class SpringSecurityAuditor implements AuditorAware<String> {
@Override
public Optional<String> getCurrentAuditor() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || authentication.getPrincipal().equals("anonymousUser")) {
return (Optional<String>) authentication.getPrincipal();
}
return ... código a ser implementado...
}
}
But I can’t do that. How can I implement this method?
See if this helps you: https://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-auditing-part-two/
– StatelessDev