How to configure the Auditor using Spring Boot 2.0.2

Asked

Viewed 163 times

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/

1 answer

1


I will consider your old implementation, which returns String, not a user entity, for example. Also, I won’t consider anything that leads me to believe that you are using custom things for user details, security Holder/manager, etc..

In this case, the only change is that now one is returned Optional - AuditorAware 1.x, AuditorAware current. The implementation will be basically the same, only the altered return, something like the below:

public class SpringSecurityAuditor implements AuditorAware<String> {

    @Override
    public Optional<String> getCurrentAuditor() {
        String currentAuditor;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null || authentication.getPrincipal().equals("anonymousUser")) {
            currentAuditor = authentication.getPrincipal().toString();
        } else {
            currentAuditor = ((UsuarioLogado) authentication.getPrincipal()).getUsername()
        }
        return Optional.of(currentAuditor);
    }

}

It is worth noting that this is not necessarily related to Spring Boot, to interface comes from Spring Data, in the case of Commons. Directly related to Boot, the configuration was simplified, but there is another issue.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.