Spring Security @Preauthorize和@Async

时间:2017-04-11 14:03:58

标签: spring-boot spring-security spring-security-ldap spring-async

当我从用户界面调用时,我正在使用@PreAuthorize("hasRole('ADMIN')")检查方法(会话过期需要大约2到3个小时)我正在将SecurityContext设为null }。

尝试使用MODE_INHERITABLETHREADLOCAL,但我仍然遇到同样的错误。

我还尝试了以下代码:

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();
                }
            }
        });
    }
}   

但我仍然遇到同样的错误:

  

org.springframework.security.authentication.AuthenticationCredentialsNotFoundException:在SecurityContext中找不到Authentication对象

0 个答案:

没有答案