0

Per other answers here my code below should be correct, but I am getting nothing from SecurityUtils. I need to assign a user to a new record in an application. Am I missing something here?

This other response also returns a null user.

User user = userRepository.findOneByLogin(SecurityUtils.getCurrentUserLogin().get()).get();
entity.setUser(user);

entityRepository.save(entity);
Ned
  • 237
  • 3
  • 12
  • Is it an asynchronous processing? SecurityContextHolder uses ThreadLocal so if your code is executed in another thread, you must propagate security context. – Gaël Marziou Jan 11 '19 at 13:17
  • Gaël, I will be honest. I am not sure. How would I go about propagating the security context if that were the case? – Ned Jan 11 '19 at 13:21
  • Well you should easily say if your processing is annotated with `@Async` or using an executor service. Another thing you can do is to run under debugger and stop at different levels to see if you have access to SecurityContext. Propagating can be done in different ways, simplest way would be to extract login in resource controller and passing it as a parameter to service layer. – Gaël Marziou Jan 11 '19 at 13:27

0 Answers0