2

I am having the @PreAuthorize("hasRole('ADMIN')") check methods when I am calling from the UI (It will take around 2 to 3 hours meanwhile session is getting expired) I am geetting the SecurityContext as null.

Tried with MODE_INHERITABLETHREADLOCAL, but I'm still getting the same error.

Also I tried the following code:

public class CustomThreadPoolTaskExecutor extends ThreadPoolTaskExecutor{
    @Override
    public void execute(final Runnable r) {
        final Authentication a = SecurityContextHolder.getContext().getAuthentication();

        super.execute(new Runnable() {
            public void run() {
                try {
                    SecurityContext ctx = SecurityContextHolder.createEmptyContext();
                    ctx.setAuthentication(a);
                    SecurityContextHolder.setContext(ctx);
                    r.run();
                } finally {
                    SecurityContextHolder.clearContext();
                }
            }
        });
    }
}   

but I'm still getting the same error:

org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext

dur
  • 14,335
  • 22
  • 74
  • 113
Dileep
  • 21
  • 3
  • 2
    Did you try [`org.springframework.security.task.DelegatingSecurityContextAsyncTaskExecutor`](http://docs.spring.io/autorepo/docs/spring-security/4.0.0.M1/apidocs/org/springframework/security/task/DelegatingSecurityContextAsyncTaskExecutor.html)? – dur Apr 11 '17 at 14:37
  • Yeah tried with the **DelegatingSecurityContextAsyncTaskExecutor** getting the same error. – Dileep Apr 12 '17 at 03:56
  • Tried with the code provided in the link http://stackoverflow.com/questions/37214386/get-user-that-runs-an-asynchronous-method – Dileep Apr 12 '17 at 04:12

0 Answers0