/** * Add authentication to supplied builder. * @param builder the builder to configure * @param uri the server URI * @param username the username * @param password the password * @return the passed in builder */ protected static HttpClientBuilder addAuthentication(final HttpClientBuilder builder, final URI uri, final String username, String password) { if (isNotBlank(username)) { CredentialsProvider provider = new BasicCredentialsProvider(); AuthScope scope = new AuthScope(uri.getHost(), uri.getPort(), "realm"); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password); provider.setCredentials(scope, credentials); builder.setDefaultCredentialsProvider(provider); builder.addInterceptorFirst(new PreemptiveAuth()); } return builder; }
/** * Create an authenticated Jenkins HTTP client * * @param uri * Location of the jenkins server (ex. http://localhost:8080) * @param username * Username to use when connecting * @param password * Password or auth token to use when connecting */ public JenkinsHttpClient(URI uri, String username, String password) { this(uri); if (isNotBlank(username)) { CredentialsProvider provider = client.getCredentialsProvider(); AuthScope scope = new AuthScope(uri.getHost(), uri.getPort(), AuthScope.ANY_REALM); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password); provider.setCredentials(scope, credentials); localContext = new BasicHttpContext(); localContext.setAttribute("preemptive-auth", new BasicScheme()); client.addRequestInterceptor(new PreemptiveAuth(), 0); } }
client.addRequestInterceptor(new PreemptiveAuth(), 0);