當前位置: 首頁>>代碼示例>>Java>>正文


Java BasicCredentialsProvider.setCredentials方法代碼示例

本文整理匯總了Java中org.apache.http.impl.client.BasicCredentialsProvider.setCredentials方法的典型用法代碼示例。如果您正苦於以下問題:Java BasicCredentialsProvider.setCredentials方法的具體用法?Java BasicCredentialsProvider.setCredentials怎麽用?Java BasicCredentialsProvider.setCredentials使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.http.impl.client.BasicCredentialsProvider的用法示例。


在下文中一共展示了BasicCredentialsProvider.setCredentials方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: createTemplate

import org.apache.http.impl.client.BasicCredentialsProvider; //導入方法依賴的package包/類
public RestTemplate createTemplate(final String host, final Integer port, final String username, final String password)
{
	Preconditions.checkArgument(StringUtils.isNotBlank(host));
	Preconditions.checkArgument(port != null);
	Preconditions.checkArgument(StringUtils.isNotBlank(username));
	Preconditions.checkArgument(StringUtils.isNotBlank(password));

	final AuthScope authscope = new AuthScope(host, port.intValue());
	final Credentials credentials = new UsernamePasswordCredentials(username, password);
	final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
	credentialsProvider.setCredentials(authscope, credentials);

	final HttpClientBuilder clientBuilder = HttpClientBuilder.create();
	clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
	final CloseableHttpClient httpClient = clientBuilder.build();

	final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
	requestFactory.setHttpClient(httpClient);

	return new RestTemplate(requestFactory);
}
 
開發者ID:fredhopper,項目名稱:hybris-connector,代碼行數:22,代碼來源:RestTemplateProvider.java

示例2: determineProxy

import org.apache.http.impl.client.BasicCredentialsProvider; //導入方法依賴的package包/類
@Override
protected HttpHost determineProxy(HttpHost host, HttpRequest request, HttpContext context) throws HttpException {
    HttpClientContext httpClientContext = HttpClientContext.adapt(context);
    Proxy proxy = proxyPlanner.determineProxy(host, request, context, ipPool, crawlerSession);

    if (proxy == null) {
        return null;
    }
    if (log.isDebugEnabled()) {
        log.debug("{} 當前使用IP為:{}:{}", host.getHostName(), proxy.getIp(), proxy.getPort());
    }
    context.setAttribute(VSCRAWLER_AVPROXY_KEY, proxy);

    if (proxy.getAuthenticationHeaders() != null) {
        for (Header header : proxy.getAuthenticationHeaders()) {
            request.addHeader(header);
        }
    }

    if (StringUtils.isNotEmpty(proxy.getUsername()) && StringUtils.isNotEmpty(proxy.getPassword())) {
        BasicCredentialsProvider credsProvider1 = new BasicCredentialsProvider();
        httpClientContext.setCredentialsProvider(credsProvider1);
        credsProvider1.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword()));
    }
    return new HttpHost(proxy.getIp(), proxy.getPort());
}
 
開發者ID:virjar,項目名稱:vscrawler,代碼行數:28,代碼來源:VSCrawlerRoutePlanner.java

示例3: afterPropertiesSet

import org.apache.http.impl.client.BasicCredentialsProvider; //導入方法依賴的package包/類
public void afterPropertiesSet() throws UnsupportedEncodingException {
  Collection<Header> defaultHeaders = new ArrayList<Header>();
  Header header = new BasicHeader("Authorization",
      "Basic " + BaseEncoding.base64().encode("apollo:".getBytes("UTF-8")));
  defaultHeaders.add(header);

  BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  credentialsProvider.setCredentials(AuthScope.ANY,
      new UsernamePasswordCredentials("apollo", ""));
  CloseableHttpClient httpClient =
      HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider)
          .setDefaultHeaders(defaultHeaders).build();


  restTemplate = new RestTemplate(httpMessageConverters.getConverters());
  HttpComponentsClientHttpRequestFactory requestFactory =
      new HttpComponentsClientHttpRequestFactory(httpClient);
  requestFactory.setConnectTimeout(portalConfig.connectTimeout());
  requestFactory.setReadTimeout(portalConfig.readTimeout());

  restTemplate.setRequestFactory(requestFactory);
}
 
