当前位置: 首页>>代码示例>>Java>>正文


Java HostnameVerifier类代码示例

本文整理汇总了Java中javax.net.ssl.HostnameVerifier的典型用法代码示例。如果您正苦于以下问题:Java HostnameVerifier类的具体用法?Java HostnameVerifier怎么用?Java HostnameVerifier使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


HostnameVerifier类属于javax.net.ssl包,在下文中一共展示了HostnameVerifier类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: disableCertificateValidation

import javax.net.ssl.HostnameVerifier; //导入依赖的package包/类
public static void disableCertificateValidation() 
{
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] 
    { 
      new TrustAllManager() 
    };

    // Ignore differences between given hostname and certificate hostname
    HostnameVerifier hv = new TrustAllHostnameVerifier();
    
    // Install the all-trusting trust manager
    try 
    {
      SSLContext sc = SSLContext.getInstance("SSL");
      sc.init(null, trustAllCerts, new SecureRandom());
      HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
      HttpsURLConnection.setDefaultHostnameVerifier(hv);
    } catch (Exception e) {}
}
 
开发者ID:johndavidbustard,项目名称:RoughWorld,代码行数:21,代码来源:WebClient.java

示例2: getHostnameVerifier

import javax.net.ssl.HostnameVerifier; //导入依赖的package包/类
/**
 * 主机名校验方法,请把”192.168.0.10”换成你们公司的主机IP:
 */
public static HostnameVerifier getHostnameVerifier() {
    return new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            if ("192.168.0.10".equals(hostname)) {
                return true;
            } else {
                HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
                return hv.verify(hostname, session);
            }
        }
    };
}
 
开发者ID:guiying712,项目名称:AndroidModulePattern,代码行数:17,代码来源:HttpsUtils.java

示例3: getOkHttpClient

import javax.net.ssl.HostnameVerifier; //导入依赖的package包/类
public static synchronized OkHttpClient getOkHttpClient() {
  if (okHttpClient == null) {
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.connectTimeout(90, TimeUnit.SECONDS);
    builder.sslSocketFactory(createSSLSocketFactory());
    builder.hostnameVerifier(new HostnameVerifier() {
      @Override public boolean verify(String hostname, SSLSession session) {
        return true;
      }
    });
    if (BuildConfig.DEBUG) {
      // Log信息拦截器
      HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
      loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);//这里可以选择拦截级别

      //设置 Debug Log 模式
      builder.addInterceptor(loggingInterceptor);
    }
    okHttpClient = builder.build();
  }
  return okHttpClient;
}
 
开发者ID:Lingzh0ng,项目名称:ITSM,代码行数:23,代码来源:GlideUtils.java

示例4: getHostnameVerifier

import javax.net.ssl.HostnameVerifier; //导入依赖的package包/类
/**
 * Creates the {@code HostnameVerifier} given the provided {@code verification}.
 *
 * @param verification The intended hostname verification action.
 * @return A verifier for the request verification.
 * @throws IllegalArgumentException if the provided verification cannot be handled.
 */
HostnameVerifier getHostnameVerifier(HostnameVerification verification) {
  // Normally, the configuration logic would give us a default of STRICT if it was not
  // provided by the user. It's easy for us to do a double-check.
  if (verification == null) {
    verification = HostnameVerification.STRICT;
  }
  switch (verification) {
  case STRICT:
    return SSLConnectionSocketFactory.getDefaultHostnameVerifier();
  case NONE:
    return NoopHostnameVerifier.INSTANCE;
  default:
    throw new IllegalArgumentException("Unhandled HostnameVerification: "
        + hostnameVerification);
  }
}
 
开发者ID:apache,项目名称:calcite-avatica,代码行数:24,代码来源:AvaticaCommonsHttpClientImpl.java

示例5: Address

