本文整理汇总了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);
}
示例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());
}
示例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);
}
示例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();
}
示例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());
}
示例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());
}
}
示例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;
}
示例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());
}
示例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());
}
示例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());
}
}
示例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());
}
示例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());
}
示例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());
}
示例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());
}
示例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;
}