|
|
@@ -3,8 +3,11 @@ package com.njuzr.eaibackend.config;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.core.task.TaskDecorator;
|
|
|
import org.springframework.scheduling.annotation.EnableAsync;
|
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
+import org.springframework.security.core.context.SecurityContext;
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
|
@@ -47,8 +50,22 @@ public class ThreadPoolConfig {
|
|
|
executor.setMaxPoolSize(50);
|
|
|
executor.setQueueCapacity(100);
|
|
|
executor.setThreadNamePrefix("ai-eval-");
|
|
|
- executor.initialize();
|
|
|
+ executor.setTaskDecorator(runnable -> {
|
|
|
+ var auth = SecurityContextHolder.getContext().getAuthentication();
|
|
|
+ var context = SecurityContextHolder.createEmptyContext();
|
|
|
+ context.setAuthentication(auth);
|
|
|
+
|
|
|
+ return () -> {
|
|
|
+ try {
|
|
|
+ SecurityContextHolder.setContext(context);
|
|
|
+ runnable.run();
|
|
|
+ } finally {
|
|
|
+ SecurityContextHolder.clearContext();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ });
|
|
|
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
|
|
+ executor.initialize();
|
|
|
return executor;
|
|
|
}
|
|
|
|