開發者ID:dewey-its,項目名稱:apollo-custom,代碼行數:23,代碼來源:RestTemplateFactory.java

示例4: build

import org.apache.http.impl.client.BasicCredentialsProvider; //導入方法依賴的package包/類
public static HttpClient build(byte[] token) {
    HttpClientBuilder builder = HttpClientBuilder.create();
    Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
            .register(AuthSchemes.SPNEGO, new CollaredSPNegoSchemeFactory(token)).build();
    builder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(null, -1, null), new Credentials() {
        @Override
        public Principal getUserPrincipal() {
            return null;
        }

        @Override
        public String getPassword() {
            return null;
        }
    });
    builder.setDefaultCredentialsProvider(credentialsProvider);
    return builder.build();
}
 
開發者ID:tellisnz,項目名稱:collared-kerberos,代碼行數:21,代碼來源:CollaredHttpClientFactory.java

示例5: testNTLMType2NonUnicodeMessageOnlyAuthenticationFailure

import org.apache.http.impl.client.BasicCredentialsProvider; //導入方法依賴的package包/類
@Test
public void testNTLMType2NonUnicodeMessageOnlyAuthenticationFailure() throws Exception {
    this.serverBootstrap.registerHandler("*", new NtlmType2MessageOnlyResponseHandler("TlRMTVNTUAACAA" +
            "AABgAGADgAAAAyggLiASNFZ4mrze8AAAAAAAAAAAAAAAAAAAAABgBwFwAAAA9T" +
            "ZXJ2ZXI="));

    final HttpHost target = start();

    final HttpClientContext context = HttpClientContext.create();
    final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY,
            new NTCredentials("test", "test", null, null));
    context.setCredentialsProvider(credsProvider);
    final HttpGet httpget = new HttpGet("/");

    final HttpResponse response = this.httpclient.execute(target, httpget, context);
    EntityUtils.consume(response.getEntity());
    Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED,
            response.getStatusLine().getStatusCode());
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:21,代碼來源:TestClientAuthenticationFakeNTLM.java

示例6: setProxy

import org.apache.http.impl.client.BasicCredentialsProvider; //導入方法依賴的package包/類
public static void setProxy(String type, final String host, final int port, final String user, final String pwd) {
    if (type != null && host != null) {
        Proxy.Type proxyType = Enum.valueOf(Proxy.Type.class, type.toUpperCase());
        javaNetProxy = new Proxy(proxyType, new InetSocketAddress(host, port));
        Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, pwd.toCharArray());
            }
        });
        if (user != null && pwd != null) {
            proxyCredentials = new BasicCredentialsProvider();
            proxyCredentials.setCredentials(new AuthScope(host, port), new UsernamePasswordCredentials(user, pwd));
        }
        apacheHttpProxy = new HttpHost(host, port, proxyType.name());
    }
}
 
開發者ID:kraftek,項目名稱:awsdownload,代碼行數:18,代碼來源:NetUtils.java

示例7: getHttpContext

import org.apache.http.impl.client.BasicCredentialsProvider; //導入方法依賴的package包/類
private HttpClientContext getHttpContext() {
    HttpClientContext context = HttpClientContext.create();
    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    Credentials credentials = new Credentials() {

        public String getPassword() {
            return null;
        }

        public Principal getUserPrincipal() {
            return null;
        }
    };
    credentialsProvider.setCredentials(new AuthScope(null, -1, null), credentials);
    context.setCredentialsProvider(credentialsProvider);
    return context;
}
 
