javax.net.ssl.SSLContext.getDefault()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(337)

本文整理了Java中javax.net.ssl.SSLContext.getDefault()方法的一些代码示例,展示了SSLContext.getDefault()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SSLContext.getDefault()方法的具体详情如下:
包路径:javax.net.ssl.SSLContext
类名称:SSLContext
方法名:getDefault

SSLContext.getDefault介绍

[英]Returns the default SSLContext. The default SSL context can be set with #setDefault. If not, one will be created with SSLContext.getInstance("Default"), which will already be initialized.
[中]返回默认的SSLContext。可以使用#setDefault设置默认SSL上下文。如果没有,将使用SSLContext创建一个。getInstance(“默认”),它将被初始化。

代码示例

代码示例来源:origin: ch.qos.logback/logback-classic

/**
 * Creates a new server using the default SSL context.
 * @param lc logger context for received events
 * @param port port on which the server is to listen
 * @throws NoSuchAlgorithmException if the default SSL context cannot be
 *         created
 */
public SimpleSSLSocketServer(LoggerContext lc, int port) throws NoSuchAlgorithmException {
  this(lc, port, SSLContext.getDefault());
}

代码示例来源:origin: cloudfoundry/uaa

public void configure(HttpsParameters params) {
    try {
      SSLContext c = SSLContext.getDefault();
      SSLEngine engine = c.createSSLEngine();
      params.setNeedClientAuth(false);
      params.setCipherSuites(engine.getEnabledCipherSuites());
      params.setProtocols(engine.getEnabledProtocols());
      SSLParameters defaultSSLParameters = c.getDefaultSSLParameters();
      params.setSSLParameters(defaultSSLParameters);
    } catch (Exception ex) {
      throw new IllegalStateException(ex);
    }
  }
});

代码示例来源:origin: apache/incubator-druid

static void configureSsl(Binder binder)
{
 final SSLContext context;
 try {
  context = SSLContext.getDefault();
 }
 catch (NoSuchAlgorithmException e) {
  throw new RuntimeException(e);
 }
 binder.bind(SSLContext.class).toProvider(Providers.of(context)).in(LazySingleton.class);
}

代码示例来源:origin: apache/zookeeper

@Override
public SSLContext get() {
  try {
    return SSLContext.getDefault();
  } catch (NoSuchAlgorithmException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: Netflix/eureka

private void addSSLConfiguration(ClientBuilder clientBuilder) {
  try {
    if (systemSSL) {
      clientBuilder.sslContext(SSLContext.getDefault());
    } else if (trustStoreFileName != null) {
      KeyStore trustStore = KeyStore.getInstance(KEY_STORE_TYPE);
      FileInputStream fin = new FileInputStream(trustStoreFileName);
      trustStore.load(fin, trustStorePassword.toCharArray());
      clientBuilder.trustStore(trustStore);
    } else if (sslContext != null) {
      clientBuilder.sslContext(sslContext);
    }
  } catch (Exception ex) {
    throw new IllegalArgumentException("Cannot setup SSL for Jersey2 client", ex);
  }
}

代码示例来源:origin: apache/nifi

public SslSocketFactory(final NiFiProperties nifiProperties) {
  final SSLContext sslCtx = SslContextFactory.createSslContext(nifiProperties);
  if (sslCtx == null) {
    try {
      sslSocketFactory = SSLContext.getDefault().getSocketFactory();
    } catch (final NoSuchAlgorithmException nsae) {
      throw new SslSocketFactoryCreationException(nsae);
    }
  } else {
    sslSocketFactory = sslCtx.getSocketFactory();
  }
}

代码示例来源:origin: apache/nifi

public SslServerSocketFactory(final NiFiProperties nifiProperties) {
  final SSLContext sslCtx = SslContextFactory.createSslContext(nifiProperties);
  if (sslCtx == null) {
    try {
      sslServerSocketFactory = SSLContext.getDefault().getServerSocketFactory();
    } catch (final NoSuchAlgorithmException nsae) {
      throw new SslServerSocketFactoryCreationException(nsae);
    }
  } else {
    sslServerSocketFactory = sslCtx.getServerSocketFactory();
  }
}

代码示例来源:origin: elastic/elasticsearch-hadoop

private ProtocolSocketFactory getSocketFactory() throws Exception {
  final SSLSocketFactory delegate = SSLContext.getDefault().getSocketFactory();
  return new ProtocolSocketFactory() {
    @Override
    public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) throws IOException, UnknownHostException {
      return delegate.createSocket(host, port, localAddress, localPort);
    }
    @Override
    public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
      return this.createSocket(host, port, localAddress, localPort);
    }
    @Override
    public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
      return delegate.createSocket(host, port);
    }
  };
}

