org.apache.http.auth.AuthScope类的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(17.4k)|赞(0)|评价(0)|浏览(382)

本文整理了Java中org.apache.http.auth.AuthScope类的一些代码示例,展示了AuthScope类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AuthScope类的具体详情如下:
包路径:org.apache.http.auth.AuthScope
类名称:AuthScope

AuthScope介绍

[英]The class represents an authentication scope consisting of a host name, a port number, a realm name and an authentication scheme name which Credentials apply to.
[中]该类表示由主机名、端口号、领域名和凭据应用的身份验证方案名称组成的身份验证作用域。

代码示例

代码示例来源:origin: eirslett/frontend-maven-plugin

private CredentialsProvider makeCredentialsProvider(String host, int port, String username, String password) {
  CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  credentialsProvider.setCredentials(
      new AuthScope(host, port),
      new UsernamePasswordCredentials(username, password)
  );
  return credentialsProvider;
}

代码示例来源:origin: robovm/robovm

/** 
 * Creates a copy of the given credentials scope.
 */
public AuthScope(final AuthScope authscope) {
  super();
  if (authscope == null) {
    throw new IllegalArgumentException("Scope may not be null");
  }
  this.host = authscope.getHost();
  this.port = authscope.getPort();
  this.realm = authscope.getRealm();
  this.scheme = authscope.getScheme();
}

代码示例来源:origin: yasserg/crawler4j

connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost());
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
if (config.getCookieStore() != null) {
  clientBuilder.setDefaultCookieStore(config.getCookieStore());
clientBuilder.setDefaultRequestConfig(requestConfig);
clientBuilder.setConnectionManager(connectionManager);
clientBuilder.setUserAgent(config.getUserAgentString());
if (config.getProxyHost() != null) {
  if (config.getProxyUsername() != null) {
    AuthScope authScope = new AuthScope(config.getProxyHost(), config.getProxyPort());
    Credentials credentials = new UsernamePasswordCredentials(config.getProxyUsername(),
        config.getProxyPassword());
    credentialsMap.put(authScope, credentials);
  HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort());
  clientBuilder.setProxy(proxy);
  logger.debug("Working through Proxy: {}", proxy.getHostName());
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsMap.forEach((AuthScope authscope, Credentials credentials) -> {
      credentialsProvider.setCredentials(authscope, credentials);
    });
    clientBuilder.setDefaultCredentialsProvider(credentialsProvider);

代码示例来源:origin: jersey/jersey

final HttpClientBuilder clientBuilder = HttpClientBuilder.create();
clientBuilder.setConnectionManager(getConnectionManager(client, config, sslContext));
clientBuilder.setConnectionManagerShared(
    PropertiesHelper.getValue(config.getProperties(), ApacheClientProperties.CONNECTION_MANAGER_SHARED, false, null));
clientBuilder.setSslcontext(sslContext);
if (proxyUri != null) {
  final URI u = getProxyUri(proxyUri);
  final HttpHost proxy = new HttpHost(u.getHost(), u.getPort(), u.getScheme());
  final String userName;
  userName = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_USERNAME, String.class);
      final CredentialsProvider credsProvider = new BasicCredentialsProvider();
      credsProvider.setCredentials(
          new AuthScope(u.getHost(), u.getPort()),
          new UsernamePasswordCredentials(userName, password)
      );
      clientBuilder.setDefaultCredentialsProvider(credsProvider);

代码示例来源:origin: syncany/syncany

.setRedirectsEnabled(false);
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    Integer proxyPort = Integer.parseInt(proxyPortStr);
    requestConfigBuilder.setProxy(new HttpHost(proxyHost, proxyPort));
    logger.log(Level.INFO, "Using proxy: " + proxyHost + ":" + proxyPort);
      logger.log(Level.INFO, "Proxy required credentials; using '" + proxyUser + "' (username) and *** (hidden password)");
      CredentialsProvider credsProvider = new BasicCredentialsProvider();
      credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword));
      httpClientBuilder.setDefaultCredentialsProvider(credsProvider);					
httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
return httpClientBuilder.build();

代码示例来源:origin: igniterealtime/Openfire

final HttpHost target = HttpHost.create(crowdServer.toString());
final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setDefaultMaxPerRoute(crowdProps.getHttpMaxConnections());
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
  new AuthScope(target.getHostName(), target.getPort()),
  new UsernamePasswordCredentials(crowdProps.getApplicationName(), crowdProps.getApplicationPassword()));
