本文整理匯總了Java中javax.net.ssl.TrustManagerFactory.getTrustManagers方法的典型用法代碼示例。如果您正苦於以下問題:Java TrustManagerFactory.getTrustManagers方法的具體用法?Java TrustManagerFactory.getTrustManagers怎麽用?Java TrustManagerFactory.getTrustManagers使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.net.ssl.TrustManagerFactory
的用法示例。
在下文中一共展示了TrustManagerFactory.getTrustManagers方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createTrustManagers
import javax.net.ssl.TrustManagerFactory; //導入方法依賴的package包/類
private static TrustManager[] createTrustManagers(final KeyStore keystore)
throws KeyStoreException, NoSuchAlgorithmException
{
if (keystore == null) {
throw new IllegalArgumentException("Keystore may not be null");
}
LOG.debug("Initializing trust manager");
TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
tmfactory.init(keystore);
TrustManager[] trustmanagers = tmfactory.getTrustManagers();
for (int i = 0; i < trustmanagers.length; i++) {
if (trustmanagers[i] instanceof X509TrustManager) {
trustmanagers[i] = new AuthSSLX509TrustManager(
(X509TrustManager)trustmanagers[i]);
}
}
return trustmanagers;
}
示例2: dumpTrustedCerts
import javax.net.ssl.TrustManagerFactory; //導入方法依賴的package包/類
public void dumpTrustedCerts() {
try {
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory
.getDefaultAlgorithm());
tmf.init((KeyStore) null);
X509TrustManager xtm = (X509TrustManager) tmf.getTrustManagers()[0];
StringBuffer buff = new StringBuffer();
for (X509Certificate cert : xtm.getAcceptedIssuers()) {
String certStr = "S:" + cert.getSubjectDN().getName() + "\nI:"
+ cert.getIssuerDN().getName();
Log.d(TAG, certStr);
buff.append(certStr + "\n\n");
}
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
}
示例3: init
import javax.net.ssl.TrustManagerFactory; //導入方法依賴的package包/類
public DefaultX509TrustManager init() throws IOException {
try {
final TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
factory.init(KeyStore.getInstance(KeyStore.getDefaultType()));
final TrustManager[] trustmanagers = factory.getTrustManagers();
if(trustmanagers.length == 0) {
throw new NoSuchAlgorithmException("SunX509 trust manager not supported");
}
system = (javax.net.ssl.X509TrustManager) trustmanagers[0];
}
catch(NoSuchAlgorithmException | KeyStoreException e) {
log.error(String.format("Initialization of trust store failed. %s", e.getMessage()));
throw new IOException(e);
}
return this;
}
示例4: createTrustManagers
import javax.net.ssl.TrustManagerFactory; //導入方法依賴的package包/類
public static TrustManager[] createTrustManagers() throws KeyStoreException,
NoSuchAlgorithmException,
CertificateException,
IOException {
TrustManager[] trustManagers = null;
Properties props = FileUtils.loadPropertiesFromBundle("org.dpdirect.datapower.utils.ssl");
// create Inputstream to truststore file
InputStream inputStream = new FileInputStream(props.getProperty("trust.store.filename"));
// create keystore object, load it with truststorefile data
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(inputStream, props.getProperty("trust.store.password").toCharArray());
// create trustmanager factory and load the keystore object in it
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
trustManagers = trustManagerFactory.getTrustManagers();
return trustManagers;
}
示例5: getCertificateTrustManager
import javax.net.ssl.TrustManagerFactory; //導入方法依賴的package包/類
private CertificateTrustManager getCertificateTrustManager(KeyStore keyStore) {
CertificateTrustManager certificateTrustManager = null;
try {
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
X509TrustManager defaultTrustManager = (X509TrustManager) tmf.getTrustManagers()[0];
certificateTrustManager = new CertificateTrustManager(defaultTrustManager);
} catch (Exception e) {
e.printStackTrace();
}
return certificateTrustManager;
}
示例6: systemDefaultTrustManager
import javax.net.ssl.TrustManagerFactory; //導入方法依賴的package包/類
private X509TrustManager systemDefaultTrustManager() {
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:"
+ Arrays.toString(trustManagers));
}
return (X509TrustManager) trustManagers[0];
} catch (GeneralSecurityException e) {
throw new AssertionError(); // The system has no TLS. Just give up.
}
}
示例7: trustManagerForCertificates
import javax.net.ssl.TrustManagerFactory; //導入方法依賴的package包/類
/**
* Returns a trust manager that trusts {@code certificates} and none other. HTTPS services whose
* certificates have not been signed by these certificates will fail with a {@code
* SSLHandshakeException}.
*
* <p>This can be used to replace the host platform's built-in trusted certificates with a custom
* set. This is useful in development where certificate authority-trusted certificates aren't
* available. Or in production, to avoid reliance on third-party certificate authorities.
*
* <p>See also {@link CertificatePinner}, which can limit trusted certificates while still using
* the host platform's built-in trust store.
*
* <h3>Warning: Customizing Trusted Certificates is Dangerous!</h3>
*
* <p>Relying on your own trusted certificates limits your server team's ability to update their
* TLS certificates. By installing a specific set of trusted certificates, you take on additional
* operational complexity and limit your ability to migrate between certificate authorities. Do
* not use custom trusted certificates in production without the blessing of your server's TLS
* administrator.
*/
private X509TrustManager trustManagerForCertificates(InputStream in)
throws GeneralSecurityException {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(in);
if (certificates.isEmpty()) {
throw new IllegalArgumentException("expected non-empty set of trusted certificates");
}
// Put the certificates a key store.
char[] password = "password".toCharArray(); // Any password will work.
KeyStore keyStore = newEmptyKeyStore(password);
int index = 0;
for (Certificate certificate : certificates) {
String certificateAlias = Integer.toString(index++);
keyStore.setCertificateEntry(certificateAlias, certificate);
}
// Use it to build an X509 trust manager.
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(
KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, password);
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:"
+ Arrays.toString(trustManagers));
}
return (X509TrustManager) trustManagers[0];
}
示例8: build
import javax.net.ssl.TrustManagerFactory; //導入方法依賴的package包/類
public SslClient build() {
try {
// Put the certificate in a key store.
char[] password = "password".toCharArray();
KeyStore keyStore = newEmptyKeyStore(password);
if (keyPair != null) {
Certificate[] certificates = chainCertificates.toArray(
new Certificate[chainCertificates.size()]);
keyStore.setKeyEntry("private", keyPair.getPrivate(), password, certificates);
}
for (int i = 0; i < certificates.size(); i++) {
keyStore.setCertificateEntry("cert_" + i, certificates.get(i));
}
// Wrap it up in an SSL context.
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(
KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, password);
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
throw new IllegalStateException("Unexpected default trust managers:"
+ Arrays.toString(trustManagers));
}
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagerFactory.getKeyManagers(), trustManagers, new SecureRandom());
return new SslClient(sslContext, (X509TrustManager) trustManagers[0]);
} catch (GeneralSecurityException gse) {
throw new AssertionError(gse);
}
}
示例9: get
import javax.net.ssl.TrustManagerFactory; //導入方法依賴的package包/類
public static void get(String host, int port, KeyStore keyStore) throws Exception {
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
X509TrustManager defaultTrustManager = (X509TrustManager) tmf.getTrustManagers()[0];
SavingTrustManager tm = new SavingTrustManager(defaultTrustManager);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] {tm}, null);
LOGGER.info("Iniciando conexão com: " + host + ":" + port + "...");
SSLSocket socket = (SSLSocket) sslContext.getSocketFactory().createSocket(host, port);
try {
socket.setSoTimeout(30 * 1000);
socket.startHandshake();
socket.close();
} catch (Exception e) {
LOGGER.info(e.toString());
}
X509Certificate[] chain = tm.chain;
if (chain == null) {
LOGGER.info("Não foi possivel obter a cadeia de certificados");
}
LOGGER.info("O servidor enviou " + chain.length + " certificado(s):");
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
MessageDigest md5 = MessageDigest.getInstance("MD5");
for (int i = 0; i < chain.length; i++) {
X509Certificate cert = chain[i];
sha1.update(cert.getEncoded());
md5.update(cert.getEncoded());
String alias = host + "-" + (i);
keyStore.setCertificateEntry(alias, cert);
LOGGER.info("Certificado adicionado usando alias: '" + alias + "'");
}
}
示例10: createTrustManagers
import javax.net.ssl.TrustManagerFactory; //導入方法依賴的package包/類
public static TrustManager[] createTrustManagers(final KeyStore keystore) {
try {
TrustManagerFactory tmfactory =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmfactory.init(keystore);
TrustManager[] trustmanagers = tmfactory.getTrustManagers();
return trustmanagers;
} catch (Exception e) {
throw new IllegalArgumentException("Bad trust store."
+ e.getMessage());
}
}
示例11: getTrustableSSLContext
import javax.net.ssl.TrustManagerFactory; //導入方法依賴的package包/類
/**
* Gets a SSLContext to be trusted.
*
* @param trustStoreFile the trust store file
* @return the trustable SSL context
* @throws IOException
* @throws FileNotFoundException
* @throws CertificateException
* @throws NoSuchAlgorithmException
* @throws KeyStoreException
* @throws KeyManagementException
*/
private static SSLContext getTrustableSSLContext(File trustStoreFile) throws NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException, KeyStoreException, KeyManagementException {
SSLContext sslContext = null;
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(new FileInputStream(trustStoreFile), TRUSTSTORE_PASSWORD.toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(trustStore);
TrustManager[] tms = tmf.getTrustManagers();
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tms, null);
return sslContext;
}
示例12: EasyX509TrustManager
import javax.net.ssl.TrustManagerFactory; //導入方法依賴的package包/類
/**
* Constructor for EasyX509TrustManager.
*/
public EasyX509TrustManager(KeyStore keystore) throws NoSuchAlgorithmException, KeyStoreException {
super();
TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
factory.init(keystore);
TrustManager[] trustmanagers = factory.getTrustManagers();
if (trustmanagers.length == 0) {
throw new NoSuchAlgorithmException("no trust manager found");
}
this.standardTrustManager = (X509TrustManager)trustmanagers[0];
}
示例13: createSSLContext
import javax.net.ssl.TrustManagerFactory; //導入方法依賴的package包/類
public static SSLContext createSSLContext()
{
try
{
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(new FileInputStream("A2KeyStore.jks"), "1234567890".toCharArray());
// Create key manager
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyManagerFactory.init(keyStore, "1234567890".toCharArray());
KeyManager[] km = keyManagerFactory.getKeyManagers();
// Create trust manager
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
trustManagerFactory.init(keyStore);
TrustManager[] tm = trustManagerFactory.getTrustManagers();
// Initialize SSLContext
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(km, tm, null);
return sslContext;
}
catch (Exception ex)
{
ex.printStackTrace();
}
return null;
}
示例14: AlwaysTrustManager
import javax.net.ssl.TrustManagerFactory; //導入方法依賴的package包/類
public AlwaysTrustManager(KeyStore keyStore)
throws NoSuchAlgorithmException, KeyStoreException {
TrustManagerFactory tmf
= TrustManagerFactory.getInstance(TrustManagerFactory.
getDefaultAlgorithm());
tmf.init(keyStore);
TrustManager tms[] = tmf.getTrustManagers();
for (TrustManager tm : tms) {
trustManager = (X509TrustManager) tm;
return;
}
}
示例15: createTrustManagers
import javax.net.ssl.TrustManagerFactory; //導入方法依賴的package包/類
/**
* {@inheritDoc}
*/
@Override
public TrustManager[] createTrustManagers()
{
KeyInfoManager keyInfoManager = null;
try
{
keyInfoManager = getKeyInfoManager(getKeyMetaDataFileLocation());
KeyStore ks = loadKeyStore(getKeyStoreParameters(), keyInfoManager);
logger.debug("Initializing trust managers");
TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
tmfactory.init(ks);
return tmfactory.getTrustManagers();
}
catch(Throwable e)
{
throw new AlfrescoRuntimeException("Unable to create key manager", e);
}
finally
{
if(keyInfoManager != null)
{
keyInfoManager.clear();
}
}
}