当前位置: 首页>>代码示例>>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;未经允许,请勿转载。