我开发了一个用于将文件导入数据库的API。我创建了一种使用批注@Async导入文件的方法
在这种方法下,我无法恢复用户的安全上下文
我的方法的代码:
@Async("taskExecutor")
@Transactional
public void run(ImportFichierDTO importFichierDTO,
TypeImport typeImport,
String description,
InputStream inputStream,
Long idEntite){
我有此配置类'AsyncConfiguration':
@Override
@Bean(name = "taskExecutor")
public Executor getAsyncExecutor() {
log.debug("Creating Async Task Executor");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(jHipsterProperties.getAsync().getCorePoolSize());
executor.setMaxPoolSize(jHipsterProperties.getAsync().getMaxPoolSize());
executor.setQueueCapacity(jHipsterProperties.getAsync().getQueueCapacity());
executor.setThreadNamePrefix("yvidya-Executor-");
return new ExceptionHandlingAsyncTaskExecutor(executor);
}
在项目的主类中,我添加了:
@ComponentScan
@EnableAutoConfiguration(exclude = {MetricFilterAutoConfiguration.class, MetricRepositoryAutoConfiguration.class})
@EnableConfigurationProperties({LiquibaseProperties.class, ApplicationProperties.class})
@EnableAsync()
public class ProjectApp {
然后我在SecurityConfiguration中添加了MODE_INHERITABLETHREADLOCAL:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private final AuthenticationManagerBuilder authenticationManagerBuilder;
private final UserDetailsService userDetailsService;
public SecurityConfiguration(AuthenticationManagerBuilder authenticationManagerBuilder, UserDetailsService userDetailsService) {
this.authenticationManagerBuilder = authenticationManagerBuilder;
this.userDetailsService = userDetailsService;
// nécessaire pour avoir accès aux données du contexte dans les services
SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);
}
您有一个解决我的丢失安全上下文问题的想法