開發者ID:egetman,項目名稱:ibm-bpm-rest-client,代碼行數:18,代碼來源:KerberosBpmClient.java

示例8: testPreemptiveAuthentication

import org.apache.http.impl.client.BasicCredentialsProvider; //導入方法依賴的package包/類
@Test
public void testPreemptiveAuthentication() throws Exception {
    final CountingAuthHandler requestHandler = new CountingAuthHandler();
    this.serverBootstrap.registerHandler("*", requestHandler);

    final HttpHost target = start();

    final HttpClientContext context = HttpClientContext.create();
    final AuthCache authCache = new BasicAuthCache();
    authCache.put(target, new BasicScheme());
    context.setAuthCache(authCache);
    final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials("test", "test"));
    context.setCredentialsProvider(credsProvider);

    final HttpGet httpget = new HttpGet("/");
    final HttpResponse response1 = this.httpclient.execute(target, httpget, context);
    final HttpEntity entity1 = response1.getEntity();
    Assert.assertEquals(HttpStatus.SC_OK, response1.getStatusLine().getStatusCode());
    Assert.assertNotNull(entity1);
    EntityUtils.consume(entity1);

    Assert.assertEquals(1, requestHandler.getCount());
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:26,代碼來源:TestClientAuthentication.java

示例9: testPreemptiveAuthenticationFailure

import org.apache.http.impl.client.BasicCredentialsProvider; //導入方法依賴的package包/類
@Test
public void testPreemptiveAuthenticationFailure() throws Exception {
    final CountingAuthHandler requestHandler = new CountingAuthHandler();
    this.serverBootstrap.registerHandler("*", requestHandler);

    final HttpHost target = start();

    final HttpClientContext context = HttpClientContext.create();
    final AuthCache authCache = new BasicAuthCache();
    authCache.put(target, new BasicScheme());
    context.setAuthCache(authCache);
    final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials("test", "stuff"));
    context.setCredentialsProvider(credsProvider);

    final HttpGet httpget = new HttpGet("/");
    final HttpResponse response1 = this.httpclient.execute(target, httpget, context);
    final HttpEntity entity1 = response1.getEntity();
    Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response1.getStatusLine().getStatusCode());
    Assert.assertNotNull(entity1);
    EntityUtils.consume(entity1);

    Assert.assertEquals(1, requestHandler.getCount());
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:26,代碼來源:TestClientAuthentication.java

示例10: testConnectionCloseAfterAuthenticationSuccess

import org.apache.http.impl.client.BasicCredentialsProvider; //導入方法依賴的package包/類
@Test
public void testConnectionCloseAfterAuthenticationSuccess() throws Exception {
    this.serverBootstrap.registerHandler("*", new ClosingAuthHandler());

    final HttpHost target = start();

    final HttpClientContext context = HttpClientContext.create();
    final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials("test", "test"));
    context.setCredentialsProvider(credsProvider);

    for (int i = 0; i < 2; i++) {
        final HttpGet httpget = new HttpGet("/");

        final HttpResponse response = this.httpclient.execute(target, httpget, context);
        EntityUtils.consume(response.getEntity());
        Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    }
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:21,代碼來源:TestClientAuthentication.java

示例11: testNTLMAuthenticationFailure

import org.apache.http.impl.client.BasicCredentialsProvider; //導入方法依賴的package包/類
@Test
public void testNTLMAuthenticationFailure() throws Exception {
    this.serverBootstrap.registerHandler("*", new NtlmResponseHandler());

    final HttpHost target = start();

    final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY,
            new NTCredentials("test", "test", null, null));

    this.httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider)
            .build();

    final HttpContext context = HttpClientContext.create();
    final HttpGet httpget = new HttpGet("/");

    final HttpResponse response = this.httpclient.execute(target, httpget, context);
    EntityUtils.consume(response.getEntity());
    Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED,
            response.getStatusLine().getStatusCode());
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:23,代碼來源:TestClientAuthenticationFakeNTLM.java

