本文整理汇总了Java中org.apache.http.conn.ssl.SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER属性的典型用法代码示例。如果您正苦于以下问题:Java SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER属性的具体用法?Java SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER怎么用?Java SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类org.apache.http.conn.ssl.SSLConnectionSocketFactory
的用法示例。
在下文中一共展示了SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createSSLConnectionSocketFactory
private static SSLConnectionSocketFactory createSSLConnectionSocketFactory(Path path, char[] password, boolean strict, KeystoreType keystoreType) {
try {
SSLContextBuilder builder = SSLContexts.custom();
if (path != null) {
KeyStore trustStore = KeyStore.getInstance(keystoreType.name());
try (InputStream is = Files.newInputStream(path)) {
trustStore.load(is, password);
}
builder.loadTrustMaterial(trustStore);
} else {
builder.loadTrustMaterial(null, new TrustEverythingStrategy());
}
X509HostnameVerifier verifier;
if (strict) {
verifier = SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER;
} else {
verifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
}
return new SSLConnectionSocketFactory(builder.build(), new String[] {"TLSv1", "TLSv1.2"}, null, verifier);
} catch (IOException | GeneralSecurityException ex) {
throw new RuntimeException("Can't create SSL connection factory", ex);
}
}
示例2: triggerHttpGetWithCustomSSL
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;
}
示例3: getSSLSocketFactory
/**
* 获取LayeredConnectionSocketFactory 使用ssl单向认证
*
* @date 2015年7月17日
* @return
*/
private LayeredConnectionSocketFactory getSSLSocketFactory() {
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
// 信任所有
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
return sslsf;
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
}
示例4: init
public void init()
{
try
{
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
Unirest.setHttpClient(httpclient);
}
catch (Exception e)
{
System.out.println("Failed to start server: " + e.toString());
e.printStackTrace();
}
}
示例5: AptlyRestClient
public AptlyRestClient(PrintStream logger, AptlySite site)
{
this.mSite = site;
this.mLogger = logger;
if (Boolean.parseBoolean(site.getEnableSelfSigned())){
try{
SSLContext sslcontext = SSLContexts.custom()
.loadTrustMaterial(null, new TrustSelfSignedStrategy())
.build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
Unirest.setHttpClient(httpclient);
} catch (Exception ex) {
logger.println("Failed to setup ssl self");
}
}
}
示例6: SimpleHttpClient
public SimpleHttpClient(final SSLContext sslContext, final int listenPort, final boolean useCompression) {
HttpClientBuilder builder = HttpClientBuilder.create();
if (!useCompression) {
builder.disableContentCompression();
}
if (sslContext != null) {
SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(
sslContext,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
builder.setSSLSocketFactory(sslConnectionFactory);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", sslConnectionFactory)
.build();
builder.setConnectionManager(new BasicHttpClientConnectionManager(registry));
scheme = "https";
} else {
scheme = "http";
}
this.delegate = builder.build();
this.listenPort = listenPort;
}
示例7: getClient
@NotNull
private HttpClient getClient(@NotNull SVNURL repositoryUrl) {
// TODO: Implement algorithm of resolving necessary enabled protocols (TLSv1 vs SSLv3) instead of just using values from Settings.
SSLContext sslContext = createSslContext(repositoryUrl);
List<String> supportedProtocols = getSupportedSslProtocols();
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, ArrayUtil.toStringArray(supportedProtocols), null,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
// TODO: Seems more suitable here to read timeout values directly from config file - without utilizing SvnAuthenticationManager.
final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
if (haveDataForTmpConfig()) {
IdeHttpClientHelpers.ApacheHttpClient4.setProxyIfEnabled(requestConfigBuilder);
IdeHttpClientHelpers.ApacheHttpClient4.setProxyCredentialsIfEnabled(credentialsProvider);
}
return HttpClients.custom()
.setSSLSocketFactory(socketFactory)
.setDefaultSocketConfig(SocketConfig.custom()
.setSoTimeout(getAuthenticationManager().getReadTimeout(repositoryUrl))
.build())
.setDefaultRequestConfig(requestConfigBuilder
.setConnectTimeout(getAuthenticationManager().getConnectTimeout(repositoryUrl))
.build())
.setDefaultCredentialsProvider(credentialsProvider)
.build();
}
示例8: initInsecureSslHttpClient
private void initInsecureSslHttpClient() throws NoSuchAlgorithmException, KeyManagementException,
KeyStoreException {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (arg0, arg1) -> true).build();
httpClientBuilder.setSslcontext(sslContext);
HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory)
.build();
insecureSslClient = httpClientBuilder.build();
}
示例9: RestClient
public RestClient(String baseUrl) throws Exception {
this.baseUrl = baseUrl;
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
this.httpclient = HttpClients.custom().setUserAgent("r2cloud/0.1 [email protected]").setSSLSocketFactory(sslsf).build();
}
示例10: createSocketFactory
private void createSocketFactory(SSLContext sslContext) {
this.sf = new SSLConnectionSocketFactory(sslContext,
new String[] { "TLSv1" }, null,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER) {
protected void prepareSocket(SSLSocket socket) throws IOException {
if (null == socket)
return;
HttpConnectionAdaptor.setEnableSafeCipherSuites(socket);
}
};
}
示例11: getHttpClient
/**
* @see http://literatejava.com/networks/ignore-ssl-certificate-errors-apache-httpclient-4-4/
* @return
* @throws Exception
*/
public static synchronized HttpClient getHttpClient() throws Exception
{
HttpClientBuilder b = HttpClientBuilder.create();
// setup a Trust Strategy that allows all certificates.
//
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy()
{
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException
{
return true;
}
}).build();
b.setSslcontext(sslContext);
// don't check Hostnames, either.
// -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken
HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
// here's the special part:
// -- need to create an SSL Socket Factory, to use our weakened "trust strategy";
// -- and create a Registry, to register it.
//
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
//Registry<ConnectionSocketFactory> socketFactoryRegistry = ;
// now, we create connection-manager using our Registry.
// -- allows multi-threaded use
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory> create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build());
b.setConnectionManager(connMgr);
// finally, build the HttpClient;
// -- done!
HttpClient client = b.build();
return client;
}
示例12: createHttpClient_AcceptsUntrustedCerts
public HttpClient createHttpClient_AcceptsUntrustedCerts() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
HttpClientBuilder b = HttpClientBuilder.create();
// setup a Trust Strategy that allows all certificates.
//
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build();
b.setSslcontext( sslContext);
// don't check Hostnames, either.
// -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken
HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
// here's the special part:
// -- need to create an SSL Socket Factory, to use our weakened "trust strategy";
// -- and create a Registry, to register it.
//
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory)
.build();
// now, we create connection-manager using our Registry.
// -- allows multi-threaded use
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager( socketFactoryRegistry);
b.setConnectionManager( connMgr);
// finally, build the HttpClient;
// -- done!
HttpClient client = b.build();
return client;
}
示例13: _prepareSSL
private void _prepareSSL(HttpClientBuilder clientBuilder) {
try {
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[]{TRUST_ALL_TRUST_MANAGER}, new SecureRandom());
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
verifyServer ? SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER : SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
clientBuilder.setSSLSocketFactory(sslConnectionSocketFactory);
} catch (NoSuchAlgorithmException | KeyManagementException e) {
throw new IllegalStateException(e);
}
}
示例14: createHttpClientAcceptsUntrustedCertificates
private static HttpClient createHttpClientAcceptsUntrustedCertificates() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
HttpClientBuilder b = HttpClientBuilder.create();
// setup a Trust Strategy that allows all certificates.
//
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build();
b.setSslcontext(sslContext);
// don't check Hostnames, either.
// -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken
HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
// here's the special part:
// -- need to create an SSL Socket Factory, to use our weakened "trust strategy";
// -- and create a Registry, to register it.
//
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory)
.build();
// now, we create connection-manager using our Registry.
// -- allows multi-threaded use
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
b.setConnectionManager(connMgr);
// finally, build the HttpClient;
// -- done!
return b.build();
}
示例15: init
public static void init() throws RuntimeException {
try {
logger.warn(NOTICELINE + " httpUtil init begin " + NOTICELINE);
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
// sslContextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
sslContextBuilder.loadTrustMaterial(null,new TrustAnyTrustManager());
SSLConnectionSocketFactory sslConnectionSocketFactory =
new SSLConnectionSocketFactory(
sslContextBuilder.build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().
register("http", new PlainConnectionSocketFactory()).
register("https", sslConnectionSocketFactory).
build();
logger.warn(NOTICELINE + " SSL context init done " + NOTICELINE);
//init connectionManager , ThreadSafe pooled conMgr
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(registry);
poolingHttpClientConnectionManager.setMaxTotal(30);
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(3);
//init request config. pooltimeout,sotime,contimeout
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(POOL_TIMECOUT).setConnectTimeout(CON_TIMEOUT).setSocketTimeout(SO_TIMEOUT).build();
// begin construct httpclient
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager);
httpClientBuilder.setDefaultRequestConfig(requestConfig);
httpClientBuilder.setRetryHandler(new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (executionCount >= HTTP_RETRY_COUNT) {
return false;
}
if (exception instanceof InterruptedIOException) {
// Timeout
logger.warn("httpUtil retry for InterruptIOException");
return true;
}
if (exception instanceof UnknownHostException) {
// Unknown host
return false;
}
if (exception instanceof SSLException) {
// SSL handshake exception
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent) {
// Retry if the request is considered idempotent
logger.warn("httpUtil retry for idempotent");
return true;
}
return false;
}
});
logger.warn(NOTICELINE + " poolManager , requestconfig init done " + NOTICELINE);
httpclient = httpClientBuilder.build();
logger.warn(NOTICELINE + " httpUtil init done " + NOTICELINE);
} catch (Exception e) {
logger.error(NOTICELINE + "httpclient init fail" + NOTICELINE, e);
throw new RuntimeException(e);
}
}