On Thu, 2023-07-13 at 17:37 +0000, D'Ascola, Giovanni wrote:
> I did, but what I tried didn’t work. I’ve put the code in my original
> email.
> I ended up manually adding the Authorization header to the request in
> the same interceptor.
> It works, but it’s not very nice, I’d rather let the framework take
> care of that.
> 

It does. 

Oleg

---
final HttpHost target = new HttpHost("http", "httpbin.org", 80);
try (final CloseableHttpClient httpclient = HttpClients.custom()
        .addExecInterceptorBefore(ChainElement.PROTOCOL.name(), "my-exec", 
(request, scope, chain) -> {
            final BasicScheme authScheme = new 
BasicScheme(StandardCharsets.UTF_8);
            authScheme.initPreemptive(new UsernamePasswordCredentials("user", 
"passwd".toCharArray()));
            scope.clientContext.resetAuthExchange(target, authScheme);
            return chain.proceed(request, scope);
        })
        .build()) {

    final HttpGet httpget = new 
HttpGet("http://httpbin.org/hidden-basic-auth/user/passwd";);

    System.out.println("Executing request " + httpget.getMethod() + " " + 
httpget.getUri());
    for (int i = 0; i < 3; i++) {
        httpclient.execute(httpget, response -> {
            System.out.println("----------------------------------------");
            System.out.println(httpget + "->" + new StatusLine(response));
            EntityUtils.consume(response.getEntity());
            return null;
        });
    }
}



