本文整理匯總了Java中org.apache.http.impl.client.BasicCredentialsProvider類的典型用法代碼示例。如果您正苦於以下問題:Java BasicCredentialsProvider類的具體用法?Java BasicCredentialsProvider怎麽用?Java BasicCredentialsProvider使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
BasicCredentialsProvider類屬於org.apache.http.impl.client包,在下文中一共展示了BasicCredentialsProvider類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: constructHttpClient
import org.apache.http.impl.client.BasicCredentialsProvider; //導入依賴的package包/類
protected CloseableHttpClient constructHttpClient() throws IOException {
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(20 * 1000)
.setConnectionRequestTimeout(20 * 1000)
.setSocketTimeout(20 * 1000)
.setMaxRedirects(20)
.build();
URL mmsc = new URL(apn.getMmsc());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
if (apn.hasAuthentication()) {
credsProvider.setCredentials(new AuthScope(mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()),
new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword()));
}
return HttpClients.custom()
.setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4())
.setRedirectStrategy(new LaxRedirectStrategy())
.setUserAgent(TextSecurePreferences.getMmsUserAgent(context, USER_AGENT))
.setConnectionManager(new BasicHttpClientConnectionManager())
.setDefaultRequestConfig(config)
.setDefaultCredentialsProvider(credsProvider)
.build();
}
示例2: 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);
}
示例3: handleError
import org.apache.http.impl.client.BasicCredentialsProvider; //導入依賴的package包/類
public static void handleError(String url, String login, String password, Scenario scenario) throws Exception {
String issueId = "";
for(String tag : scenario.getSourceTagNames()) {
if (tag.contains("SAM-")) {
issueId = tag.substring(1);
break;
}
}
if (issueId.equals("")) {
return;
}
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(login, password);
provider.setCredentials(AuthScope.ANY, credentials);
HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
URI uri = UriBuilder.fromUri(url)
.path("/rest/api/2/issue/" + issueId).build();
String status = "Passed";
if (scenario != null && scenario.isFailed()) {
status = "Failed";
}
HttpPut request = new HttpPut(uri);
HttpEntity entity = new StringEntity("{\"fields\":{\"customfield_10007\": \"" + status + "\"}}");
request.setEntity(entity);
request.addHeader("Content-Type", "application/json");
HttpResponse response = client.execute(request);
client.getConnectionManager().shutdown();
}
示例4: setAuth
import org.apache.http.impl.client.BasicCredentialsProvider; //導入依賴的package包/類
@Override
public void setAuth(SensorThingsService service) {
try {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
URL url = service.getEndpoint().toURL();
credsProvider.setCredentials(
new AuthScope(url.getHost(), url.getPort()),
new UsernamePasswordCredentials(editorUsername.getValue(), editorPassword.getValue()));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
service.setClient(httpclient);
} catch (MalformedURLException ex) {
LOGGER.error("Failed to initialise basic auth.", ex);
}
}
示例5: generateClient
import org.apache.http.impl.client.BasicCredentialsProvider; //導入依賴的package包/類
@Override
public CloseableHttpAsyncClient generateClient ()
{
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope (AuthScope.ANY),
new UsernamePasswordCredentials(serviceUser, servicePass));
RequestConfig rqconf = RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.setSocketTimeout(Timeouts.SOCKET_TIMEOUT)
.setConnectTimeout(Timeouts.CONNECTION_TIMEOUT)
.setConnectionRequestTimeout(Timeouts.CONNECTION_REQUEST_TIMEOUT)
.build();
CloseableHttpAsyncClient res = HttpAsyncClients.custom ()
.setDefaultCredentialsProvider (credsProvider)
.setDefaultRequestConfig(rqconf)
.build ();
res.start ();
return res;
}
示例6: generateClient
import org.apache.http.impl.client.BasicCredentialsProvider; //導入依賴的package包/類
@Override
public CloseableHttpAsyncClient generateClient ()
{
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope (AuthScope.ANY),
new UsernamePasswordCredentials(username, password));
RequestConfig rqconf = RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.setSocketTimeout(Timeouts.SOCKET_TIMEOUT)
.setConnectTimeout(Timeouts.CONNECTION_TIMEOUT)
.setConnectionRequestTimeout(Timeouts.CONNECTION_REQUEST_TIMEOUT)
.build();
CloseableHttpAsyncClient res = HttpAsyncClients.custom ()
.setDefaultCredentialsProvider (credsProvider)
.setDefaultRequestConfig(rqconf)
.build ();
res.start ();
return res;
}
示例7: ElasticBulkSender
import org.apache.http.impl.client.BasicCredentialsProvider; //導入依賴的package包/類
public ElasticBulkSender(String user, String password, HttpHost... hosts) {
this.user = user;
this.password = password;
this.hosts = hosts;
this.restClient = RestClient.builder(hosts)
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
if (!Strings.isBlank(user)) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
} else {
return httpClientBuilder;
}
}
})
.build();
}
示例8: setProxy
import org.apache.http.impl.client.BasicCredentialsProvider; //導入依賴的package包/類
public void setProxy(String proxyHost, Integer proxyPort, Credentials credentials) {
this.proxyHost = proxyHost;
this.proxyPort = proxyPort;
if (this.proxyHost.length() > 0 && !this.proxyPort.equals(0)) {
HttpClientBuilder clientBuilder = HttpClients.custom()
.useSystemProperties()
.setProxy(new HttpHost(proxyHost, proxyPort, "http"));
if (credentials != null) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), credentials);
clientBuilder.setDefaultCredentialsProvider(credsProvider);
Loggers.SERVER.debug("MsTeamsNotification ::using proxy credentials " + credentials.getUserPrincipal().getName());
}
this.client = clientBuilder.build();
}
}
示例9: getProxyExecutor
import org.apache.http.impl.client.BasicCredentialsProvider; //導入依賴的package包/類
public static HttpCommandExecutor getProxyExecutor(URL url, Properties prop) {
prop = decrypt(prop);
String proxyHost = prop.getProperty("proxyHost");
int proxyPort = Integer.valueOf(prop.getProperty("proxyPort"));
String proxyUserDomain = prop.getProperty("proxyUserDomain");
String proxyUser = prop.getProperty("proxyUser");
String proxyPassword = prop.getProperty("proxyPassword");
HttpClientBuilder builder = HttpClientBuilder.create();
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort),
new NTCredentials(proxyUser, proxyPassword, getWorkstation(), proxyUserDomain));
if (url.getUserInfo() != null && !url.getUserInfo().isEmpty()) {
credsProvider.setCredentials(new AuthScope(url.getHost(), (url.getPort() > 0 ? url.getPort() : url.getDefaultPort())),
new UsernamePasswordCredentials(proxyUser, proxyPassword));
}
builder.setProxy(proxy);
builder.setDefaultCredentialsProvider(credsProvider);
HttpClient.Factory factory = new SimpleHttpClientFactory(builder);
return new HttpCommandExecutor(new HashMap<String, CommandInfo>(), url, factory);
}
示例10: setUsernamePassword
import org.apache.http.impl.client.BasicCredentialsProvider; //導入依賴的package包/類
@Override public void setUsernamePassword(AuthenticationType authType, String username,
String password) {
this.credentials = new UsernamePasswordCredentials(
Objects.requireNonNull(username), Objects.requireNonNull(password));
this.credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, credentials);
RegistryBuilder<AuthSchemeProvider> authRegistryBuilder = RegistryBuilder.create();
switch (authType) {
case BASIC:
authRegistryBuilder.register(AuthSchemes.BASIC, new BasicSchemeFactory());
break;
case DIGEST:
authRegistryBuilder.register(AuthSchemes.DIGEST, new DigestSchemeFactory());
break;
default:
throw new IllegalArgumentException("Unsupported authentiation type: " + authType);
}
this.authRegistry = authRegistryBuilder.build();
}
示例11: 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());
}
示例12: setUp
import org.apache.http.impl.client.BasicCredentialsProvider; //導入依賴的package包/類
@Before
public void setUp() throws Exception {
/**
* 如果es集群安裝了x-pack插件則以此種方式連接集群
* 1. java客戶端的方式是以tcp協議在9300端口上進行通信
* 2. http客戶端的方式是以http協議在9200端口上進行通信
*/
Settings settings = Settings.builder(). put("xpack.security.user", "elastic:changeme").build();
client = new PreBuiltXPackTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
elasticsearchTemplate = new ElasticsearchTemplate(client);
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("elastic", "changeme"));
restClient = RestClient.builder(new HttpHost("localhost",9200,"http"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}).build();
}
示例13: 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);
}
示例14: createHttpClientContext
import org.apache.http.impl.client.BasicCredentialsProvider; //導入依賴的package包/類
/**
* Creates client context.
* @param url url
* @param cred credentials
* @return client context
*/
public static HttpClientContext createHttpClientContext(URL url, SimpleCredentials cred) {
HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials(cred.getUserName(),cred.getPassword()));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
// Add AuthCache to the execution context
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
return context;
}
示例15: 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();
}