2

当我从 UI 调用时,我有@PreAuthorize("hasRole('ADMIN')")检查方法(这将需要大约 2 到 3 个小时,同时会话即将过期)我正在获取SecurityContextas 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 对象

4

0 回答 0