final AuthCache authCache = new BasicAuthCache();
authCache.put(target, new BasicScheme());
  proxyString = proxyHost.toString();
  if (StringUtils.isNotBlank(crowdProps.getHttpProxyUsername()) || StringUtils.isNotBlank(crowdProps.getHttpProxyPassword())) {
    credentialsProvider.setCredentials(new AuthScope(crowdProps.getHttpProxyHost(), crowdProps.getHttpProxyPort()),
      new UsernamePasswordCredentials(crowdProps.getHttpProxyUsername(), crowdProps.getHttpProxyPassword()));
client = HttpClientBuilder.create()
  .setConnectionManager(connectionManager)
  .setDefaultCredentialsProvider(credentialsProvider)
  .setRoutePlanner(httpRoutePlanner)
  .build();

代码示例来源:origin: kaaproject/kaa

this.configuration = configuration;
this.executor = Executors.newFixedThreadPool(configuration.getConnectionPoolSize());
target = new HttpHost(configuration.getHost(),
  configuration.getPort(), configuration.getSsl() ? "https" : "http");
HttpClientBuilder builder = HttpClients.custom();
if (configuration.getUsername() != null && configuration.getPassword() != null) {
 LOG.info("Adding basic auth credentials provider");
 CredentialsProvider credsProvider = new BasicCredentialsProvider();
 credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()),
   new UsernamePasswordCredentials(configuration.getUsername(), configuration.getPassword())
 );
 builder.setDefaultCredentialsProvider(credsProvider);
  sslBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
  SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslBuilder.build());
  builder.setSSLSocketFactory(sslsf);
 } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) {
  LOG.error("Failed to init socket factory {}", ex.getMessage(), ex);
cm.setDefaultMaxPerRoute(configuration.getConnectionPoolSize());
cm.setMaxTotal(configuration.getConnectionPoolSize());
builder.setConnectionManager(cm);
this.client = builder.build();

代码示例来源:origin: dropwizard/dropwizard

.setRequestExecutor(new InstrumentedHttpRequestExecutor(metricRegistry, metricNameStrategy, name))
  .setConnectionManager(manager)
  .setDefaultRequestConfig(requestConfig)
  .setDefaultSocketConfig(socketConfig)
  .setConnectionReuseStrategy(reuseStrategy)
final HttpHost httpHost = new HttpHost(proxy.getHost(), proxy.getPort(), proxy.getScheme());
builder.setRoutePlanner(new NonProxyListProxyRoutePlanner(httpHost, proxy.getNonProxyHosts()));
if (auth != null) {
  if (credentialsProvider == null) {
    credentialsProvider = new BasicCredentialsProvider();
  AuthScope authScope = new AuthScope(httpHost, auth.getRealm(), auth.getAuthScheme());
  credentialsProvider.setCredentials(authScope, credentials);

代码示例来源:origin: wiztools/rest-client

final HttpClientBuilder hcBuilder = HttpClientBuilder.create();
final RequestConfig.Builder rcBuilder = RequestConfig.custom();
final RequestBuilder reqBuilder = RequestBuilder.create(
hcBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
proxy.acquire();
if (proxy.isEnabled()) {
  final HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort(), "http");
  if (proxy.isAuthEnabled()) {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
        new AuthScope(proxy.getHost(), proxy.getPort()),
        new UsernamePasswordCredentials(proxy.getUsername(), new String(proxy.getPassword())));
    hcBuilder.setDefaultCredentialsProvider(credsProvider);
  hcBuilder.setProxy(proxyHost);
    String realm = StringUtil.isEmpty(a.getRealm()) ? AuthScope.ANY_REALM : a.getRealm();
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
        new AuthScope(host, urlPort, realm),
        new UsernamePasswordCredentials(uid, pwd));
    hcBuilder.setDefaultCredentialsProvider(credsProvider);
    String pwd = new String(a.getPassword());

代码示例来源:origin: Impetus/Kundera

schemeRegistry.register(new Scheme("http", Integer.parseInt(port), ssf));
PoolingClientConnectionManager ccm = new PoolingClientConnectionManager(schemeRegistry);
httpClient = new DefaultHttpClient(ccm);
httpHost = new HttpHost(hosts[0], Integer.parseInt(port), "http");
  ((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials(
      new AuthScope(hosts[0], Integer.parseInt(port)),
      new UsernamePasswordCredentials(userName, password));
((DefaultHttpClient) httpClient).addRequestInterceptor(new HttpRequestInterceptor()
((DefaultHttpClient) httpClient).addResponseInterceptor(new HttpResponseInterceptor()

代码示例来源:origin: jamesagnew/hapi-fhir

public static void main(String[] args) {
 /*
  * This is out ot date - Just keeping
  * it in case it's helpful...
  */
 final String authUser = "username"; 
 final String authPassword = "password"; 
 CredentialsProvider credsProvider = new BasicCredentialsProvider();
 credsProvider.setCredentials(new AuthScope("10.10.10.10", 8080),
    new UsernamePasswordCredentials(authUser, authPassword)); 
 HttpHost myProxy = new HttpHost("10.10.10.10", 8080);
 
 
 HttpClientBuilder clientBuilder = HttpClientBuilder.create(); 
 clientBuilder
   .setProxy(myProxy)
   .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
   .setDefaultCredentialsProvider(credsProvider) 
   .disableCookieManagement(); 
 CloseableHttpClient httpClient = clientBuilder.build();
 
 FhirContext ctx = FhirContext.forDstu2(); 
 String serverBase = "http://spark.furore.com/fhir/"; 
 ctx.getRestfulClientFactory().setHttpClient(httpClient); 
 IGenericClient client = ctx.newRestfulGenericClient(serverBase); 
 IdDt id = new IdDt("Patient", "123"); 
 client.read(Patient.class, id); 
 
}

代码示例来源:origin: apache/nifi

private HttpClient openConnection() throws IOException {
  HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
  if (sslContextService != null) {
    try {
      SSLContext sslContext = getSslSocketFactory(sslContextService);
      httpClientBuilder.setSSLContext(sslContext);
    } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | UnrecoverableKeyException | KeyManagementException e) {
      throw new IOException(e);
    }
  }
  if (credentialsService != null) {
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(null, -1, null),
      new KerberosKeytabCredentials(credentialsService.getPrincipal(), credentialsService.getKeytab()));
    httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
    Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider> create()
      .register(AuthSchemes.SPNEGO, new KerberosKeytabSPNegoAuthSchemeProvider()).build();
    httpClientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
  }
  RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
  requestConfigBuilder.setConnectTimeout(connectTimeout);
  requestConfigBuilder.setConnectionRequestTimeout(connectTimeout);
  requestConfigBuilder.setSocketTimeout(connectTimeout);
  httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
  return httpClientBuilder.build();
}

代码示例来源:origin: org.wso2.bps/org.wso2.bps.integration.common.clients

private DefaultHttpClient getHttpClient() {
    HttpHost target = new HttpHost(hostname, port, "http");
    DefaultHttpClient httpClient = new DefaultHttpClient();
    httpClient.getCredentialsProvider().setCredentials
        (new AuthScope(target.getHostName(), target.getPort()),
         new UsernamePasswordCredentials(USERNAME, PASSWORD));
    return httpClient;
  }
}

代码示例来源:origin: jamesagnew/hapi-fhir

public synchronized HttpClient getNativeHttpClient() {
  if (myHttpClient == null) {
    //FIXME potential resoource leak
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000,
        TimeUnit.MILLISECONDS);
    connectionManager.setMaxTotal(getPoolMaxTotal());
    connectionManager.setDefaultMaxPerRoute(getPoolMaxPerRoute());
    // @formatter:off
    //TODO: Use of a deprecated method should be resolved.
    RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(getSocketTimeout())
        .setConnectTimeout(getConnectTimeout()).setConnectionRequestTimeout(getConnectionRequestTimeout())
        .setStaleConnectionCheckEnabled(true).setProxy(myProxy).build();
    HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connectionManager)
        .setDefaultRequestConfig(defaultRequestConfig).disableCookieManagement();
    if (myProxy != null && StringUtils.isNotBlank(getProxyUsername()) && StringUtils.isNotBlank(getProxyPassword())) {
      CredentialsProvider credsProvider = new BasicCredentialsProvider();
      credsProvider.setCredentials(new AuthScope(myProxy.getHostName(), myProxy.getPort()),
          new UsernamePasswordCredentials(getProxyUsername(), getProxyPassword()));
      builder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
      builder.setDefaultCredentialsProvider(credsProvider);
    }
    myHttpClient = builder.build();
    // @formatter:on
  }
  return myHttpClient;
}

代码示例来源:origin: iSafeBlue/TrackRay

private CloseableHttpClient getCloseableHttpClient(HttpProxy proxy) {
  if (null != proxy && proxy.isProxy()) {
    HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort());
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    if (null != proxy.getUser() && null != proxy.getPassword()) {
      credentialsProvider.setCredentials(new AuthScope(proxy.getHost(), proxy.getPort()),
          new UsernamePasswordCredentials(proxy.getUser(), proxy.getPassword()));
    }
    return HttpClients.custom().setProxy(proxyHost).setDefaultCredentialsProvider(credentialsProvider).build();
  }
  return HttpClients.custom().setConnectionManager(connManager).build();
}

代码示例来源:origin: apache/geode

/**
 * this handles rest calls. each request creates a different httpClient object
 */
public HttpResponse doRequest(HttpRequestBase request, String username, String password) {
 HttpClientBuilder clientBuilder = HttpClients.custom();
 HttpClientContext clientContext = HttpClientContext.create();
 // configures the clientBuilder and clientContext
 if (username != null) {
  CredentialsProvider credsProvider = new BasicCredentialsProvider();
  credsProvider.setCredentials(new AuthScope(bindAddress, restPort),
    new UsernamePasswordCredentials(username, password));
  clientBuilder.setDefaultCredentialsProvider(credsProvider);
 }
 try {
  if (useSsl) {
   SSLContext ctx = SSLContext.getInstance("TLS");
   ctx.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()},
     new SecureRandom());
   clientBuilder.setSSLContext(ctx);
   clientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier());
  }
  return clientBuilder.build().execute(host, request, clientContext);
 } catch (Exception e) {
  throw new RuntimeException(e.getMessage(), e);
 }
}

