SecurityContextHolder.getContext().getAuthentication() always return 'anonymousUser'

I created Spring boot application with the following configuration:

  • Spring boot 2.1.0.RELEASE
  • OpenJdk 11

I have an AuditConfiguration class in my project that looks like:

@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class AuditConfiguration {

    @Bean
    public AuditorAware<String> auditorProvider() {
        return new AuditorAwareImpl();
    }

    class AuditorAwareImpl implements AuditorAware<String> {
        @Override
        public Optional<String> getCurrentAuditor() {
            Principal principal = 
            SecurityContextHolder.getContext().getAuthentication();
            return Optional.of(principal.getName());
        }
    }
}

and SecurityContextHolder.getContext().getAuthentication() always returns anonymousUser.

However, the following code returns the correct user name.

@RestController
@RequestMapping("/history")
public class HistoryEndpoint {

    @RequestMapping(value = "/username", method = RequestMethod.GET)
    @ResponseBody
    public String currentUserName(Principal principal) {
        return principal.getName();
    }
}

I need your help for resolving this issue.