From 9ef57e302749308afe709f68c45b58c17d73ca7b Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Sat, 28 Nov 2020 21:56:57 +0100 Subject: [PATCH] BAEL-3630 Improve article "Spring Security Context Propagation with @Async" (#10284) * BAEL-3630 Improve article "Spring Security Context Propagation with @Async" * BAEL-3630 Improve article "Spring Security Context Propagation with @Async" Co-authored-by: Krzysztof Majewski --- .../baeldung/security/SecurityJavaConfig.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/spring-security-modules/spring-security-web-rest/src/main/java/com/baeldung/security/SecurityJavaConfig.java b/spring-security-modules/spring-security-web-rest/src/main/java/com/baeldung/security/SecurityJavaConfig.java index d20198c2bb..7f0b20ea34 100644 --- a/spring-security-modules/spring-security-web-rest/src/main/java/com/baeldung/security/SecurityJavaConfig.java +++ b/spring-security-modules/spring-security-web-rest/src/main/java/com/baeldung/security/SecurityJavaConfig.java @@ -7,14 +7,15 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.security.task.DelegatingSecurityContextAsyncTaskExecutor; import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler; @Configuration @@ -34,11 +35,6 @@ public class SecurityJavaConfig extends WebSecurityConfigurerAdapter { private SimpleUrlAuthenticationFailureHandler myFailureHandler = new SimpleUrlAuthenticationFailureHandler(); - public SecurityJavaConfig() { - super(); - SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL); - } - @Override protected void configure(final AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() @@ -77,4 +73,18 @@ public class SecurityJavaConfig extends WebSecurityConfigurerAdapter { return new BCryptPasswordEncoder(); } + @Bean + public ThreadPoolTaskExecutor threadPoolTaskExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(10); + executor.setMaxPoolSize(100); + executor.setQueueCapacity(50); + executor.setThreadNamePrefix("async-"); + return executor; + } + + @Bean + public DelegatingSecurityContextAsyncTaskExecutor taskExecutor(ThreadPoolTaskExecutor delegate) { + return new DelegatingSecurityContextAsyncTaskExecutor(delegate); + } } \ No newline at end of file