Simplify use of HttpContext in HttpComponentsClientHttpRequestFactory in support of user associated connections #25066
Labels
in: web
Issues in web modules (web, webmvc, webflux, websocket)
type: enhancement
A general enhancement
Milestone
As described in the Apache HttpClient docs a pooled connection may be associated with a user, for example if there is a client certificate involved such as with mutual TLS. In that case it becomes necessary to select a user token for each request to ensure only pooled connections for that user may be re-used.
There is a
UserTokenHandler
strategy in Apache HttpComponents but in the 4.5.x branch that is only used to associate an already established connection with the local user. So that strategy alone can't be used as a solution (e.g. to use a fixed single identity) and applications have to set the user token on theClientContext
before each request. Currently the only way to do that with theRestTemplate
is by overridingHttpComponentsClientHttpRequestFactory#createHttpContext
.All this is not easy to find, here are some example SO threads [1] and [2] and even then having to override a protected method along with lack of any guidance feels less than ideal.
We can't remove the need for an application to decide which request is associated with which user but we can make this a little easier and better supported by allowing a fixed user token to be configured on
HttpComponentsClientHttpRequestFactory
or accepting aBiFunction
to create theHttpContext
for each request that would at least eliminate the need to extend. We can also improve the Javadoc to provide concrete pointers for those cases.The text was updated successfully, but these errors were encountered: