Here is a class I used with the spring framework version 4.3.4.RELEASE
(I still did not use the spring boot) and it 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 what 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 not do this. How can I implement this method?