http配置加入ssl证书输入流

This commit is contained in:
egan
2019-01-06 13:15:34 +08:00
parent c4fb53c416
commit f4fada0973
2 changed files with 41 additions and 8 deletions

View File

@@ -1,6 +1,8 @@
package com.egzosn.pay.common.http;
import java.io.*;
/**
* HTTP 配置
* @author: egan
@@ -34,8 +36,9 @@ public class HttpConfigStorage {
/**
* https请求所需的证书PKCS12
* 证书内容
*/
private String keystore;
private Object keystore;
/**
* 证书对应的密码
*/
@@ -144,7 +147,7 @@ public class HttpConfigStorage {
*/
@Deprecated
public String getKeystorePath() {
return keystore;
return (String) keystore;
}
/**
@@ -178,17 +181,47 @@ public class HttpConfigStorage {
* 获取证书信息
* @return 证书信息 根据 {@link #isPath()}进行区别地址与信息串
*/
public String getKeystore() {
return keystore;
public InputStream getKeystoreInputStream() throws FileNotFoundException, UnsupportedEncodingException {
if (null == keystore){
return null;
}
if(isPath()){
return new FileInputStream(new File(getKeystoreStr()));
}
if(this.keystore instanceof String){
return new ByteArrayInputStream(getKeystoreStr().getBytes("ISO-8859-1"));
}
return (InputStream) keystore;
}
/**
* 获取证书信息
* @return 证书信息 根据 {@link #isPath()}进行区别地址与信息串
*/
public Object getKeystore() {
return keystore;
}
/**
* 获取证书信息 证书地址
* @return 证书信息 根据 {@link #isPath()}进行区别地址与信息串
*/
public String getKeystoreStr() {
return (String) keystore;
}
/**
* 设置证书
* @param keystore 证书信息
* 设置证书字符串信息或证书绝对地址
* @param keystore 证书信息字符串信息或证书绝对地址
*/
public void setKeystore(String keystore) {
this.keystore = keystore;
}
/**
* 设置证书字符串信息输入流
* @param keystore 证书信息 输入流
*/
public void setKeystore(InputStream keystore) {
this.keystore = keystore;
}
/**
* 证书对应的密码

View File

@@ -103,7 +103,7 @@ public class HttpRequestTemplate {
*/
public SSLConnectionSocketFactory createSSL( HttpConfigStorage configStorage){
if (StringUtils.isEmpty(configStorage.getKeystore())){
if (null == configStorage.getKeystore()){
try {
return new SSLConnectionSocketFactory(SSLContext.getDefault());
} catch (NoSuchAlgorithmException e) {
@@ -112,7 +112,7 @@ public class HttpRequestTemplate {
}
//读取本机存放的PKCS12证书文件
try(InputStream instream = configStorage.isPath() ? new FileInputStream(new File(configStorage.getKeystore())) : new ByteArrayInputStream(configStorage.getKeystore().getBytes("ISO-8859-1"))){
try(InputStream instream = configStorage.getKeystoreInputStream()){
//指定读取证书格式为PKCS12
KeyStore keyStore = KeyStore.getInstance("PKCS12");