示例12: testNTLMv1Type2Message

import org.apache.http.impl.client.BasicCredentialsProvider; //導入方法依賴的package包/類
@Test
public void testNTLMv1Type2Message() throws Exception {
    this.serverBootstrap.registerHandler("*", new NtlmType2MessageResponseHandler("TlRMTVNTUAACAA" +
            "AADAAMADgAAAAzggLiASNFZ4mrze8AAAAAAAAAAAAAAAAAAAAABgBwFwAAAA9T" +
            "AGUAcgB2AGUAcgA="));
    final HttpHost target = start();

    final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY,
            new NTCredentials("test", "test", null, null));

    this.httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider)
            .build();

    final HttpContext context = HttpClientContext.create();
    final HttpGet httpget = new HttpGet("/");

    final HttpResponse response = this.httpclient.execute(target, httpget, context);
    EntityUtils.consume(response.getEntity());
    Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED,
            response.getStatusLine().getStatusCode());
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:24,代碼來源:TestClientAuthenticationFakeNTLM.java

示例13: testNTLMv2Type2Message

import org.apache.http.impl.client.BasicCredentialsProvider; //導入方法依賴的package包/類
@Test
public void testNTLMv2Type2Message() throws Exception {
    this.serverBootstrap.registerHandler("*", new NtlmType2MessageResponseHandler("TlRMTVNTUAACAA" +
            "AADAAMADgAAAAzgoriASNFZ4mrze8AAAAAAAAAACQAJABEAAAABgBwFwAAAA9T" +
            "AGUAcgB2AGUAcgACAAwARABvAG0AYQBpAG4AAQAMAFMAZQByAHYAZQByAAAAAAA="));
    final HttpHost target = start();

    final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY,
            new NTCredentials("test", "test", null, null));

    this.httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider)
            .build();

    final HttpContext context = HttpClientContext.create();
    final HttpGet httpget = new HttpGet("/");

    final HttpResponse response = this.httpclient.execute(target, httpget, context);
    EntityUtils.consume(response.getEntity());
    Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED,
            response.getStatusLine().getStatusCode());
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:24,代碼來源:TestClientAuthenticationFakeNTLM.java

示例14: testNTLMType2MessageOnlyAuthenticationFailure

import org.apache.http.impl.client.BasicCredentialsProvider; //導入方法依賴的package包/類
@Test
public void testNTLMType2MessageOnlyAuthenticationFailure() throws Exception {
    this.serverBootstrap.registerHandler("*", new NtlmType2MessageOnlyResponseHandler("TlRMTVNTUAACAA" +
            "AADAAMADgAAAAzggLiASNFZ4mrze8AAAAAAAAAAAAAAAAAAAAABgBwFwAAAA9T" +
            "AGUAcgB2AGUAcgA="));

    final HttpHost target = start();

    final HttpClientContext context = HttpClientContext.create();
    final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY,
            new NTCredentials("test", "test", null, null));
    context.setCredentialsProvider(credsProvider);
    final HttpGet httpget = new HttpGet("/");

    final HttpResponse response = this.httpclient.execute(target, httpget, context);
    EntityUtils.consume(response.getEntity());
    Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED,
            response.getStatusLine().getStatusCode());
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:21,代碼來源:TestClientAuthenticationFakeNTLM.java

示例15: basicAuthCredentials

import org.apache.http.impl.client.BasicCredentialsProvider; //導入方法依賴的package包/類
public HttpClientConfigurer basicAuthCredentials(String username, String password) {
	final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
	credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
	httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);

	useBasicAuth = true;

	return this;
}
 
開發者ID:spring-cloud,項目名稱:spring-cloud-skipper,代碼行數:10,代碼來源:HttpClientConfigurer.java


注:本文中的org.apache.http.impl.client.BasicCredentialsProvider.setCredentials方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。