本文整理匯總了Java中org.apache.http.ssl.SSLContextBuilder.loadTrustMaterial方法的典型用法代碼示例。如果您正苦於以下問題:Java SSLContextBuilder.loadTrustMaterial方法的具體用法?Java SSLContextBuilder.loadTrustMaterial怎麽用?Java SSLContextBuilder.loadTrustMaterial使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.ssl.SSLContextBuilder
的用法示例。
在下文中一共展示了SSLContextBuilder.loadTrustMaterial方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: init
import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
public void init() {
try {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
/* 配置同時支持 HTTP 和 HTPPS */
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslsf).build();
/* 初始化連接管理器 */
poolConnManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
poolConnManager.setMaxTotal(maxTotal);
poolConnManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
requestConfig = RequestConfig.custom().setConnectionRequestTimeout(connectionRequestTimeout)
.setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
httpClient = getConnection();
log.info("HttpConnectionManager初始化完成...");
} catch (Exception e) {
log.error("error", e);
}
}
示例2: createAllTrustingClient
import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的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;
}
示例3: triggerHttpGetWithCustomSSL
import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的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;
}
示例4: init
import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
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: buildHttpClient
import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
/**
* This creates a HTTP client instance for connecting the IFTTT server.
*
* @return the HTTP client instance
*/
private CloseableHttpClient buildHttpClient ()
{
if ( configuration.isIftttIgnoreServerCertificate() ) {
try {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(new TrustStrategy() {
@Override
public boolean isTrusted (X509Certificate[] chain_, String authType_) throws CertificateException
{
return true;
}
});
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
}
catch (Exception ex) {
LOG.error(ex);
// This should never happen, but we have to handle it
throw new RuntimeException(ex);
}
}
else {
return HttpClients.createDefault();
}
}
示例6: getHttpsClient
import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的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();
}
示例7: configureInsecureSSL
import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
/**
* Allow SSL connections to utilize untrusted certificates
*
* @param httpClientBuilder
* @throws MojoExecutionException
*/
private void configureInsecureSSL(HttpClientBuilder httpClientBuilder) throws MojoExecutionException {
try {
SSLContextBuilder sslBuilder = new SSLContextBuilder();
// Accept all Certificates
sslBuilder.loadTrustMaterial(null, AcceptAllTrustStrategy.INSTANCE);
SSLContext sslContext = sslBuilder.build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
NoopHostnameVerifier.INSTANCE);
httpClientBuilder.setSSLSocketFactory(sslsf);
} catch (Exception e) {
throw new MojoExecutionException("Error setting insecure SSL configuration", e);
}
}
示例8: setTrustedKeystore
import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
public void setTrustedKeystore(final RestSettings.Keystore keystore) throws Exception {
final SSLContextBuilder ssl = SSLContexts.custom();
final String path = keystore.getPath();
final String password = keystore.getPassword();
final URL resource = HttpClientFactoryBean.class.getClassLoader().getResource(path);
if (resource == null) {
throw new FileNotFoundException(format("Keystore [%s] not found.", path));
}
try {
ssl.loadTrustMaterial(resource, password == null ? null : password.toCharArray());
builder.setSSLSocketFactory(new SSLConnectionSocketFactory(ssl.build(), getDefaultHostnameVerifier()));
} catch (final Exception e) {
LOG.error("Error loading keystore [{}]:", path, e); // log full exception, bean initialization code swallows it
throw e;
}
}
示例9: HttpPosterThread
import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
HttpPosterThread(LinkedBlockingQueue<String> lbq, String url) throws Exception {
this.lbq = lbq;
this.url = url;
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
builder.build());
httpClient = HttpClients.custom().setSSLSocketFactory(
sslsf).build();
//httpClient = HttpClientBuilder.create().build();
httpPost = new HttpPost(url);
cntErr = 0;
}
示例10: createInsecureSslFactory
import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
/**
* This method creates an insecure SSL factory that will trust on self signed certificates.
* For that we use {@link TrustSelfSignedStrategy}.
*/
protected SSLConnectionSocketFactory createInsecureSslFactory() {
try {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(new TrustSelfSignedStrategy());
SSLContext sc = builder.build();
if (acceptAllKindsOfCertificates) {
TrustManager[] trustAllCerts = new TrustManager[1];
TrustManager tm = new TrustAllManager();
trustAllCerts[0] = tm;
sc.init(null, trustAllCerts, null);
HostnameVerifier hostnameVerifier = createInsecureHostNameVerifier();
return new SSLConnectionSocketFactory(sc, hostnameVerifier);
}
return new SSLConnectionSocketFactory(sc);
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
throw new ApacheCloudStackClientRuntimeException(e);
}
}
示例11: setTrustedKeystore
import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
public void setTrustedKeystore(final Keystore keystore) throws Exception {
final SSLContextBuilder ssl = SSLContexts.custom();
final String path = keystore.getPath();
final String password = keystore.getPassword();
final URL resource = HttpClientFactoryBean.class.getClassLoader().getResource(path);
if (resource == null) {
throw new FileNotFoundException(format("Keystore [%s] not found.", path));
}
try {
ssl.loadTrustMaterial(resource, password == null ? null : password.toCharArray());
builder.setSSLSocketFactory(new SSLConnectionSocketFactory(ssl.build(), getDefaultHostnameVerifier()));
} catch (final Exception e) {
LOG.error("Error loading keystore [{}]:", path, e); // log full exception, bean initialization code swallows it
throw e;
}
}
示例12: TankHttpClient4
import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
/**
* no-arg constructor for client
*/
public TankHttpClient4() {
try {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
sslsf = new SSLConnectionSocketFactory(builder.build(), NoopHostnameVerifier.INSTANCE);
} catch (Exception e) {
LOG.error("Error setting accept all: " + e, e);
}
httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
requestConfig = RequestConfig.custom().setSocketTimeout(30000)
.setConnectTimeout(30000)
.setCircularRedirectsAllowed(true)
.setAuthenticationEnabled(true)
.setRedirectsEnabled(true)
.setCookieSpec(CookieSpecs.STANDARD)
.setMaxRedirects(100).build();
// Make sure the same context is used to execute logically related
// requests
context = HttpClientContext.create();
context.setCredentialsProvider(new BasicCredentialsProvider());
context.setCookieStore(new BasicCookieStore());
context.setRequestConfig(requestConfig);
}
示例13: getHttpClient
import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的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;
}
示例14: getHttpClient
import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的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();
}
示例15: getAuthenticatedClient
import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
public CloseableHttpClient getAuthenticatedClient() {
try {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
builder.build(), NoopHostnameVerifier.INSTANCE);
return HttpClients.custom()
.setSSLSocketFactory(sslsf)
.setDefaultCredentialsProvider(getCredentialsProvider())
.build();
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) {
Logger.getLogger(AuthHandler.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}