代码示例来源:origin: h2oai/h2o-2

protected HttpClient createHttpClient(GoogleAnalyticsConfig config) {
 ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager();
 connManager.setDefaultMaxPerRoute(getDefaultMaxPerRoute(config));
 BasicHttpParams params = new BasicHttpParams();
 if (isNotEmpty(config.getUserAgent())) {
  params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgent());
 }
 if (isNotEmpty(config.getProxyHost())) {
  params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(config.getProxyHost(), config.getProxyPort()));
 }
 DefaultHttpClient client = new DefaultHttpClient(connManager, params);
 if (isNotEmpty(config.getProxyUserName())) {
  BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()),
      new UsernamePasswordCredentials(config.getProxyUserName(), config.getProxyPassword()));
  client.setCredentialsProvider(credentialsProvider);
 }
 return client;
}

代码示例来源:origin: stackoverflow.com

RestTemplate restTemplate = new RestTemplate();

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials( new AuthScope("proxyHost", "proxyPort"), new UsernamePasswordCredentials("proxyUser", "proxyPass") );
HttpClientBuilder clientBuilder = HttpClientBuilder.create();

clientBuilder.useSystemProperties();
clientBuilder.setProxy(new HttpHost("proxyHost", "proxyPort"));
clientBuilder.setDefaultCredentialsProvider(credsProvider);
clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());

