Add DelegatingSecurityContextThreadFactory#19220
Open
klouds27 wants to merge 1 commit into
Open
Conversation
Adds DelegatingSecurityContextThreadFactory to propagate the SecurityContext to threads created via ThreadFactory, following the same pattern as DelegatingSecurityContextExecutor. This enables SecurityContext propagation for APIs that accept a ThreadFactory directly, such as structured concurrency (JEP 533). Closes spring-projectsgh-19075 Signed-off-by: klouds27 <adalwolf@gmail.com>
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds a new ThreadFactory implementation that propagates Spring Security SecurityContext into created threads, along with unit tests to validate behavior for platform threads, virtual threads, and explicit/current contexts.
Changes:
- Introduce
DelegatingSecurityContextThreadFactorythat wrapsRunnables with security-context propagation. - Add JUnit tests covering constructor validation and context propagation scenarios (platform/virtual threads, explicit/current contexts).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| core/src/main/java/org/springframework/security/concurrent/DelegatingSecurityContextThreadFactory.java | Adds a delegating ThreadFactory that wraps tasks with DelegatingSecurityContextRunnable via existing support utilities. |
| core/src/test/java/org/springframework/security/concurrent/DelegatingSecurityContextThreadFactoryTests.java | Adds coverage for propagation behavior and constructor validation, including a virtual-thread scenario. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+95
to
+105
| private SecurityContext runAndCapture(DelegatingSecurityContextThreadFactory factory) throws InterruptedException { | ||
| CountDownLatch latch = new CountDownLatch(1); | ||
| AtomicReference<SecurityContext> result = new AtomicReference<>(); | ||
| Thread thread = factory.newThread(() -> { | ||
| result.set(SecurityContextHolder.getContext()); | ||
| latch.countDown(); | ||
| }); | ||
| thread.start(); | ||
| latch.await(); | ||
| return result.get(); | ||
| } |
Comment on lines
+59
to
+64
| @Test | ||
| @DisabledOnJre(JRE.JAVA_17) | ||
| public void newThreadWhenVirtualThreadThenSecurityContextPropagated() throws Exception { | ||
| SecurityContext context = propagateAndCapture(new VirtualThreadTaskExecutor().getVirtualThreadFactory()); | ||
| assertThat(context.getAuthentication()).isNotNull(); | ||
| } |
| } | ||
|
|
||
| @Override | ||
| public Thread newThread(Runnable r) { |
Comment on lines
+56
to
+63
| assertThat(context.getAuthentication()).isNotNull(); | ||
| } | ||
|
|
||
| @Test | ||
| @DisabledOnJre(JRE.JAVA_17) | ||
| public void newThreadWhenVirtualThreadThenSecurityContextPropagated() throws Exception { | ||
| SecurityContext context = propagateAndCapture(new VirtualThreadTaskExecutor().getVirtualThreadFactory()); | ||
| assertThat(context.getAuthentication()).isNotNull(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
There is no
ThreadFactoryequivalent in theDelegatingSecurityContext*family, making it impossible to propagate theSecurityContextto libraries that accept aThreadFactoryrather than anExecutor. The primary motivation is structured concurrency: the proposedStructuredTaskScopeAPI (JEP 533) accepts aThreadFactoryto create subtask threads, so without this class there is no supported way to propagate the security context to those threads.Fix
Adds
DelegatingSecurityContextThreadFactory, which wraps eachRunnablepassed tonewThread()in aDelegatingSecurityContextRunnablebefore delegating to the underlyingThreadFactory. It follows the same design asDelegatingSecurityContextExecutor:SecurityContextor capture-at-call-time (null)setSecurityContextHolderStrategyfor custom holder strategiesAbstractDelegatingSecurityContextSupportto reuse wrapping logicTests
newThreadWhenPlatformThreadThenSecurityContextPropagated— context propagated via platform threadnewThreadWhenVirtualThreadThenSecurityContextPropagated— context propagated via virtual thread (disabled on JRE 17)newThreadWhenExplicitSecurityContextThenUsesThatContext— explicit context is usednewThreadWhenCurrentContextThenPropagatesCallerContext— null constructor captures caller contextconstructorWhenNullDelegateThenException— null delegate rejectedCloses gh-19075