当前位置: 首页>>代码示例>>Java>>正文


Java KeyStore.getDefaultType方法代码示例

本文整理汇总了Java中java.security.KeyStore.getDefaultType方法的典型用法代码示例。如果您正苦于以下问题:Java KeyStore.getDefaultType方法的具体用法?Java KeyStore.getDefaultType怎么用?Java KeyStore.getDefaultType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.security.KeyStore的用法示例。


在下文中一共展示了KeyStore.getDefaultType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: loadPublicKeyFromKeyStore

import java.security.KeyStore; //导入方法依赖的package包/类
/**
 * 从KeyStore获取公钥
 * @param location
 * @param alias
 * @param storeType
 * @param storePass
 * @param keyPass
 * @return
 */
public static PublicKey loadPublicKeyFromKeyStore(String location, String alias, String storeType, String storePass, String keyPass) {
    try {
        storeType = null == storeType ? KeyStore.getDefaultType() : storeType;
        keyPass = keyPass == null ? storePass : keyPass;
        KeyStore keyStore = KeyStore.getInstance(storeType);
        InputStream is = new FileInputStream(location);
        keyStore.load(is, storePass.toCharArray());

        RSAPrivateCrtKey key = (RSAPrivateCrtKey) keyStore.getKey(alias, keyPass.toCharArray());
        RSAPublicKeySpec spec = new RSAPublicKeySpec(key.getModulus(),
                key.getPublicExponent());
        PublicKey publicKey = KeyFactory.getInstance(KEY_ALGORITHM).generatePublic(spec);
        return publicKey;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
开发者ID:warlock-china,项目名称:azeroth,代码行数:27,代码来源:RSA.java

示例2: addCertificate

import java.security.KeyStore; //导入方法依赖的package包/类
private SSLSocketFactory addCertificate(InputStream inputStream) throws CertificateException, NoSuchAlgorithmException, IOException, KeyStoreException, KeyManagementException {
    // loading CAs from an InputStream
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    Certificate ca;
    try {
        ca = cf.generateCertificate(inputStream);
    } finally {
        inputStream.close();
    }

    // creating a KeyStore containing our trusted CAs
    String keyStoreType = KeyStore.getDefaultType();
    KeyStore keyStore = KeyStore.getInstance(keyStoreType);
    keyStore.load(null, null);
    keyStore.setCertificateEntry("ca", ca);

    // creating a TrustManager that trusts the CAs in our KeyStore
    String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
    tmf.init(keyStore);

    // creating an SSLSocketFactory that uses our TrustManager
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, tmf.getTrustManagers(), null);

    return sslContext.getSocketFactory();
}
 
开发者ID:sinhaDroid,项目名称:BlogBookApp,代码行数:28,代码来源:MyWebService.java

示例3: FileTrustStoreSslSocketFactory

import java.security.KeyStore; //导入方法依赖的package包/类
/**
 * Instantiates a new trusted proxy authentication trust store ssl socket factory.
 * Defaults to {@code TLSv1} and {@link SSLConnectionSocketFactory#BROWSER_COMPATIBLE_HOSTNAME_VERIFIER}
 * for the supported protocols and hostname verification.
 * @param trustStoreFile the trust store file
 * @param trustStorePassword the trust store password
 */
@Autowired
public FileTrustStoreSslSocketFactory(
        @Value("${http.client.truststore.file:classpath:truststore.jks}")
        final File trustStoreFile,
        @Value("${http.client.truststore.psw:changeit}")
        final String trustStorePassword) {
    this(trustStoreFile, trustStorePassword, KeyStore.getDefaultType());
}
 
开发者ID:hsj-xiaokang,项目名称:springboot-shiro-cas-mybatis,代码行数:16,代码来源:FileTrustStoreSslSocketFactory.java

示例4: setTrustedCertificate

import java.security.KeyStore; //导入方法依赖的package包/类
public void setTrustedCertificate(InputStream inputFile) {
    try {
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        InputStream caInput = new BufferedInputStream(inputFile);
        Certificate ca = cf.generateCertificate(caInput);

        // Create a KeyStore containing our trusted CAs
        String keyStoreType = KeyStore.getDefaultType();
        keyStore = KeyStore.getInstance(keyStoreType);
        keyStore.load(null, null);
        keyStore.setCertificateEntry("ca", ca);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
开发者ID:nhancv,项目名称:nc-android-webrtcpeer,代码行数:16,代码来源:DefaultSocketService.java

示例5: init

import java.security.KeyStore; //导入方法依赖的package包/类
public void init(Properties properties)
        throws GeneralSecurityException, IOException {
    // required, could use default keyStore, but it is better practice to explicitly specify
    final String keyStoreFilename = properties.getProperty("javax.net.ssl.keyStore");
    // required
    final String keyStorePassword = properties.getProperty("javax.net.ssl.keyStorePassword");
    // optional, uses default if not specified 
    String keyStoreType = properties.getProperty("javax.net.ssl.keyStoreType");
    if (keyStoreType == null) {
        keyStoreType = KeyStore.getDefaultType();
        logger.logWarning("Using default keystore type " + keyStoreType);
    }
    if (keyStoreFilename == null || keyStorePassword == null) {
        logger.logWarning("TLS server settings will be inactive - TLS key store will use JVM defaults"
                + " keyStoreType=" +  keyStoreType
                + " javax.net.ssl.keyStore=" + keyStoreFilename
                + " javax.net.ssl.keyStorePassword=" + (keyStorePassword == null? null: "***"));
    }

    // required, could use default trustStore, but it is better practice to explicitly specify
    final String trustStoreFilename = properties.getProperty("javax.net.ssl.trustStore");
    // optional, if not specified using keyStorePassword
    String trustStorePassword = properties.getProperty("javax.net.ssl.trustStorePassword");
    if(trustStorePassword == null) {
    	logger.logInfo("javax.net.ssl.trustStorePassword is null, using the password passed through javax.net.ssl.keyStorePassword");
    	trustStorePassword = keyStorePassword;
    }
    // optional, uses default if not specified 
    String trustStoreType = properties.getProperty("javax.net.ssl.trustStoreType");
    if (trustStoreType == null) {
        trustStoreType = KeyStore.getDefaultType();
        logger.logWarning("Using default truststore type " + trustStoreType);
    }
    if (trustStoreFilename == null || trustStorePassword == null) {
        logger.logWarning("TLS trust settings will be inactive - TLS trust store will use JVM defaults."
                + " trustStoreType=" +  trustStoreType
                + " javax.net.ssl.trustStore=" +  trustStoreFilename
                + " javax.net.ssl.trustStorePassword=" + (trustStorePassword == null? null: "***"));
    }

    String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
    if (algorithm == null) {
        algorithm = "SunX509";
    }
    if (logger.isLoggingEnabled(LogWriter.TRACE_DEBUG)) {
        logger.logDebug("SecurityManagerProvider " + this.getClass().getCanonicalName() + " will use algorithm " + algorithm);
    }
    
    keyManagerFactory = KeyManagerFactory.getInstance(algorithm);
    if(keyStoreFilename != null) {
    	final KeyStore ks = KeyStore.getInstance(keyStoreType);
    	ks.load(new FileInputStream(new File(keyStoreFilename)), keyStorePassword.toCharArray());
    	
    	keyManagerFactory.init(ks, keyStorePassword.toCharArray());
    } else {
    	keyManagerFactory.init(null, null);
    }

    trustManagerFactory = TrustManagerFactory.getInstance(algorithm);
    if(trustStoreFilename != null) {
    	final KeyStore ts = KeyStore.getInstance(trustStoreType);
    	ts.load(new FileInputStream(new File(trustStoreFilename)), trustStorePassword.toCharArray());
    	
    	trustManagerFactory.init((KeyStore) ts);
    } else {
    	trustManagerFactory.init((KeyStore)null);
    }
    if (logger.isLoggingEnabled(LogWriter.TRACE_DEBUG)) {
    	logger.logDebug("TLS settings OK. SecurityManagerProvider " + this.getClass().getCanonicalName() + " initialized.");
    }
}
 
开发者ID:YunlongYang,项目名称:LightSIP,代码行数:72,代码来源:DefaultSecurityManagerProvider.java

示例6: getDefaultSocketFactory

import java.security.KeyStore; //导入方法依赖的package包/类
public static SSLSocketFactory getDefaultSocketFactory(InputStream inputStream) {
    try {
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        Certificate ca;
        try {
            ca = cf.generateCertificate(inputStream);
        } finally {
            inputStream.close();
        }

        String keyStoreType = KeyStore.getDefaultType();
        KeyStore keyStore = KeyStore.getInstance(keyStoreType);
        keyStore.load(null);
        keyStore.setCertificateEntry("ca", ca);

        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(keyStore);

        // Install the all-trusting trust manager
        final SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, tmf.getTrustManagers(), new java.security.SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        return sslContext.getSocketFactory();
    } catch (Exception e) {
        Log.e(TAG, Log.getStackTraceString(e));
    }

    return null;
}
 
开发者ID:jonyChina162,项目名称:okhttpNDS,代码行数:31,代码来源:HttpsHelper.java

示例7: configure

import java.security.KeyStore; //导入方法依赖的package包/类
@Override
public void configure(HttpConfig config, ScriptContext context) {
    ClientConfig cc = new ClientConfig();
    // support request body for DELETE (non-standard)
    cc.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
    if (!config.isFollowRedirects()) {
        cc.property(ClientProperties.FOLLOW_REDIRECTS, false);
    }
    ClientBuilder clientBuilder = ClientBuilder.newBuilder()
            .withConfig(cc)
            .register(new LoggingInterceptor(context)) // must be first
            .register(MultiPartFeature.class);
    if (config.isSslEnabled()) {
        SSLContext sslContext;
        if (config.getSslTrustStore() != null) {
            String trustStoreFile = config.getSslTrustStore();                
            String password = config.getSslTrustStorePassword();
            char[] passwordChars = password == null ? null : password.toCharArray();
            String algorithm = config.getSslAlgorithm();
            String type = config.getSslTrustStoreType();
            if (type == null) {
                type = KeyStore.getDefaultType();
            }
            try {
                KeyStore trustStore = KeyStore.getInstance(type);
                InputStream is = FileUtils.getFileStream(trustStoreFile, context);
                trustStore.load(is, passwordChars);
                context.logger.debug("trust store key count: {}", trustStore.size());
                sslContext = SslConfigurator.newInstance()
                        .securityProtocol(algorithm) // will default to TLS if null
                        .trustStore(trustStore)
                        // .keyStore(trustStore)
                        .createSSLContext();
            } catch (Exception e) {
                context.logger.error("ssl config failed: {}", e.getMessage());
                throw new RuntimeException(e);
            }                
        } else {
            sslContext = HttpUtils.getSslContext(config.getSslAlgorithm());
        }
        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
        clientBuilder.sslContext(sslContext);
        clientBuilder.hostnameVerifier((host, session) -> true);
    }
    client = clientBuilder.build();
    client.property(ClientProperties.CONNECT_TIMEOUT, config.getConnectTimeout());
    client.property(ClientProperties.READ_TIMEOUT, config.getReadTimeout());
    if (config.getProxyUri() != null) {
        client.property(ClientProperties.PROXY_URI, config.getProxyUri());
        if (config.getProxyUsername() != null && config.getProxyPassword() != null) {
            client.property(ClientProperties.PROXY_USERNAME, config.getProxyUsername());
            client.property(ClientProperties.PROXY_PASSWORD, config.getProxyPassword());
        }
    }
}
 
开发者ID:intuit,项目名称:karate,代码行数:56,代码来源:JerseyHttpClient.java

示例8: getKeystore

import java.security.KeyStore; //导入方法依赖的package包/类
private synchronized KeyStore getKeystore() throws IOException {
    String type = null;
    try {
        if(null == _keystore) {
            // Get the key manager factory for the default algorithm.
            final Preferences preferences = PreferencesFactory.get();
            type = preferences.getProperty("connection.ssl.keystore.type");
            if(log.isInfoEnabled()) {
                log.info(String.format("Load default store of type %s", type));
            }
            if(null == type) {
                type = KeyStore.getDefaultType();
            }
            final String provider = preferences.getProperty("connection.ssl.keystore.provider");
            if(StringUtils.isBlank(provider)) {
                _keystore = KeyStore.getInstance(type);
            }
            else {
                _keystore = KeyStore.getInstance(type, provider);
            }
            // Load default key store
            _keystore.load(null, null);
        }
    }
    catch(Exception e) {
        try {
            log.error(String.format("Could not load default store of type %s", type), e);
            if(log.isInfoEnabled()) {
                log.info("Load default store of default type");
            }
            _keystore = KeyStore.getInstance(KeyStore.getDefaultType());
            _keystore.load(null, null);
        }
        catch(NoSuchAlgorithmException | KeyStoreException | CertificateException ex) {
            log.error(String.format("Initialization of key store failed. %s", e.getMessage()));
            throw new IOException(e);
        }
    }
    return _keystore;
}
 
开发者ID:iterate-ch,项目名称:cyberduck,代码行数:41,代码来源:CertificateStoreX509KeyManager.java

示例9: signZip

import java.security.KeyStore; //导入方法依赖的package包/类
public void signZip(URL keystoreURL,
                    String keystoreType,
                    char[] keystorePw,
                    String certAlias,
                    char[] certPw,
                    String signatureAlgorithm,
                    String inputZipFilename,
                    String outputZipFilename)
        throws ClassNotFoundException, IllegalAccessException, InstantiationException,
        IOException, GeneralSecurityException {
    InputStream keystoreStream = null;


    try {
        KeyStore keystore = null;
        if (keystoreType == null) keystoreType = KeyStore.getDefaultType();
        keystore = KeyStore.getInstance(keystoreType);

        keystoreStream = keystoreURL.openStream();
        keystore.load(keystoreStream, keystorePw);
        Certificate cert = keystore.getCertificate(certAlias);
        X509Certificate publicKey = (X509Certificate) cert;
        Key key = keystore.getKey(certAlias, certPw);
        PrivateKey privateKey = (PrivateKey) key;

        setKeys("custom", publicKey, privateKey, signatureAlgorithm, null);

        signZip(inputZipFilename, outputZipFilename);
    } finally {
        if (keystoreStream != null) keystoreStream.close();
    }
}
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:33,代码来源:ZipSigner.java

示例10: loadPrivateKeyFromKeyStore

import java.security.KeyStore; //导入方法依赖的package包/类
/**
 * 从KeyStore获取私钥
 * @param location
 * @param alias
 * @param storeType
 * @param storePass
 * @param keyPass
 * @return
 */
public static PrivateKey loadPrivateKeyFromKeyStore(String location, String alias, String storeType, String storePass, String keyPass) {
    try {
        storeType = null == storeType ? KeyStore.getDefaultType() : storeType;
        keyPass = keyPass == null ? storePass : keyPass;
        KeyStore keyStore = KeyStore.getInstance(storeType);
        InputStream is = new FileInputStream(location);
        keyStore.load(is, storePass.toCharArray());
        // 由密钥库获取密钥的两种方式
        return (PrivateKey) keyStore.getKey(alias, keyPass.toCharArray());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
开发者ID:warlock-china,项目名称:azeroth,代码行数:23,代码来源:RSA.java

示例11: getClient

import java.security.KeyStore; //导入方法依赖的package包/类
public static OkHttpClient.Builder getClient(SatispayContext satispayContext) {
    OkHttpClient.Builder okHttpClientBuilder;
    okHttpClientBuilder = new OkHttpClient.Builder();

    // ==> the SSL context is build only in environments different from PROD / STAGING, where the server cert is self signed
    String serverCert = satispayContext.getServerCert();
    if (serverCert != null) {
        try {
            String keyStoreType = KeyStore.getDefaultType();
            KeyStore keyStore;
            keyStore = KeyStore.getInstance(keyStoreType);
            keyStore.load(null, null);
            keyStore.setCertificateEntry("ca", CryptoUtils.certificateX509(serverCert));

            String trustManagerDefaultAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(trustManagerDefaultAlgorithm);
            trustManagerFactory.init(keyStore);

            SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
            okHttpClientBuilder.sslSocketFactory(sslSocketFactory);
        } catch (Exception e) {
            ProtoLogger.error("!!! Error generating TLS context !!!");
        }
    }
    okHttpClientBuilder.connectTimeout(10, TimeUnit.SECONDS);
    okHttpClientBuilder.writeTimeout(10, TimeUnit.SECONDS);
    okHttpClientBuilder.readTimeout(30, TimeUnit.SECONDS);
    return okHttpClientBuilder;
}
 
开发者ID:satispay,项目名称:in-store-api-java-sdk,代码行数:32,代码来源:NetworkUtilities.java

示例12: createClient

import java.security.KeyStore; //导入方法依赖的package包/类
public static OkHttpClient createClient(Context context) {

        OkHttpClient client = null;

        CertificateFactory cf = null;
        InputStream cert = null;
        Certificate ca = null;
        SSLContext sslContext = null;
        try {
            cf = CertificateFactory.getInstance("X.509");
            cert = context.getResources().openRawResource(R.raw.public_key); // Place your 'my_cert.crt' file in `res/raw`

            ca = cf.generateCertificate(cert);
            cert.close();
            
            String keyStoreType = KeyStore.getDefaultType();
            KeyStore keyStore = KeyStore.getInstance(keyStoreType);
            keyStore.load(null, null);
            keyStore.setCertificateEntry("ca", ca);

            String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
            tmf.init(keyStore);

            sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, tmf.getTrustManagers(), null);

            client = new OkHttpClient.Builder()
                    .sslSocketFactory(sslContext.getSocketFactory())
                .addInterceptor(new JwtTokenInterceptor())
                    .build();

        } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException | KeyManagementException e) {
            e.printStackTrace();
        }


        return client;
    }
 
开发者ID:humaniq,项目名称:react-native-android-library-humaniq-api,代码行数:40,代码来源:SelfSigningClientBuilder.java

示例13: createSSLSocketFactory

import java.security.KeyStore; //导入方法依赖的package包/类
/**
     * create ssl socket factory. by target crt file.
     * @param context the context.
     * @param assetsFilePath the crt file path in assets.
     * @return an instance of SSLSocketFactory.
     */
    public static SSLSocketFactory createSSLSocketFactory(Context context, String assetsFilePath) {
        SSLContext sslContext = null;
        try {
            CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
            InputStream certificates = new BufferedInputStream(context.getAssets().open(assetsFilePath));
            Certificate ca;
            try {
                ca = certificateFactory.generateCertificate(certificates);
                System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
            } finally {
                certificates.close();
            }

            // Create a KeyStore containing our trusted CAs
            String keyStoreType = KeyStore.getDefaultType();
            KeyStore keyStore = KeyStore.getInstance(keyStoreType);
            keyStore.load(null, null);
            keyStore.setCertificateEntry("ca", ca);

            // Create a TrustManager that trusts the CAs in our KeyStore
            String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
            tmf.init(keyStore);

            // Create an SSLContext that uses our TrustManager
            sslContext = SSLContext.getInstance("TLSv1", "AndroidOpenSSL");
//            sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, tmf.getTrustManagers(), null);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return sslContext != null ? sslContext.getSocketFactory() : null;
    }
 
开发者ID:LightSun,项目名称:android-util2,代码行数:42,代码来源:HttpsHelper.java

示例14: getTrustManagers

import java.security.KeyStore; //导入方法依赖的package包/类
private TrustManager[] getTrustManagers()
    throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
  TrustManager[] trustManagers = null;
  GfeConsoleReader consoleReader = GfeConsoleReaderFactory.getDefaultConsoleReader();

  String trustStoreType = sslConfig.getTruststoreType();
  if (StringUtils.isEmpty(trustStoreType)) {
    // read from console, default on empty
    if (consoleReader.isSupported()) {
      trustStoreType = consoleReader
          .readLine("Please enter the trustStoreType (javax.net.ssl.trustStoreType) : ");
    } else {
      trustStoreType = KeyStore.getDefaultType();
    }
  }

  KeyStore ts = KeyStore.getInstance(trustStoreType);
  String trustStorePath = sslConfig.getTruststore();
  if (StringUtils.isEmpty(trustStorePath)) {
    if (consoleReader.isSupported()) {
      trustStorePath = consoleReader
          .readLine("Please enter the trustStore location (javax.net.ssl.trustStore) : ");
    }
  }
  FileInputStream fis = new FileInputStream(trustStorePath);
  String passwordString = sslConfig.getTruststorePassword();
  char[] password = null;
  if (passwordString != null) {
    if (passwordString.trim().equals("")) {
      if (!StringUtils.isEmpty(passwordString)) {
        String toDecrypt = "encrypted(" + passwordString + ")";
        passwordString = PasswordUtil.decrypt(toDecrypt);
        password = passwordString.toCharArray();
      }
      // read from the console
      if (StringUtils.isEmpty(passwordString) && consoleReader.isSupported()) {
        password = consoleReader.readPassword(
            "Please enter password for trustStore (javax.net.ssl.trustStorePassword) : ");
      }
    } else {
      password = passwordString.toCharArray();
    }
  }
  ts.load(fis, password);

  // default algorithm can be changed by setting property "ssl.TrustManagerFactory.algorithm" in
  // security properties
  TrustManagerFactory tmf =
      TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  tmf.init(ts);
  trustManagers = tmf.getTrustManagers();
  // follow the security tip in java doc
  if (password != null) {
    java.util.Arrays.fill(password, ' ');
  }

  return trustManagers;
}
 
开发者ID:ampool,项目名称:monarch,代码行数:59,代码来源:SocketCreator.java

示例15: getKeyManagers

import java.security.KeyStore; //导入方法依赖的package包/类
private KeyManager[] getKeyManagers() throws KeyStoreException, IOException,
    NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException {
  GfeConsoleReader consoleReader = GfeConsoleReaderFactory.getDefaultConsoleReader();

  KeyManager[] keyManagers = null;
  String keyStoreType = sslConfig.getKeystoreType();
  if (StringUtils.isEmpty(keyStoreType)) {
    // read from console, default on empty
    if (consoleReader.isSupported()) {
      keyStoreType =
          consoleReader.readLine("Please enter the keyStoreType (javax.net.ssl.keyStoreType) : ");
    } else {
      keyStoreType = KeyStore.getDefaultType();
    }
  }
  KeyStore keyStore = KeyStore.getInstance(keyStoreType);
  String keyStoreFilePath = sslConfig.getKeystore();
  if (StringUtils.isEmpty(keyStoreFilePath)) {
    if (consoleReader.isSupported()) {
      keyStoreFilePath = consoleReader
          .readLine("Please enter the keyStore location (javax.net.ssl.keyStore) : ");
    } else {
      keyStoreFilePath =
          System.getProperty("user.home") + System.getProperty("file.separator") + ".keystore";
    }
  }

  FileInputStream fileInputStream = new FileInputStream(keyStoreFilePath);
  String passwordString = sslConfig.getKeystorePassword();
  char[] password = null;
  if (passwordString != null) {
    if (passwordString.trim().equals("")) {
      String encryptedPass = System.getenv("javax.net.ssl.keyStorePassword");
      if (!StringUtils.isEmpty(encryptedPass)) {
        String toDecrypt = "encrypted(" + encryptedPass + ")";
        passwordString = PasswordUtil.decrypt(toDecrypt);
        password = passwordString.toCharArray();
      }
      // read from the console
      if (StringUtils.isEmpty(passwordString) && consoleReader != null) {
        password = consoleReader.readPassword(
            "Please enter password for keyStore (javax.net.ssl.keyStorePassword) : ");
      }
    } else {
      password = passwordString.toCharArray();
    }
  }
  keyStore.load(fileInputStream, password);
  // default algorithm can be changed by setting property "ssl.KeyManagerFactory.algorithm" in
  // security properties
  KeyManagerFactory keyManagerFactory =
      KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  keyManagerFactory.init(keyStore, password);
  keyManagers = keyManagerFactory.getKeyManagers();
  // follow the security tip in java doc
  if (password != null) {
    java.util.Arrays.fill(password, ' ');
  }

  KeyManager[] extendedKeyManagers = new KeyManager[keyManagers.length];

  for (int i = 0; i < keyManagers.length; i++)

  {
    extendedKeyManagers[i] = new ExtendedAliasKeyManager(keyManagers[i], sslConfig.getAlias());
  }

  return extendedKeyManagers;
}
 
开发者ID:ampool,项目名称:monarch,代码行数:70,代码来源:SocketCreator.java


注:本文中的java.security.KeyStore.getDefaultType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。