代码示例来源:origin: apache/ignite

@Override public SSLContext create() {
    try {
      return SSLContext.getDefault();
    }
    catch (NoSuchAlgorithmException e) {
      throw new IgniteException(e);
    }
  }
};

代码示例来源:origin: org.mongodb/mongo-java-driver

private SSLContext getSslContext() {
    try {
      return (sslSettings.getContext() == null) ? SSLContext.getDefault() : sslSettings.getContext();
    } catch (NoSuchAlgorithmException e) {
      throw new MongoClientException("Unable to create default SSLContext", e);
    }
  }
}

代码示例来源:origin: org.mongodb/mongo-java-driver

private SSLContext getSslContext() {
  try {
    return (sslSettings.getContext() == null) ? SSLContext.getDefault() : sslSettings.getContext();
  } catch (NoSuchAlgorithmException e) {
    throw new MongoClientException("Unable to create default SSLContext", e);
  }
}

代码示例来源:origin: org.mongodb/mongo-java-driver

private SSLContext getSslContext() {
  try {
    return (sslSettings.getContext() == null) ? SSLContext.getDefault() : sslSettings.getContext();
  } catch (NoSuchAlgorithmException e) {
    throw new MongoClientException("Unable to create default SSLContext", e);
  }
}

代码示例来源:origin: apache/geode

/**
 * Creates & configures the SSLContext when SSL is enabled.
 *
 * @return new SSLContext configured using the given protocols & properties
 *
 * @throws GeneralSecurityException if security information can not be found
 * @throws IOException if information can not be loaded
 */
private SSLContext createAndConfigureSSLContext() throws GeneralSecurityException, IOException {
 if (sslConfig.useDefaultSSLContext()) {
  return SSLContext.getDefault();
 }
 SSLContext newSSLContext = getSSLContextInstance();
 KeyManager[] keyManagers = getKeyManagers();
 TrustManager[] trustManagers = getTrustManagers();
 newSSLContext.init(keyManagers, trustManagers, null /* use the default secure random */);
 return newSSLContext;
}

代码示例来源:origin: apache/incubator-druid

ClientHolder(int maxClientConnections)
{
 final Lifecycle druidLifecycle = new Lifecycle();
 try {
  this.client = HttpClientInit.createClient(
    new HttpClientConfig(maxClientConnections, SSLContext.getDefault(), Duration.ZERO),
    LifecycleUtils.asMmxLifecycle(druidLifecycle)
  );
 }
 catch (Exception e) {
  throw Throwables.propagate(e);
 }
}

代码示例来源:origin: apache/zookeeper

