本文整理匯總了Java中javax.net.ssl.SSLContext.getInstance方法的典型用法代碼示例。如果您正苦於以下問題:Java SSLContext.getInstance方法的具體用法?Java SSLContext.getInstance怎麽用?Java SSLContext.getInstance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.net.ssl.SSLContext
的用法示例。
在下文中一共展示了SSLContext.getInstance方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getSSLSocketFactory
import javax.net.ssl.SSLContext; //導入方法依賴的package包/類
/**
* 獲得SSLSocektFactory
*
* @param password 密碼
* @param keyStorePath 密鑰庫路徑
* @param trustStorePath 信任庫路徑
* @return SSLSocketFactory
* @throws Exception
*/
private static SSLSocketFactory getSSLSocketFactory(String password, String keyStorePath, String trustStorePath)
throws Exception {
// 實例化密鑰庫
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
// 獲得密鑰庫
KeyStore keyStore = getKeyStore(keyStorePath, password);
// 初始化密鑰工廠
keyManagerFactory.init(keyStore, password.toCharArray());
// 實例化信任庫
TrustManagerFactory trustManagerFactory = TrustManagerFactory
.getInstance(TrustManagerFactory.getDefaultAlgorithm());
// 獲得信任庫
KeyStore trustStore = getKeyStore(trustStorePath, password);
// 初始化信任庫
trustManagerFactory.init(trustStore);
// 實例化SSL上下文
SSLContext ctx = SSLContext.getInstance(PROTOCOL);
// 初始化SSL上下文
ctx.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), new SecureRandom());
// 獲得SSLSocketFactory
return ctx.getSocketFactory();
}
示例2: createSSLContext
import javax.net.ssl.SSLContext; //導入方法依賴的package包/類
/**
* Lifted from ACCUMULO-3318 - Lifted from TSSLTransportFactory in Thrift-0.9.1.
* The method to create a client socket with an SSLContextFactory object is not visible to us.
* Have to use * SslConnectionParams instead of TSSLTransportParameters because no getters exist
* on TSSLTransportParameters.
*/
private static SSLContext createSSLContext(String truststore,
String truststorePassword,
String truststoreType) throws FlumeException {
SSLContext ctx;
try {
ctx = SSLContext.getInstance("TLS");
TrustManagerFactory tmf;
tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
KeyStore ts = null;
if (truststore != null && truststoreType != null) {
ts = KeyStore.getInstance(truststoreType);
ts.load(new FileInputStream(truststore), truststorePassword.toCharArray());
tmf.init(ts);
}
tmf.init(ts);
ctx.init(null, tmf.getTrustManagers(), null);
} catch (Exception e) {
throw new FlumeException("Error creating the transport", e);
}
return ctx;
}
示例3: initTrustAllSSLcontext
import javax.net.ssl.SSLContext; //導入方法依賴的package包/類
public static SSLSocketFactory initTrustAllSSLcontext(TrustManager[] trustAllCerts) throws NoSuchAlgorithmException,
KeyManagementException {
// Install the all-trusting trust manager
SSLSocketFactory sslSocketfactory = null;
final SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
sslSocketfactory = sc.getSocketFactory();
return sslSocketfactory;
}
示例4: makeSSLSocketFactory
import javax.net.ssl.SSLContext; //導入方法依賴的package包/類
public static SSLServerSocketFactory makeSSLSocketFactory(KeyStore loadedKeyStore, KeyManager[] keyManagers)
{
SSLServerSocketFactory res = null;
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(loadedKeyStore);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(keyManagers, trustManagerFactory.getTrustManagers(), null);
res = ctx.getServerSocketFactory();
}
catch (Exception e)
{
System.out.println(e.toString());
//throw new IOException(e.getMessage());
}
return res;
}
示例5: CustomHttpsTrust
import javax.net.ssl.SSLContext; //導入方法依賴的package包/類
/**
* 雙向證書的驗證,極少數的應用需要雙向證書驗證,比如銀行、金融類
*
* @param certificates
* @param bksFile
* @param password
*/
public CustomHttpsTrust(InputStream[] certificates, InputStream bksFile, String password) {
try {
TrustManager[] trustManagers = prepareTrustManager(certificates);
KeyManager[] keyManagers = prepareKeyManager(bksFile, password);
if (trustManagers == null) {
x509TrustManager = new UnSafeTrustManager();
} else {
for (TrustManager trustManager :
trustManagers) {
if (trustManager instanceof X509TrustManager) {
x509TrustManager = (X509TrustManager) trustManager;
break;
}
}
}
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagers, new TrustManager[]{x509TrustManager}, null);
sSLSocketFactory = sslContext.getSocketFactory();
} catch (Exception e) {
e.printStackTrace();
}
}
示例6: create
import javax.net.ssl.SSLContext; //導入方法依賴的package包/類
private void create(Path path)
throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException,
KeyManagementException {
TrustManager[] trustManagers;
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
installCertificates(path, keyStore);
String defaultAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(defaultAlgorithm);
trustManagerFactory.init(keyStore);
trustManagers = trustManagerFactory.getTrustManagers();
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagers, null);
trustManager = (X509TrustManager) trustManagers[0];
X509Certificate[] acceptedIssuers = trustManager.getAcceptedIssuers();
for (X509Certificate acceptedIssuer : acceptedIssuers) {
logger.info("installed cert details: subject={} issuer={}",
acceptedIssuer.getSubjectX500Principal(), acceptedIssuer.getIssuerX500Principal());
}
}
示例7: get
import javax.net.ssl.SSLContext; //導入方法依賴的package包/類
/**
* 鍙戦�丟et璿鋒眰
* @param url
* @return
* @throws NoSuchProviderException
* @throws NoSuchAlgorithmException
* @throws IOException
* @throws KeyManagementException
*/
public static String get(String url,Boolean https) throws NoSuchAlgorithmException, NoSuchProviderException, IOException, KeyManagementException {
StringBuffer bufferRes = null;
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 浠庝笂榪癝SLContext瀵矽薄涓緱鍒癝SLSocketFactory瀵矽薄
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL urlGet = new URL(url);
HttpsURLConnection http = (HttpsURLConnection) urlGet.openConnection();
// 榪炴帴瓚呮椂
http.setConnectTimeout(25000);
// 璿誨彇瓚呮椂 --鏈嶅姟鍣ㄥ搷搴旀瘮杈冩參锛屽澶ф椂闂�
http.setReadTimeout(25000);
http.setRequestMethod("GET");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setSSLSocketFactory(ssf);
http.setHostnameVerifier(new Verifier());
http.setDoOutput(true);
http.setDoInput(true);
http.connect();
InputStream in = http.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
String valueString = null;
bufferRes = new StringBuffer();
while ((valueString = read.readLine()) != null){
bufferRes.append(valueString);
}
in.close();
if (http != null) {
// 鍏抽棴榪炴帴
http.disconnect();
}
return bufferRes.toString();
}
示例8: createDefaultSSLSocketFactory
import javax.net.ssl.SSLContext; //導入方法依賴的package包/類
/**
* 創建一個默認的,空的信任管理工廠
*
* @return 返回創建的信任管理工廠
*/
private static SSLSocketFactory createDefaultSSLSocketFactory() throws Exception {
TrustManager[] tm = {new DefaultX509TrustManager()};
SSLContext sslContext = null;
SSLSocketFactory factory = null;
try {
sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new SecureRandom());
factory = sslContext.getSocketFactory();
} catch (NoSuchAlgorithmException | NoSuchProviderException | KeyManagementException e) {
logger.error(e.getMessage());
}
if (null == factory) {
throw new Exception("SSLSocketFactory is null");
}
return factory;
}
示例9: createSslSocketFactory
import javax.net.ssl.SSLContext; //導入方法依賴的package包/類
private SSLSocketFactory createSslSocketFactory() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, KeyManagementException {
// load up the key store
String STORETYPE = "JKS";
String KEYSTORE = "keystore.jks";
String STOREPASSWORD = "storepassword";
String KEYPASSWORD = "keypassword";
KeyStore ks = KeyStore.getInstance(STORETYPE);
ks.load(MattermostClient.class.getResourceAsStream(KEYSTORE), STOREPASSWORD.toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, KEYPASSWORD.toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
SSLContext sslContext = null;
sslContext = SSLContext.getInstance("TLS");
// sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
sslContext.init(null, null, null); // will use java's default key and trust store which is sufficient unless you deal with self-signed certificates
return sslContext.getSocketFactory();// (SSLSocketFactory) SSLSocketFactory.getDefault();
}
示例10: createSSLContext
import javax.net.ssl.SSLContext; //導入方法依賴的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;
}
示例11: createSSLContext
import javax.net.ssl.SSLContext; //導入方法依賴的package包/類
private SSLContext createSSLContext(OptionMap options) {
try {
SSLContext context = SSLContext.getInstance("TLSv1.2");
ReloadableTrustManager trustManager = getDependency(ReloadableTrustManager.class);
context.init(getKeyManagers(options),
trustManager.initialized() ? new TrustManager[]{ trustManager } : null,
null);
return context;
} catch (NoSuchAlgorithmException | KeyManagementException | UnrecoverableKeyException | CertificateException | KeyStoreException | IOException e) {
// TODO
throw new MisconfigurationException("bouncr.", e);
}
}
示例12: createSocketFactory
import javax.net.ssl.SSLContext; //導入方法依賴的package包/類
public static SSLSocketFactory createSocketFactory(
final String caCertificateFileName,
final String clientCertificateFileName,
final String clientKeyFileName) throws Exception
{
// Creates a TLS socket factory with the given
// CA certificate file, client certificate, client key
// In this case, we are working without a client key password
final String clientKeyPassword = "";
try
{
Security.addProvider(new BouncyCastleProvider());
final KeyManager[] keyManagers = createKeyManagerFactory(clientCertificateFileName, clientKeyFileName, clientKeyPassword).getKeyManagers();
final TrustManager[] trustManagers = createTrustManagerFactory(caCertificateFileName).getTrustManagers();
// Create the TLS socket factory for the desired TLS version
final SSLContext context = SSLContext.getInstance(TLS_VERSION);
context.init(keyManagers, trustManagers, new SecureRandom());
//context.init(keyManagers, trustManagers, null);
return context.getSocketFactory();
}
catch (Exception e)
{
throw new Exception("I cannot create the TLS socket factory.", e);
}
}
開發者ID:PacktPublishing,項目名稱:MQTT-Essentials-A-Lightweight-IoT-Protocol,代碼行數:29,代碼來源:SecurityHelper.java
示例13: httpsRequest
import javax.net.ssl.SSLContext; //導入方法依賴的package包/類
/**
* 發起https請求並獲取結果
*
* @param requestUrl
* 請求地址
* @param requestMethod
* 請求方式(GET、POST)
* @param outputStr
* 提交的數據
* @return JSONObject(通過JSONObject.get(key)的方式獲取json對象的屬性值)
*/
public static JSONObject httpsRequest(String requestUrl,String requestMethod,String outputStr){
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try {
// 創建SSLContext對象,並使用我們指定的信任管理器初始化
TrustManager[] tm = { new SaicX509TrustManager() };
// SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
SSLContext sslContext = SSLContext.getInstance("TLS", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 從上述SSLContext對象中得到SSLSocketFactory對象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
httpUrlConn.setSSLSocketFactory(ssf);
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// 設置請求方式(GET/POST)
httpUrlConn.setRequestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod))
httpUrlConn.connect();
// 當有數據需要提交時
if (null != outputStr) {
OutputStream outputStream = httpUrlConn.getOutputStream();
// 注意編碼格式,防止中文亂碼
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 將返回的輸入流轉換成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 釋放資源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
logger.error("connection timed out cause by " + ce.getMessage());
} catch (Exception e) {
logger.error("https request error : " + e.getMessage());
}
return jsonObject;
}
示例14: getServerSslContext
import javax.net.ssl.SSLContext; //導入方法依賴的package包/類
public static SSLContext getServerSslContext()
throws IOException, GeneralSecurityException
{
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(loadTrustStore(getCertificateChainFile()));
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(PemReader.loadKeyStore(getCertificateChainFile(), getPrivateKeyFile(), Optional.empty()), new char[0]);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
return sslContext;
}
示例15: httpsRequest
import javax.net.ssl.SSLContext; //導入方法依賴的package包/類
public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
JSONObject jsonObject = null;
try {
// 創建SSLContext對象,並使用我們指定的信任管理器初始化
TrustManager[] tm = { new HttpsX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 從上述SSLContext對象中得到SSLSocketFactory對象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(ssf);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36");
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type", "application/json;;charset=UTF-8");
// 設置請求方式(GET/POST)
conn.setRequestMethod(requestMethod);
// 當outputStr不為null時向輸出流寫數據
if (null != outputStr) {
OutputStream outputStream = conn.getOutputStream();
// 注意編碼格式
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 從輸入流讀取返回內容
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
// 釋放資源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
conn.disconnect();
jsonObject = JSONObject.parseObject(buffer.toString());
} catch (ConnectException ce) {
ce.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}