本文整理匯總了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();
}
}