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