已集成security的状况
@Component
public class AuditorAwareImpl implements AuditorAware<String> {
@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<String> {
@Override
public String getCurrentAuditor() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
return request.getHeader("X-USER-ID");
}
}