import javax.net.ssl.HostnameVerifier; //导入依赖的package包/类
public Address(String uriHost, int uriPort, Dns dns, SocketFactory socketFactory,
    @Nullable SSLSocketFactory sslSocketFactory, @Nullable HostnameVerifier hostnameVerifier,
    @Nullable CertificatePinner certificatePinner, Authenticator proxyAuthenticator,
    @Nullable Proxy proxy, List<Protocol> protocols, List<ConnectionSpec> connectionSpecs,
    ProxySelector proxySelector) {
  this.url = new HttpUrl.Builder()
      .scheme(sslSocketFactory != null ? "https" : "http")
      .host(uriHost)
      .port(uriPort)
      .build();

  if (dns == null) throw new NullPointerException("dns == null");
  this.dns = dns;

  if (socketFactory == null) throw new NullPointerException("socketFactory == null");
  this.socketFactory = socketFactory;

  if (proxyAuthenticator == null) {
    throw new NullPointerException("proxyAuthenticator == null");
  }
  this.proxyAuthenticator = proxyAuthenticator;

  if (protocols == null) throw new NullPointerException("protocols == null");
  this.protocols = Util.immutableList(protocols);

  if (connectionSpecs == null) throw new NullPointerException("connectionSpecs == null");
  this.connectionSpecs = Util.immutableList(connectionSpecs);

  if (proxySelector == null) throw new NullPointerException("proxySelector == null");
  this.proxySelector = proxySelector;

  this.proxy = proxy;
  this.sslSocketFactory = sslSocketFactory;
  this.hostnameVerifier = hostnameVerifier;
  this.certificatePinner = certificatePinner;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:37,代码来源:Address.java

示例6: allowAllSSL

import javax.net.ssl.HostnameVerifier; //导入依赖的package包/类
public static void allowAllSSL() {
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
        public boolean verify(String arg0, SSLSession arg1) {
            return true;
        }
    });
    SSLContext context = null;
    if (trustManagers == null) {
        trustManagers = new TrustManager[]{new HttpsTrustManager()};
    }
    try {
        context = SSLContext.getInstance("TLS");
        context.init(null, trustManagers, new SecureRandom());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e2) {
        e2.printStackTrace();
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:21,代码来源:HttpsTrustManager.java

示例7: prepare

import javax.net.ssl.HostnameVerifier; //导入依赖的package包/类
@Override public void prepare(Benchmark benchmark) {
  super.prepare(benchmark);
  client = new OkHttpClient.Builder()
      .protocols(benchmark.protocols)
      .build();

  if (benchmark.tls) {
    SslClient sslClient = SslClient.localhost();
    SSLSocketFactory socketFactory = sslClient.socketFactory;
    HostnameVerifier hostnameVerifier = new HostnameVerifier() {
      @Override public boolean verify(String s, SSLSession session) {
        return true;
      }
    };
    client = new OkHttpClient.Builder()
        .sslSocketFactory(socketFactory, sslClient.trustManager)
        .hostnameVerifier(hostnameVerifier)
        .build();
  }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:21,代码来源:OkHttp.java

示例8: getHostnameVerifier

import javax.net.ssl.HostnameVerifier; //导入依赖的package包/类
public static HostnameVerifier getHostnameVerifier(String verifier)
  throws GeneralSecurityException, IOException {
  HostnameVerifier hostnameVerifier;
  if (verifier.equals("DEFAULT")) {
    hostnameVerifier = SSLHostnameVerifier.DEFAULT;
  } else if (verifier.equals("DEFAULT_AND_LOCALHOST")) {
    hostnameVerifier = SSLHostnameVerifier.DEFAULT_AND_LOCALHOST;
  } else if (verifier.equals("STRICT")) {
    hostnameVerifier = SSLHostnameVerifier.STRICT;
  } else if (verifier.equals("STRICT_IE6")) {
    hostnameVerifier = SSLHostnameVerifier.STRICT_IE6;
  } else if (verifier.equals("ALLOW_ALL")) {
    hostnameVerifier = SSLHostnameVerifier.ALLOW_ALL;
  } else {
    throw new GeneralSecurityException("Invalid hostname verifier: " +
                                       verifier);
  }
  return hostnameVerifier;
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:20,代码来源:SSLFactory.java

示例9: getInstance

import javax.net.ssl.HostnameVerifier; //导入依赖的package包/类
public static OkHttpManager getInstance() {
    if (sOkHttpManager == null) {
        synchronized (OkHttpManager.class) {
            if (sOkHttpManager == null) {
                sOkHttpManager = new OkHttpManager();
                sOkHttpClient = new OkHttpClient.Builder()
                        .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                        .connectTimeout(20, TimeUnit.SECONDS)
                        .readTimeout(20, TimeUnit.SECONDS)
                        .writeTimeout(20, TimeUnit.SECONDS)
                        .retryOnConnectionFailure(true)
                        .addNetworkInterceptor(new HttpInterceptor(SPUtils.getUserAgent()))
                        .hostnameVerifier(new HostnameVerifier() {
                            @Override
                            public boolean verify(String hostname, SSLSession session) {
                                return true;
                            }
                        })
                        .sslSocketFactory(TrustSSLContext.getSSLSocketFactory())

                        .build();
            }
        }
    }
    return sOkHttpManager;
}
 
开发者ID:Zyj163,项目名称:yyox,代码行数:27,代码来源:OkHttpManager.java

示例10: JedisFactory

import javax.net.ssl.HostnameVerifier; //导入依赖的package包/类
public JedisFactory(final URI uri, final int connectionTimeout, final int soTimeout, final String clientName,
		final boolean ssl, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters,
		final HostnameVerifier hostnameVerifier) {
	if (!JedisURIHelper.isValid(uri)) {
		throw new InvalidURIException(
				String.format("Cannot open Redis connection due invalid URI. %s", uri.toString()));
	}

	this.hostAndPort.set(new HostAndPort(uri.getHost(), uri.getPort()));
	this.connectionTimeout = connectionTimeout;
	this.soTimeout = soTimeout;
	this.password = JedisURIHelper.getPassword(uri);
	this.database = JedisURIHelper.getDBIndex(uri);
	this.clientName = clientName;
	this.ssl = ssl;
	this.sslSocketFactory = sslSocketFactory;
	this.sslParameters = sslParameters;
	this.hostnameVerifier = hostnameVerifier;
}
 
开发者ID:qq1588518,项目名称:JRediClients,代码行数:20,代码来源:JedisFactory.java

示例11: testHostnameVerification

import javax.net.ssl.HostnameVerifier; //导入依赖的package包/类
@Test public void testHostnameVerification() throws Exception {
  AvaticaCommonsHttpClientImpl client = mock(AvaticaCommonsHttpClientImpl.class);
  // Call the real method
  when(client.getHostnameVerifier(nullable(HostnameVerification.class)))
      .thenCallRealMethod();

  // No verification should give the default (strict) verifier
  HostnameVerifier actualVerifier = client.getHostnameVerifier(null);
  assertNotNull(actualVerifier);
  assertTrue(actualVerifier instanceof DefaultHostnameVerifier);

  actualVerifier = client.getHostnameVerifier(HostnameVerification.STRICT);
  assertNotNull(actualVerifier);
  assertTrue(actualVerifier instanceof DefaultHostnameVerifier);

  actualVerifier = client.getHostnameVerifier(HostnameVerification.NONE);
  assertNotNull(actualVerifier);
  assertTrue(actualVerifier instanceof NoopHostnameVerifier);
}
 
开发者ID:apache,项目名称:calcite-avatica,代码行数:20,代码来源:AvaticaCommonsHttpClientImplTest.java

示例12: setupNodeIfNotExist

import javax.net.ssl.HostnameVerifier; //导入依赖的package包/类
public JedisPool setupNodeIfNotExist(HostAndPort node, boolean ssl, SSLSocketFactory sslSocketFactory,
		SSLParameters sslParameters, HostnameVerifier hostnameVerifier) {
	w.lock();
	try {
		String nodeKey = getNodeKey(node);
		JedisPool existingPool = nodes.get(nodeKey);
		if (existingPool != null)
			return existingPool;

		JedisPool nodePool = new JedisPool(poolConfig, node.getHost(), node.getPort(), connectionTimeout, soTimeout,
				password, 0, null, ssl, sslSocketFactory, sslParameters, hostnameVerifier);
		nodes.put(nodeKey, nodePool);
		return nodePool;
	} finally {
		w.unlock();
	}
}
 
开发者ID:qq1588518,项目名称:JRediClients,代码行数:18,代码来源:JedisClusterInfoCache.java

示例13: connectWithShardInfoAndCustomHostnameVerifier

import javax.net.ssl.HostnameVerifier; //导入依赖的package包/类
/**
 * Tests opening an SSL/TLS connection to redis with a custom hostname
 * verifier.
 */
@Test
public void connectWithShardInfoAndCustomHostnameVerifier() {
  final URI uri = URI.create("rediss://localhost:6390");
  final SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
  final SSLParameters sslParameters = new SSLParameters();

  HostnameVerifier hostnameVerifier = new BasicHostnameVerifier();
  JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier);
  shardInfo.setPassword("foobared");

  Jedis jedis = new Jedis(shardInfo);
  jedis.get("foo");
  jedis.disconnect();
  jedis.close();
}
 
开发者ID:qq1588518,项目名称:JRediClients,代码行数:20,代码来源:SSLJedisTest.java

示例14: triggerHttpGetWithCustomSSL

import javax.net.ssl.HostnameVerifier; //导入依赖的package包/类
public static String triggerHttpGetWithCustomSSL(String requestUrl) {
	String result = null;
	try {
		SSLContextBuilder builder = new SSLContextBuilder();
		builder.loadTrustMaterial(null, new TrustSelfSignedStrategy() {
			public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
				return true;
			}
		});
		@SuppressWarnings("deprecation")
		HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

		SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(), hostnameVerifier);

		CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

		HttpGet httpGet = new HttpGet("https://" + requestUrl);
		CloseableHttpResponse response = httpclient.execute(httpGet);
		try {
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity entity = response.getEntity();
				result = EntityUtils.toString(entity);
				log.debug("Received response: " + result);
			} else {
				log.error("Request not successful. StatusCode was " + response.getStatusLine().getStatusCode());
			}
		} finally {
			response.close();
		}
	} catch (IOException | KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
		log.error("Error executing the request.", e);
	}
	return result;
}
 
