I have a method secured with @PreAuthorize
@PreAuthorize("hasRole('ROLE_ADMIN') and (#action.userId != principal.id)")
public void execute(EditAction action)
Now I need to call this method from a background task. If I simply run this code - I catch an exception:
AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext
Seems, I need to set required Authentication to SecurityContext. I can:
- Write some custom AuthenticationToken for background tasks.
- Use UsernamePasswordAuthenticationToken with fake User.
- Don't use secured methods in background tasks.
- Any other recommendations ?
What is the right way?