當前位置: 首頁>>代碼示例>>Java>>正文


Java CredentialsProvider.setCredentials方法代碼示例

本文整理匯總了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);
    }
}
 
開發者ID:lxxlxx888,項目名稱:Reer,代碼行數:21,代碼來源:HttpClientConfigurer.java

示例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();
}
 
開發者ID:felayman,項目名稱:elasticsearch-full,代碼行數:23,代碼來源:XPackBaseDemo.java

示例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);
    }
}
 
開發者ID:hylkevds,項目名稱:SensorThingsProcessor,代碼行數:17,代碼來源:AuthBasic.java

示例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();
}
  }
 
開發者ID:spyder007,項目名稱:teamcity-msteams-notifier,代碼行數:20,代碼來源:MsTeamsNotificationImpl.java

示例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;
}
 
開發者ID:SentinelDataHub,項目名稱:dhus-core,代碼行數:20,代碼來源:ODataClient.java

示例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();
    }
}
 
開發者ID:yunpian,項目名稱:yunpian-java-sdk,代碼行數:19,代碼來源:AsyncClientAuthentication.java

示例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;
}
 
開發者ID:Communote,項目名稱:communote-server,代碼行數:20,代碼來源:ConfluenceUserImageProvider.java

示例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();
}
 
開發者ID:redcracker,項目名稱:TextSecure,代碼行數:27,代碼來源:MmsConnection.java

示例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));
        }
    }
}
 
開發者ID:johrstrom,項目名稱:cloud-meter,代碼行數:31,代碼來源:AuthManager.java

示例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();
    }
 
開發者ID:fsforna,項目名稱:MassiveDelete,代碼行數:19,代碼來源:HttpRestClient.java

示例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();
}
 
開發者ID:vmware,項目名稱:OHMS,代碼行數:27,代碼來源:HttpUtil.java

示例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;
}
 
開發者ID:devnull-tools,項目名稱:boteco,代碼行數:19,代碼來源:DefaultRestConfiguration.java

示例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);
}
 
開發者ID:sanjuthomas,項目名稱:kafka-connect-marklogic,代碼行數:15,代碼來源:MarkLogicWriter.java

示例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;
}
 
開發者ID:alexmy21,項目名稱:gmds,代碼行數:15,代碼來源:EsRestIndexer.java


注:本文中的org.apache.http.client.CredentialsProvider.setCredentials方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。