本文整理汇总了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;
}