本文整理汇总了Java中org.apache.http.conn.ssl.TrustStrategy类的典型用法代码示例。如果您正苦于以下问题:Java TrustStrategy类的具体用法?Java TrustStrategy怎么用?Java TrustStrategy使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TrustStrategy类属于org.apache.http.conn.ssl包,在下文中一共展示了TrustStrategy类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createAllTrustingClient
import org.apache.http.conn.ssl.TrustStrategy; //导入依赖的package包/类
private static HttpClient createAllTrustingClient() throws RestClientException {
HttpClient httpclient = null;
try {
final SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial((TrustStrategy) (X509Certificate[] chain, String authType) -> true);
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
builder.build());
httpclient = HttpClients
.custom()
.setSSLSocketFactory(sslsf)
.setMaxConnTotal(1000)
.setMaxConnPerRoute(1000)
.build();
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
throw new RestClientException("Cannot create all SSL certificates trusting client", e);
}
return httpclient;
}
示例2: createSecurityRestTemplate
import org.apache.http.conn.ssl.TrustStrategy; //导入依赖的package包/类
/**
* Creates the security rest template.
*
* @throws KeyManagementException the key management exception
* @throws NoSuchAlgorithmException the no such algorithm exception
* @throws KeyStoreException the key store exception
*/
private void createSecurityRestTemplate() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException{
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory();
HttpClient httpClient = HttpClientBuilder.create()
.disableCookieManagement()
.useSystemProperties()
.setSSLSocketFactory(csf)
.build();
requestFactory.setHttpClient(httpClient);
this.restTemplate = new RestTemplate(requestFactory);
}
示例3: getSSLSocketFactory
import org.apache.http.conn.ssl.TrustStrategy; //导入依赖的package包/类
/**
* 获取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: getCustomClient
import org.apache.http.conn.ssl.TrustStrategy; //导入依赖的package包/类
/**
* custom http client for server with SSL errors
*
* @return
*/
public final CloseableHttpClient getCustomClient() {
try {
HttpClientBuilder builder = HttpClientBuilder.create().useSystemProperties();
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null,
(TrustStrategy) (X509Certificate[] arg0, String arg1) -> true).build();
builder.setSSLContext(sslContext);
HostnameVerifier hostnameVerifier = new NoopHostnameVerifier();
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory)
.build();
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
builder.setConnectionManager(connMgr);
return builder.build();
} catch (Exception ex) {
LOG.log(Level.SEVERE, ex.getMessage(), ex);
}
return getSystemClient();
}
示例5: buildClient
import org.apache.http.conn.ssl.TrustStrategy; //导入依赖的package包/类
@SuppressWarnings("deprecation")
static CloseableHttpClient buildClient(boolean ignoreSSL) throws Exception {
SSLSocketFactory sslsf = new SSLSocketFactory(new TrustStrategy() {
public boolean isTrusted(
final X509Certificate[] chain, String authType) throws CertificateException {
// Oh, I am easy...
return true;
}
});
if (ignoreSSL) {
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} else {
return HttpClients.createDefault();
}
}
示例6: createSSLClientDefault
import org.apache.http.conn.ssl.TrustStrategy; //导入依赖的package包/类
private CloseableHttpClient createSSLClientDefault() {
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
// 信任所有
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
if (cert != null) {
for (X509Certificate chainCert : chain) {
if (chainCert.equals(cert)) {
return true;
}
}
return false;
}
return true;
}
}).build();
SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslContext);
return HttpClients.custom().setSSLSocketFactory(factory).build();
} catch (GeneralSecurityException e) {
logger.error(String.format("SSL connection create Fail : %s", e.getMessage()));
}
return HttpClients.createDefault();
}
示例7: buildSSLConnectionSocketFactory
import org.apache.http.conn.ssl.TrustStrategy; //导入依赖的package包/类
private SSLConnectionSocketFactory buildSSLConnectionSocketFactory() {
try {
SSLContext sslcontext = SSLContexts.custom()
//忽略掉对服务器端证书的校验
.loadTrustMaterial(new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
return new SSLConnectionSocketFactory(
sslcontext,
new String[]{"TLSv1"},
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
this.log.error(e.getMessage(), e);
}
return null;
}
示例8: createHttpClientWithDisabledSSLCheck
import org.apache.http.conn.ssl.TrustStrategy; //导入依赖的package包/类
private CloseableHttpClient createHttpClientWithDisabledSSLCheck(){
SSLContext sslcontext = null;
try {
sslcontext = SSLContexts.custom()
.loadTrustMaterial(null, new TrustStrategy(){
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
})
.build();
} catch (Exception e) {
throw new RuntimeException("SSL Context can not be created.", e);
}
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext);
return HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
}
示例9: createSSLSocketFactory
import org.apache.http.conn.ssl.TrustStrategy; //导入依赖的package包/类
private SSLSocketFactory createSSLSocketFactory(boolean useKeyStoreToConnect, String keyStorePath,
String keyStorePassword) throws Exception {
//Only load KeyStore when it's needed to connect to IP, SSLContext is fine with KeyStore being null otherwise.
KeyStore trustStore = null;
if (useKeyStoreToConnect) {
trustStore = KeyStoreLoader.loadKeyStore(keyStorePath, keyStorePassword);
}
SSLContext sslContext = SSLContexts.custom()
.useSSL()
.loadTrustMaterial(trustStore, new TrustStrategy() {
//Always trust
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
})
.loadKeyMaterial(trustStore, keyStorePassword.toCharArray())
.setSecureRandom(new java.security.SecureRandom())
.build();
return sslContext.getSocketFactory();
}
示例10: getHttpsClient
import org.apache.http.conn.ssl.TrustStrategy; //导入依赖的package包/类
private CloseableHttpClient getHttpsClient()
{
try
{
RequestConfig config = RequestConfig.custom().setSocketTimeout( 5000 ).setConnectTimeout( 5000 ).build();
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
sslContextBuilder.loadTrustMaterial( null, ( TrustStrategy ) ( x509Certificates, s ) -> true );
SSLConnectionSocketFactory sslSocketFactory =
new SSLConnectionSocketFactory( sslContextBuilder.build(), NoopHostnameVerifier.INSTANCE );
return HttpClients.custom().setDefaultRequestConfig( config ).setSSLSocketFactory( sslSocketFactory )
.build();
}
catch ( Exception e )
{
LOGGER.error( e.getMessage() );
}
return HttpClients.createDefault();
}
示例11: getSSLAcceptingClient
import org.apache.http.conn.ssl.TrustStrategy; //导入依赖的package包/类
/**
* Creates {@link HttpClient} that trusts any SSL certificate
*
* @return prepared HTTP client
*/
protected HttpClient getSSLAcceptingClient() {
final TrustStrategy trustAllStrategy = (final X509Certificate[] chain, final String authType) -> true;
try {
final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, trustAllStrategy).build();
sslContext.init(null, getTrustManager(), new SecureRandom());
final SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
new NoopHostnameVerifier());
return HttpClients.custom().setSSLSocketFactory(connectionSocketFactory).build();
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException error) {
ConsoleUtils.printError(error.getMessage());
throw new IllegalStateException(ErrorMessage.CANNOT_CREATE_SSL_SOCKET, error);
}
}
示例12: getHttpClient
import org.apache.http.conn.ssl.TrustStrategy; //导入依赖的package包/类
private HttpClient getHttpClient() {
CloseableHttpClient httpclient = null;
try {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(keyStore, (TrustStrategy) (trustedCert, nameConstraints) -> true);
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
_logger.error("Failed to create HTTPClient: {}", e);
}
return httpclient;
}
示例13: getHttpClient
import org.apache.http.conn.ssl.TrustStrategy; //导入依赖的package包/类
protected HttpClient getHttpClient(Map<String, Object> options) throws GeneralSecurityException {
SocketConfig socketConfig = SocketConfig.custom()
.setSoKeepAlive(true).build();
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setDefaultSocketConfig(socketConfig);
httpClientBuilder.disableAuthCaching();
httpClientBuilder.disableAutomaticRetries();
if(options.containsKey("sslVerify") && !Boolean.parseBoolean(options.get("sslVerify").toString())) {
log.debug("Disabling all SSL certificate verification.");
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
sslContextBuilder.loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
});
httpClientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier());
httpClientBuilder.setSSLContext(sslContextBuilder.build());
}
return httpClientBuilder.build();
}
示例14: getSSLSocketFactory
import org.apache.http.conn.ssl.TrustStrategy; //导入依赖的package包/类
private SSLConnectionSocketFactory getSSLSocketFactory() {
KeyStore trustStore;
try {
trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
TrustStrategy trustStrategy = new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
};
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
sslContextBuilder.loadTrustMaterial(trustStore, trustStrategy);
sslContextBuilder.useTLS();
SSLContext sslContext = sslContextBuilder.build();
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);
return sslSocketFactory;
} catch (GeneralSecurityException | IOException e) {
System.err.println("SSL Error : " + e.getMessage());
}
return null;
}
示例15: test1
import org.apache.http.conn.ssl.TrustStrategy; //导入依赖的package包/类
public void test1() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, ClientProtocolException, IOException{
// ## SHOULD FIRE OVER-PERMISSIVE HOSTNAME VERIFIER
CloseableHttpClient httpClient = HttpClients.custom().
setHostnameVerifier(new AllowAllHostnameVerifier()).
setSslcontext(new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
// ## SHOULD FIRE OVER-PERMISSIVE TRUST MANAGER
@Override
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException {
return true;
}
}).build()).build();
HttpGet httpget = new HttpGet("https://www.google.com/");
CloseableHttpResponse response = httpClient.execute(httpget);
try {
System.out.println(response.toString());
} finally {
response.close();
}
}