Fix async Rest5 client withHeaders() not seeing ThreadLocal auth (#3300)#3305
Open
014-code wants to merge 1 commit into
Open
Fix async Rest5 client withHeaders() not seeing ThreadLocal auth (#3300)#3305014-code wants to merge 1 commit into
014-code wants to merge 1 commit into
Conversation
…ing-projects#3300) Rest5Clients registered the ClientConfiguration.withHeaders() supplier as an HttpRequestInterceptor on the Apache HttpAsyncClientBuilder. Since HC 5 runs request interceptors on the I/O reactor thread, the supplier executes off the calling thread and cannot see a ThreadLocal (e.g. Spring Security's SecurityContextHolder). With basic auth or tokens held in a ThreadLocal, requests are sent without the expected Authorization header. Switch the registration to addExecInterceptorFirst so the supplier runs on the AsyncExecChain, which executes in the calling thread before the request is dispatched asynchronously. Also fix a pre-existing typo: " Content-Type" had a leading space so the override never matched the real Content-Type header. Closes spring-projects#3300 Signed-off-by: 014-code <2402143478@qq.com>
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.
Summary
ClientConfiguration.withHeaders()configured with aSupplier<HttpHeaders>that reads aThreadLocal(typical for Spring Security'sSecurityContextHolder) silently fails on the new Rest5 async client.Root cause
Rest5Clients.getRest5ClientBuilder(...)registered the headers supplier as anHttpRequestInterceptoron theHttpAsyncClientBuilder. In HC 5, request interceptors run on the I/O reactor thread, so the supplier executes off the calling thread and cannot see anyThreadLocalset by the caller.The legacy
RestClients(HC 4) usedaddInterceptorLast, which runs synchronously on the calling thread, so the bug does not manifest there.Fix
Replace
addRequestInterceptorFirstwithaddExecInterceptorFirst. TheAsyncExecChainhandler executes on the calling thread before the request is dispatched asynchronously, so the supplier now sees the caller'sThreadLocal.While there, fix a pre-existing typo:
" Content-Type"had a leading space so the override never matched the realContent-Typeheader.Regression test
RestClientsTest.shouldConfigureClientAndSetAllRequiredHeadersnow:ThreadLocalinside the headers supplier and exposes the value as athreadheader.ThreadLocalon the calling thread before invokingclient.ping().thread: localheader.The test runs against all three client factories (
ElasticsearchRestClient/ElasticsearchRest5Client/ReactiveElasticsearchClient (ELC based)). Only the Rest5 factory exercises the changed code; the others serve as a regression net.#3300