本文整理匯總了Java中org.apache.http.client.CredentialsProvider.setCredentials方法的典型用法代碼示例。如果您正苦於以下問題:Java CredentialsProvider.setCredentials方法的具體用法?Java CredentialsProvider.setCredentials怎麽用?Java CredentialsProvider.setCredentials使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.client.CredentialsProvider
的用法示例。
在下文中一共展示了CredentialsProvider.setCredentials方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: useCredentials
import org.apache.http.client.CredentialsProvider; //導入方法依賴的package包/類
private void useCredentials(CredentialsProvider credentialsProvider, String host, int port, Collection<? extends Authentication> authentications) {
Credentials httpCredentials;
for (Authentication authentication : authentications) {
String scheme = getAuthScheme(authentication);
PasswordCredentials credentials = getPasswordCredentials(authentication);
if (authentication instanceof AllSchemesAuthentication) {
NTLMCredentials ntlmCredentials = new NTLMCredentials(credentials);
httpCredentials = new NTCredentials(ntlmCredentials.getUsername(), ntlmCredentials.getPassword(), ntlmCredentials.getWorkstation(), ntlmCredentials.getDomain());
credentialsProvider.setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM, AuthSchemes.NTLM), httpCredentials);
LOGGER.debug("Using {} and {} for authenticating against '{}:{}' using {}", credentials, ntlmCredentials, host, port, AuthSchemes.NTLM);
}
httpCredentials = new UsernamePasswordCredentials(credentials.getUsername(), credentials.getPassword());
credentialsProvider.setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM, scheme), httpCredentials);
LOGGER.debug("Using {} for authenticating against '{}:{}' using {}", credentials, host, port, scheme);
}
}
示例2: setUp
import org.apache.http.client.CredentialsProvider; //導入方法依賴的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();
}
示例3: setAuth
import org.apache.http.client.CredentialsProvider; //導入方法依賴的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);
}
}
示例4: setProxy
import org.apache.http.client.CredentialsProvider; //導入方法依賴的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();
}
}
示例5: generateClient
import org.apache.http.client.CredentialsProvider; //導入方法依賴的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;
}
示例6: main
import org.apache.http.client.CredentialsProvider; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope("localhost", 443),
new UsernamePasswordCredentials("username", "password"));
CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().setDefaultCredentialsProvider(credsProvider)
.build();
try {
HttpGet httpget = new HttpGet("http://localhost/");
System.out.println("Executing request " + httpget.getRequestLine());
Future<HttpResponse> future = httpclient.execute(httpget, null);
HttpResponse response = future.get();
System.out.println("Response: " + response.getStatusLine());
System.out.println("Shutting down");
} finally {
httpclient.close();
}
}
示例7: getRequestContext
import org.apache.http.client.CredentialsProvider; //導入方法依賴的package包/類
@Override
protected HttpContext getRequestContext(URI imageUrl, String imageIdentifier,
ConfluenceConfiguration config) {
HttpHost targetHost = new HttpHost(imageUrl.getHost(), imageUrl.getPort());
CredentialsProvider credentialsProviderProvider = new BasicCredentialsProvider();
credentialsProviderProvider.setCredentials(new AuthScope(targetHost.getHostName(),
targetHost.getPort()), new UsernamePasswordCredentials(config.getAdminLogin(),
config.getAdminPassword()));
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
// preemptive authentication (send credentials with request) by adding host to auth cache
// TODO maybe use real basic auth challenge?
authCache.put(targetHost, basicAuth);
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialsProviderProvider);
context.setAuthCache(authCache);
return context;
}
示例8: DataCiteRESTfullClient
import org.apache.http.client.CredentialsProvider; //導入方法依賴的package包/類
public DataCiteRESTfullClient(String url, String username, String password) {
this.url = url;
try {
context = HttpClientContext.create();
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(null, -1),
new UsernamePasswordCredentials(username, password));
context.setCredentialsProvider(credsProvider);
httpClient = HttpClients.createDefault();
} catch (Exception ioe) {
close();
logger.log(Level.SEVERE,"Fail to init Client",ioe);
throw new RuntimeException("Fail to init Client", ioe);
}
}
開發者ID:pengchengluo,項目名稱:Peking-University-Open-Research-Data-Platform,代碼行數:17,代碼來源:DataCiteRESTfullClient.java
示例9: constructHttpClient
import org.apache.http.client.CredentialsProvider; //導入方法依賴的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("Android-Mms/2.0")
.setConnectionManager(new BasicHttpClientConnectionManager())
.setDefaultRequestConfig(config)
.setDefaultCredentialsProvider(credsProvider)
.build();
}
示例10: setupCredentials
import org.apache.http.client.CredentialsProvider; //導入方法依賴的package包/類
/**
* Configure credentials and auth scheme on client if an authorization is
* available for url
* @param client {@link HttpClient}
* @param url URL to test
* @param credentialsProvider {@link CredentialsProvider}
* @param localHost host running JMeter
*/
public void setupCredentials(HttpClient client, URL url,
CredentialsProvider credentialsProvider, String localHost) {
Authorization auth = getAuthForURL(url);
if (auth != null) {
String username = auth.getUser();
String realm = auth.getRealm();
String domain = auth.getDomain();
if (log.isDebugEnabled()){
log.debug(username + " > D="+domain+" R="+realm + " M="+auth.getMechanism());
}
if (Mechanism.KERBEROS.equals(auth.getMechanism())) {
((AbstractHttpClient) client).getAuthSchemes().register(
AuthSchemes.SPNEGO,
new FixedSPNegoSchemeFactory(isStripPort(url)));
credentialsProvider.setCredentials(new AuthScope(null, -1, null), USE_JAAS_CREDENTIALS);
} else {
credentialsProvider.setCredentials(
new AuthScope(url.getHost(), url.getPort(), realm.length()==0 ? null : realm),
new NTCredentials(username, auth.getPass(), localHost, domain));
}
}
}
示例11: HttpRestClient
import org.apache.http.client.CredentialsProvider; //導入方法依賴的package包/類
public HttpRestClient(String user, String password) {
SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(DEFAULT_SOCKET_TIMEOUT * 1000).build();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(DEFAULT_REQUEST_TIMEOUT * 1000)
.setSocketTimeout(DEFAULT_SOCKET_TIMEOUT * 1000)
.setConnectionRequestTimeout(DEFAULT_REQUEST_TIMEOUT * 1000).build();
httpConnectionManager.setMaxTotal(DEFAULT_CONN_NUM);
httpConnectionManager.setDefaultMaxPerRoute(DEFAULT_CONN_NUM);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(AuthScope.ANY), new UsernamePasswordCredentials(user, password));
client = HttpClients.custom().setConnectionManager(httpConnectionManager)
.setDefaultRequestConfig(requestConfig)
.setDefaultSocketConfig(socketConfig)
.setDefaultCredentialsProvider(credsProvider).build();
}
示例12: getHttpClientWithBasicAuth
import org.apache.http.client.CredentialsProvider; //導入方法依賴的package包/類
/**
* Returns HttpClient with basic auth.
*
* @param userName the user name
* @param password the password
* @return the http client with basic auth
*/
private static HttpClient getHttpClientWithBasicAuth( final String userName, final String password )
{
// check that userName is not null
if ( StringUtils.isBlank( userName ) )
{
logger.warn( "In getHttpClientWithBasicAuth, userName is either null or blank." );
return null;
}
// check that password is not null
if ( StringUtils.isBlank( password ) )
{
logger.warn( "In getHttpClientWithBasicAuth, password is either null or blank." );
return null;
}
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials( userName, password );
credentialsProvider.setCredentials( AuthScope.ANY, usernamePasswordCredentials );
return HttpClientBuilder.create().setDefaultCredentialsProvider( credentialsProvider ).build();
}
示例13: withAuthentication
import org.apache.http.client.CredentialsProvider; //導入方法依賴的package包/類
@Override
public RestConfiguration withAuthentication(String user, String password) {
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
CredentialsProvider provider = new BasicCredentialsProvider();
URI uri = request.getURI();
authCache.put(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()), basicAuth);
provider.setCredentials(new AuthScope(uri.getHost(), AuthScope.ANY_PORT),
new UsernamePasswordCredentials(user, password));
this.context.setCredentialsProvider(provider);
this.context.setAuthCache(authCache);
return this;
}
示例14: MarkLogicWriter
import org.apache.http.client.CredentialsProvider; //導入方法依賴的package包/類
public MarkLogicWriter(final Map<String, String> config){
connectionUrl = config.get(MarkLogicSinkConfig.CONNECTION_URL);
user = config.get(MarkLogicSinkConfig.CONNECTION_USER);
password = config.get(MarkLogicSinkConfig.CONNECTION_PASSWORD);
requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5 * 1000).build();
localContext = HttpClientContext.create();
httpClient = HttpClientBuilder.create().build();
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
localContext.setCredentialsProvider(credentialsProvider);
localContext.setRequestConfig(requestConfig);
}
示例15: setCredentials
import org.apache.http.client.CredentialsProvider; //導入方法依賴的package包/類
/**
*
* @param uid
* @param psw
* @return
*/
@Override
public CredentialsProvider setCredentials(String uid, String psw) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(uid, psw));
return credentialsProvider;
}