增加两种证书获取方式

This commit is contained in:
egzosn
2019-10-13 23:20:57 +08:00
parent b28dc3a506
commit 409ed1df61
2 changed files with 71 additions and 8 deletions

View File

@@ -0,0 +1,22 @@
package com.egzosn.pay.common.api;
import java.io.IOException;
import java.io.InputStream;
/**
* 证书存储方式
* @author egan
* email egzosn@gmail.com
* date 2019/10/13.23:09
*/
public interface CertStore {
/**
* 证书信息转化为对应的输入流
*
* @param cert 证书信息
* @return 输入流
* @throws IOException 找不到文件异常
*/
public abstract InputStream getInputStream(Object cert) throws IOException;
}

View File

@@ -1,5 +1,8 @@
package com.egzosn.pay.common.bean;
import com.egzosn.pay.common.api.CertStore;
import com.egzosn.pay.common.http.HttpRequestTemplate;
import java.io.*;
/**
@@ -9,7 +12,7 @@ import java.io.*;
* email egzosn@gmail.com
* date 2019/4/14.23:04
*/
public enum CertStoreType {
public enum CertStoreType implements CertStore {
/**
* 路径,建议绝对路径
@@ -59,15 +62,53 @@ public enum CertStoreType {
public InputStream getInputStream(Object cert) throws IOException {
return (InputStream) cert;
}
};
},
/**
* 证书信息转化为对应的输入流
*
* @param cert 证书信息
* @return 输入流
* @throws IOException 找不到文件异常
* URL获取的方式
*/
public abstract InputStream getInputStream(Object cert) throws IOException;
URL {
/**
* 证书信息转化为对应的输入流
*
* @param url 获取证书信息的URL
* @return 输入流
* @throws IOException 找不到文件异常
*/
@Override
public InputStream getInputStream(Object url) throws IOException {
return new HttpRequestTemplate().getForObject((String) url, InputStream.class);
}
},
/**
* URL获取的方式
*/
BEAN {
/**
* 证书信息转化为对应的输入流
*
* @param beanClazz 获取证书信息的类路径(字符串),该类必须实现{@link CertStore}
* @return 输入流
* @throws IOException 找不到文件异常
*/
@Override
public InputStream getInputStream(Object beanClazz) throws IOException {
try {
Class<?> clazz = Class.forName((String) beanClazz);
CertStore certStore = (CertStore)clazz.newInstance();
return certStore.getInputStream(beanClazz);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
return null;
}
};
}