Preemptive Authentication with HttpClient 4.3 and Solr 5 -
i tried doing preemptive authentication basic auth protected solr using class https://subversion.jfrog.org/jfrog/build-info/trunk/build-info-client/src/main/java/org/jfrog/build/client/preemptivehttpclient.java , solr , methods deprecated not know if problem. situation in querying fine in solr indexing getting ioexception occured when talking server at: example.com:8983/solr/core1 .
the httpsolrclient constructor requires httpclient parameter preemptive authorization class above since httpclient stored in private variable used getter on variable httpclient , pass httpsolrclient constructor. not sure if did right either.
preemptiveauthenticate preemp = new preemptiveauthenticate("username", "password", 1); defaulthttpclient httpclient = preemp.gethttpclient(); system.out.println("made connectsolr after set authentication"); solrclient solr = new httpsolrclient(urlstring, httpclient); i aware of examples http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html example 4.6 preemptive authorization httpclient 4.3 , test case , not see way pass httpclient can preemptive authentication.
fixing paulius's code, preemptive authentication httpclient 4.3. create methods in class call createhttpclient when need connect solr.
public static httpclient createhttpclient(string username, string password) { if (username == null) { username = ""; } if (password == null) { password = ""; } httpclientbuilder clientbuilder = httpclientbuilder.create(); basiccredentialsprovider provider = new basiccredentialsprovider(); provider.setcredentials(authscope.any, new usernamepasswordcredentials(username, password)); clientbuilder.setdefaultcredentialsprovider(provider); clientbuilder.addinterceptorfirst(new preemptiveauthinterceptor()); return clientbuilder.build(); } static class preemptiveauthinterceptor implements httprequestinterceptor { @override public void process (httprequest request, httpcontext context) throws httpexception { authstate authstate = (authstate) context.getattribute(httpclientcontext.target_auth_state); if (authstate.getauthscheme() == null) { credentialsprovider credsprovider = (credentialsprovider) context.getattribute(httpclientcontext.creds_provider); httphost targethost = (httphost) context.getattribute(httpcorecontext.http_target_host); credentials credentials = credsprovider.getcredentials(new authscope(targethost.gethostname(), targethost.getport())); if (credentials == null) { throw new httpexception("no credentials provided preemptive authentication."); } authstate.update(new basicscheme(), credentials); } } }
Comments
Post a Comment