@Test
public void testCreateSSLContext_validCustomSSLContextClass() throws Exception {
  ZKConfig zkConfig = new ZKConfig();
  ClientX509Util clientX509Util = new ClientX509Util();
  zkConfig.setProperty(clientX509Util.getSslContextSupplierClassProperty(), SslContextSupplier.class.getName());
  final SSLContext sslContext = clientX509Util.createSSLContext(zkConfig);
  Assert.assertEquals(SSLContext.getDefault(), sslContext);
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testHttpsSilentServer() throws Throwable
{
 final Lifecycle lifecycle = new Lifecycle();
 try {
  final HttpClientConfig config = HttpClientConfig.builder()
                          .withSslContext(SSLContext.getDefault())
                          .withSslHandshakeTimeout(new Duration(100))
                          .build();
  final HttpClient client = HttpClientInit.createClient(config, lifecycle);
  final ListenableFuture<StatusResponseHolder> response = client
    .go(
      new Request(HttpMethod.GET, new URL(StringUtils.format("https://localhost:%d/", silentServerSocket.getLocalPort()))),
      new StatusResponseHandler(StandardCharsets.UTF_8)
    );
  Throwable e = null;
  try {
   response.get();
  }
  catch (ExecutionException e1) {
   e = e1.getCause();
  }
  Assert.assertTrue("ChannelException thrown by 'get'", e instanceof ChannelException);
 }
 finally {
  lifecycle.stop();
 }
}

代码示例来源:origin: apache/incubator-druid

@Test
public void testHttpsConnectionClosingServer() throws Throwable
{
 final Lifecycle lifecycle = new Lifecycle();
 try {
  final HttpClientConfig config = HttpClientConfig.builder().withSslContext(SSLContext.getDefault()).build();
  final HttpClient client = HttpClientInit.createClient(config, lifecycle);
  final ListenableFuture<StatusResponseHolder> response = client
    .go(
      new Request(HttpMethod.GET, new URL(StringUtils.format("https://localhost:%d/", closingServerSocket.getLocalPort()))),
      new StatusResponseHandler(StandardCharsets.UTF_8)
    );
  Throwable e = null;
  try {
   response.get();
  }
  catch (ExecutionException e1) {
   e = e1.getCause();
   e1.printStackTrace();
  }
  Assert.assertTrue("ChannelException thrown by 'get'", isChannelClosedException(e));
 }
 finally {
  lifecycle.stop();
 }
}

代码示例来源:origin: apache/incubator-druid

public static SSLContext getEffectiveSSLContext(HttpEmitterSSLClientConfig sslConfig, SSLContext sslContext)
 {
  SSLContext effectiveSSLContext;
  if (sslConfig.isUseDefaultJavaContext()) {
   try {
    effectiveSSLContext = SSLContext.getDefault();
   }
   catch (NoSuchAlgorithmException nsae) {
    throw new RuntimeException(nsae);
   }
  } else if (sslConfig.getTrustStorePath() != null) {
   log.info("Creating SSLContext for HttpEmitter client using config [%s]", sslConfig);
   effectiveSSLContext = new TLSUtils.ClientSSLContextBuilder()
     .setProtocol(sslConfig.getProtocol())
     .setTrustStoreType(sslConfig.getTrustStoreType())
     .setTrustStorePath(sslConfig.getTrustStorePath())
     .setTrustStoreAlgorithm(sslConfig.getTrustStoreAlgorithm())
     .setTrustStorePasswordProvider(sslConfig.getTrustStorePasswordProvider())
     .build();
  } else {
   effectiveSSLContext = sslContext;
  }
  return effectiveSSLContext;
 }
}

代码示例来源:origin: apache/incubator-druid

@Test
 public void testHttpsEchoServer() throws Throwable
 {
  final Lifecycle lifecycle = new Lifecycle();
  try {
   final HttpClientConfig config = HttpClientConfig.builder().withSslContext(SSLContext.getDefault()).build();
   final HttpClient client = HttpClientInit.createClient(config, lifecycle);

   final ListenableFuture<StatusResponseHolder> response = client
     .go(
       new Request(HttpMethod.GET, new URL(StringUtils.format("https://localhost:%d/", echoServerSocket.getLocalPort()))),
       new StatusResponseHandler(StandardCharsets.UTF_8)
     );

   expectedException.expect(ExecutionException.class);
   expectedException.expectMessage("org.jboss.netty.channel.ChannelException: Faulty channel in resource pool");

   response.get();
  }
  finally {
   lifecycle.stop();
  }
 }
}

代码示例来源:origin: apache/incubator-druid

final HttpClientConfig config = HttpClientConfig.builder().withSslContext(SSLContext.getDefault()).build();
final HttpClient client = HttpClientInit.createClient(config, lifecycle);

相关文章