package com.ruoyi.common.config; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.AsyncConfigurer; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.security.task.DelegatingSecurityContextAsyncTaskExecutor; import org.springframework.core.task.SimpleAsyncTaskExecutor; import java.util.concurrent.Executor; @Configuration @EnableAsync // 启用异步支持 public class AsyncConfig implements AsyncConfigurer { @Override public Executor getAsyncExecutor() { // 1. 创建一个基础的异步任务执行器 SimpleAsyncTaskExecutor delegate = new SimpleAsyncTaskExecutor(); // 2. 使用DelegatingSecurityContextAsyncTaskExecutor进行包装 // 它会自动将主线程的安全上下文绑定到异步任务的线程上 return new DelegatingSecurityContextAsyncTaskExecutor(delegate); } }