开发者ID:hotstepper13,项目名称:alexa-gira-bridge,代码行数:35,代码来源:Util.java

示例15: connectWithShardInfoAndCustomHostnameVerifierByIpAddress

import javax.net.ssl.HostnameVerifier; //导入依赖的package包/类
/**
 * Tests opening an SSL/TLS connection to redis with a custom hostname
 * verifier. This test should fail because "127.0.0.1" does not match the
 * certificate subject common name and there are no subject alternative names
 * in the certificate.
 */
@Test
public void connectWithShardInfoAndCustomHostnameVerifierByIpAddress() {
  final URI uri = URI.create("rediss://127.0.0.1:6390");
  final SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
  final SSLParameters sslParameters = new SSLParameters();

  HostnameVerifier hostnameVerifier = new BasicHostnameVerifier();
  JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier);
  shardInfo.setPassword("foobared");

  Jedis jedis = new Jedis(shardInfo);
  try {
    jedis.get("foo");
    Assert.fail("The code did not throw the expected JedisConnectionException.");
  } catch (JedisConnectionException e) {
    Assert.assertEquals("The JedisConnectionException does not contain the expected message.",
        "The connection to '127.0.0.1' failed ssl/tls hostname verification.", e.getMessage());
  }

  try {
    jedis.close();
  } catch (Throwable e1) {
    // Expected.
  }
}
 
开发者ID:qq1588518,项目名称:JRediClients,代码行数:32,代码来源:SSLJedisTest.java


注:本文中的javax.net.ssl.HostnameVerifier类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。