CloseableHttpClient client = clientBuilder.build();

HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setHttpClient(client);

restTemplate.setRequestFactory(factory);

代码示例来源:origin: pentaho/pentaho-kettle

HttpClientContext localContext = null;
try {
 HttpHost target = new HttpHost( host, port, schema );
 CredentialsProvider credsProvider = new BasicCredentialsProvider();
 credsProvider.setCredentials(
  new AuthScope( target.getHostName(), target.getPort() ),
  new UsernamePasswordCredentials( user, password ) );

代码示例来源:origin: GoogleContainerTools/jib

private static void addProxyCredentials(ApacheHttpTransport transport, String protocol) {
 Preconditions.checkArgument(protocol.equals("http") || protocol.equals("https"));
 String proxyHost = System.getProperty(protocol + ".proxyHost");
 String proxyUser = System.getProperty(protocol + ".proxyUser");
 String proxyPassword = System.getProperty(protocol + ".proxyPassword");
 if (proxyHost == null || proxyUser == null || proxyPassword == null) {
  return;
 }
 String defaultProxyPort = protocol.equals("http") ? "80" : "443";
 int proxyPort = Integer.parseInt(System.getProperty(protocol + ".proxyPort", defaultProxyPort));
 DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient();
 httpClient
   .getCredentialsProvider()
   .setCredentials(
     new AuthScope(proxyHost, proxyPort),
     new UsernamePasswordCredentials(proxyUser, proxyPassword));
}

相关文章