> From: Oleg Kalnichevski <ol...@apache.org>
> Date: Thursday, 13 July 2023 at 18:29
> To: HttpClient User Discussion <httpclient-users@hc.apache.org>
> Subject: Re: Preemptive Basic auth with Apache HttpClient 5.2
> On Thu, 2023-07-13 at 17:02 +0000, D'Ascola, Giovanni wrote:
> > I did look at the examples, but the Preemptive Basic Auth
> > one<
> > https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fg
> > ithub.com%2Fapache%2Fhttpcomponents-
> > client%2Fblob%2Fmaster%2Fhttpc&data=05%7C01%7Cgiovanni.d%27ascola%4
> > 0fisglobal.com%7Ce0e97ea71a0045101ffe08db83c6b19d%7Ce3ff91d834c84b1
> > 5a0b418910a6ac575%7C0%7C0%7C638248661625316847%7CUnknown%7CTWFpbGZs
> > b3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0
> > %3D%7C3000%7C%7C%7C&sdata=%2FfilF3W2aBdeGakzwBMi3%2BP%2B36rWEDdHLY0
> > HO2DZTKE%3D&reserved=0
> > lient5/src/test/java/org/apache/hc/client5/http/examples/ClientPree
> > mp
> > tiveBasicAuthentication.java> requires to manually create and
> > configure a context and pass it to the HttpClient.execute() method
> > every time. Apart from that not being very convenient, it wouldn’t
> > work with RestTemplate, as it uses HttpClient as its underlying
> > client and you don’t call the HttpClient.execute() directly.
> > 
> 
> And why? What stops you from manipulating the execution context from
> an
> interceptor?
> 
> Oleg
> 
> 
> > Giovanni
> > 
> > From: Oleg Kalnichevski <ol...@apache.org>
> > Date: Thursday, 13 July 2023 at 17:03
> > To: HttpClient User Discussion <httpclient-users@hc.apache.org>
> > Subject: Re: Preemptive Basic auth with Apache HttpClient 5.2
> > On Thu, 2023-07-13 at 15:27 +0000, D'Ascola, Giovanni wrote:
> > >  Is there a way to implement preemptive basic authentication with
> > > Apache HttpClient 5.2 using a HttpRequestInterceptor similar to
> > > how
> > > it's done here (accepted
> > > response)<
> > > https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fs%2F&data=05%7C01%7Cgiovanni.d%27ascola%40fisglobal.com%7Ce0e97ea71a0045101ffe08db83c6b19d%7Ce3ff91d834c84b15a0b418910a6ac575%7C0%7C0%7C638248661625316847%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=f2QYwKTW3%2FuwY39eO13blIDgH7mJdfr3BxINTLhOgsc%3D&reserved=0
> > > <https://s/>
> > > tackoverflow.com%2Fquestions%2F2014700%2Fpreemptive-
> > > basi&data=05%7C01%7Cgiovanni.d%27ascola%40fisglobal.com%7Cdacd543
> > > 15
> > > 4b24a5b9f1b08db83baaa96%7Ce3ff91d834c84b15a0b418910a6ac575%7C0%7C
> > > 0%
> > > 7C638248609963849079%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAi
> > > LC
> > > JQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdat
> > > a=
> > > Ae7mu%2F%2BJju8PJ4nS8XEe3UDQ2wtgRlNykW2PIEmgZyA%3D&reserved=0
> > > c-authentication-with-apache-httpclient-4> for Apache HttpClient
> > > 4?
> > > 
> > > 
> > > 
> > > We use Apache HttpClient both directly and as the RestTemplate
> > > underlying Http client. Preemptive auth used to work with
> > > HttpClient
> > > 4 using HttpRequestInterceptor (see the accepted response for the
> > > question I linked above), but we can't reuse the same code as the
> > > AuthScheme class is now gone.
> > > 
> > > 
> > > 
> > > I tried a few of things but neither worked (see below).
> > 
> > Have you looked at:
> > 
> > https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fhc.apache.org%2Fhttpcomponents-client-5.2.x%2Fexamples.html&data=05%7C01%7Cgiovanni.d%27ascola%40fisglobal.com%7Ce0e97ea71a0045101ffe08db83c6b19d%7Ce3ff91d834c84b15a0b418910a6ac575%7C0%7C0%7C638248661625316847%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=jWhF1bFkFBJn7gk1aDQp3vwglM5wM9xhVGXtIaaja%2F8%3D&reserved=0
> > <https://hc.apache.org/httpcomponents-client-5.2.x/examples.html>
> > <
> > https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fhc
> > .apache.org%2Fhttpcomponents-client-
> > 5.2.x%2Fexamples.html&data=05%7C01%7Cgiovanni.d%27ascola%40fisgloba
> > l.com%7Ce0e97ea71a0045101ffe08db83c6b19d%7Ce3ff91d834c84b15a0b41891
> > 0a6ac575%7C0%7C0%7C638248661625316847%7CUnknown%7CTWFpbGZsb3d8eyJWI
> > joiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C300
> > 0%7C%7C%7C&sdata=jWhF1bFkFBJn7gk1aDQp3vwglM5wM9xhVGXtIaaja%2F8%3D&r
> > eserved=0<https://hc.apache.org/httpcomponents-client-
> > 5.2.x/examples.html>>
> > https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fapache%2Fhttpcomponents-client%2Fblob%2Fmaster%2Fhttpclient5%2Fsrc%2Ftest%2Fjava%2Forg%2Fapache%2Fhc%2Fclient5%2Fhttp%2Fexamples%2FClientPreemptiveBasicAuthentication.java&data=05%7C01%7Cgiovanni.d%27ascola%40fisglobal.com%7Ce0e97ea71a0045101ffe08db83c6b19d%7Ce3ff91d834c84b15a0b418910a6ac575%7C0%7C0%7C638248661625316847%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=P1c55P%2F%2FC99E82a7ohuwLXFlO8hvh2DMutlfBThg%2BAY%3D&reserved=0
> > <
> > https://github.com/apache/httpcomponents-client/blob/master/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientPreemptiveBasicAuthentication.java
> > >
> > <
> > https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fapache%2Fhttpcomponents-client%2Fblob%2Fmaster%2Fhttpclient5%2Fsrc%2Ftest%2Fjava%2Forg%2Fapache%2Fhc%2Fclient5%2Fhttp%2Fexamples%2FClientPreemptiveBasicAuthentication.java&data=05%7C01%7Cgiovanni.d%27ascola%40fisglobal.com%7Ce0e97ea71a0045101ffe08db83c6b19d%7Ce3ff91d834c84b15a0b418910a6ac575%7C0%7C0%7C638248661625316847%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=P1c55P%2F%2FC99E82a7ohuwLXFlO8hvh2DMutlfBThg%2BAY%3D&reserved=0
> > <
> > https://github.com/apache/httpcomponents-client/blob/master/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientPreemptiveBasicAuthentication.java
> > >
> > > 
> > 
> > ?
> > 
> > Oleg
> > 
> > 
> > -------------------------------------------------------------------
> > --
> > To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org
> > For additional commands, e-mail:
> > httpclient-users-h...@hc.apache.org
> > The information contained in this message is proprietary and/or
> > confidential. If you are not the intended recipient, please: (i)
> > delete the message and all copies; (ii) do not disclose, distribute
> > or use the message in any manner; and (iii) notify the sender
> > immediately. In addition, please be aware that any message
> > addressed
> > to our domain is subject to archiving and review by persons other
> > than the intended recipient. FIS is a trading name of the following
> > companies: Alphakinetic Limited (No: 06897969) | FIS Derivatives
> > Utility Services (UK) Limited (No: 9398140) | FIS Energy Solutions
> > Limited (No: 1889028) | FIS Global Execution Services Limited (No.
> > 3127109) | FIS Capital Markets UK Limited (No: 982833) | FIS
> > Treasury
> > Centre Limited (No. 13543636) | Metavante Technologies Limited (No:
> > 2659326) | Virtus Partners Limited (No: 06602363) | Worldpay (UK)
> > Limited (No: 07316500 / FCA No: 530923) | Worldpay Limited (No:
> > 03424752 / FCA No: 504504) | Worldpay AP Limited (No: 05593466 /
> > FCA
> > No: 502597) all registered in England & Wales with their registered
> > office: C/O F I S Corporate Governance, The Walbrook Building, 25
> > Walbrook, London, EC4N 8AF. The WorldPay entities are authorised by
> > the Financial Conduct Authority under the Payment Service
> > Regulations
> > 2017 for the provision of payment services. | Worldpay (UK) Limited
> > is authorised and regulated by the Financial Conduct Authority for
> > consumer credit activities | FIS Global Execution Services Limited
> > is
> > authorised and regulated by the Financial Conduct Authority | FIS
> > Banking Solutions UK Limited (No: 3517639) and FIS Payments (UK)
> > Limited (No: 4215488) are registered in England & Wales with their
> > registered office at 1st Floor Tricorn House, 51-53 Hagley Road,
> > Edgbaston, Birmingham, West Midlands, B16 8TU, United Kingdom | FIS
> > Payments (UK) Limited is authorised and regulated by the Financial
> > Conduct Authority; some services are covered by the Financial
> > Ombudsman Service (in the UK). Worldpay B.V. has its registered
> > office in Amsterdam, the Netherlands (Handelsregister KvK No:
> > 60494344). WPBV holds a licence from and is included in the
> > register
> > kept by De Nederlandsche Bank, which registration can be consulted
> > through
> > https://eur02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.dnb.nl%2F&data=05%7C01%7Cgiovanni.d%27ascola%40fisglobal.com%7Ce0e97ea71a0045101ffe08db83c6b19d%7Ce3ff91d834c84b15a0b418910a6ac575%7C0%7C0%7C638248661625316847%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=KmIEgpD7KY30DVUFxmBp%2Be8fgtMc5JCY9Km607g8J%2FI%3D&reserved=0
> > <http://www.dnb.nl/>. Calls to and from the companies may be
> > recorded
> > for quality purposes. | All of the named companies are ultimately
> > owned by Fidelity National Information Services, Inc., an NYSE
> > listed
> > trading Company with the ticker symbol FIS.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org
> For additional commands, e-mail: httpclient-users-h...@hc.apache.org
> The information contained in this message is proprietary and/or
> confidential. If you are not the intended recipient, please: (i)
> delete the message and all copies; (ii) do not disclose, distribute
> or use the message in any manner; and (iii) notify the sender
> immediately. In addition, please be aware that any message addressed
> to our domain is subject to archiving and review by persons other
> than the intended recipient. FIS is a trading name of the following
> companies: Alphakinetic Limited (No: 06897969) | FIS Derivatives
> Utility Services (UK) Limited (No: 9398140) | FIS Energy Solutions
> Limited (No: 1889028) | FIS Global Execution Services Limited (No.
> 3127109) | FIS Capital Markets UK Limited (No: 982833) | FIS Treasury
> Centre Limited (No. 13543636) | Metavante Technologies Limited (No:
> 2659326) | Virtus Partners Limited (No: 06602363) | Worldpay (UK)
> Limited (No: 07316500 / FCA No: 530923) | Worldpay Limited (No:
> 03424752 / FCA No: 504504) | Worldpay AP Limited (No: 05593466 / FCA
> No: 502597) all registered in England & Wales with their registered
> office: C/O F I S Corporate Governance, The Walbrook Building, 25
> Walbrook, London, EC4N 8AF. The WorldPay entities are authorised by
> the Financial Conduct Authority under the Payment Service Regulations
> 2017 for the provision of payment services. | Worldpay (UK) Limited
> is authorised and regulated by the Financial Conduct Authority for
> consumer credit activities | FIS Global Execution Services Limited is
> authorised and regulated by the Financial Conduct Authority | FIS
> Banking Solutions UK Limited (No: 3517639) and FIS Payments (UK)
> Limited (No: 4215488) are registered in England & Wales with their
> registered office at 1st Floor Tricorn House, 51-53 Hagley Road,
> Edgbaston, Birmingham, West Midlands, B16 8TU, United Kingdom | FIS
> Payments (UK) Limited is authorised and regulated by the Financial
> Conduct Authority; some services are covered by the Financial
> Ombudsman Service (in the UK). Worldpay B.V. has its registered
> office in Amsterdam, the Netherlands (Handelsregister KvK No:
> 60494344). WPBV holds a licence from and is included in the register
> kept by De Nederlandsche Bank, which registration can be consulted
> through www.dnb.nl. Calls to and from the companies may be recorded
> for quality purposes. | All of the named companies are ultimately
> owned by Fidelity National Information Services, Inc., an NYSE listed
> trading Company with the ticker symbol FIS.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org
For additional commands, e-mail: httpclient-users-h...@hc.apache.org

Reply via email to