spring-data-jpa的auditor设置

已集成security的情况

@Component
public class AuditorAwareImpl implements AuditorAware {

    @Override
    public String getCurrentAuditor() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null || !authentication.isAuthenticated()) {
            return "nobody";
        }
        return authentication.getPrincipal();
    }
}

没有security的情况

@Component
public class AuditorAwareImpl implements AuditorAware {

    @Override
    public String getCurrentAuditor() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getHeader("X-USER-ID");
    }
}

你可能感兴趣的:(springboot)