解决SpringSecurity上下文自动设置

论坛 期权论坛 脚本     
已经匿名di用户   2022-3-21 23:41   1606   0

问题:

我们用用异步线程的时候不能把SpringSecurity当前用户信息context带到子线程。

为了方便,我们需要进行信息透传

解决方法

  1. 需要加个参数 -Dspring.security.strategy=MODE_INHERITABLETHREADLOCAL

  2. 或者增加代码块

  3. static {
                  SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);
        }

  4. 如果使用了线程池,则添加如下配置

    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(50);
        executor.setQueueCapacity(25);
        executor.setThreadNamePrefix("async-");
        return executor;
    }

    @Bean
    public DelegatingSecurityContextAsyncTaskExecutor taskExecutor(ThreadPoolTaskExecutor delegate) {
        return new DelegatingSecurityContextAsyncTaskExecutor(delegate);
    }

为什么要添加线程池

如果用的是线程池的话,子线程每次使用完,以前被赋值的用户context不会被清理掉。而且下次再调用这个子线程的时候也不会重新设置当前父线程的用户上下文。

所以要给异步线程加个DelegatingSecurityContextAsyncTaskExecutor

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP