Steeltoe icon indicating copy to clipboard operation
Steeltoe copied to clipboard

Eureka service registry http header support to pass token

Open ghanshyamjaswal opened this issue 5 years ago • 2 comments

we are not able to submit eureka registry and heath check due to no option to provide customize http header.

We want to send token using http header, but no option is available in eurekaclientconfig.

ghanshyamjaswal avatar Oct 21 '20 12:10 ghanshyamjaswal

There are several settings available for using OAuth2 with Eureka, are you looking to also use a custom header name here too?

IHttpClientHandlerProvider is available for customizing HttpClient interactions with Eureka

TimHess avatar Oct 21 '20 14:10 TimHess

I need to pass an http header with the name of "xmsh-token" and value is some string. How can i pass extra headers while calling my custom eureka registry api..

ghanshyamjaswal avatar Nov 12 '20 03:11 ghanshyamjaswal

After the merge of #1280, this can be accomplished by adding a custom handler (which adds your header) to the Eureka HttpClientFactory pipeline.

For example, to send an extra header to Eureka:

public class ExtraRequestHeaderDelegatingHandler : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
        CancellationToken cancellationToken)
    {
        request.Headers.Add("X-Special-Feature", "enabled");
        return base.SendAsync(request, cancellationToken);
    }
}

WebApplicationBuilder builder = WebApplication.CreateBuilder();
builder.Services.AddTransient<ExtraRequestHeaderDelegatingHandler>();
builder.Services.AddEurekaDiscoveryClient();
builder.Services.Configure<HttpClientFactoryOptions>("Eureka",
    options => options.HttpMessageHandlerBuilderActions.Add(handlerBuilder =>
        handlerBuilder.AdditionalHandlers.Add(
            handlerBuilder.Services.GetRequiredService<ExtraRequestHeaderDelegatingHandler>())));

To send an extra header to the OAuth2 endpoint, replace "Eureka" with "AccessTokenForEureka" in the example above.

bart-vmware avatar Apr 18 '24 10:04 bart-vmware