【发布时间】:2013-03-26 20:19:33
【问题描述】:
我已经通过 API 生成了一个 PKCS12 密钥库,但是进程的返回是一个 KeyStore 对象。我需要发送,在客户端发送申请时直接发送到浏览器下载。
我该怎么做?
我正在使用 java 和 jboss 5AS。
【问题讨论】:
我已经通过 API 生成了一个 PKCS12 密钥库,但是进程的返回是一个 KeyStore 对象。我需要发送,在客户端发送申请时直接发送到浏览器下载。
我该怎么做?
我正在使用 java 和 jboss 5AS。
【问题讨论】:
您可以使用KeyStore#store() 将其写入OutputStream。
keyStore.store(outputStream, password);
基本上就是这样。 OutputStream 可能是 HTTP 响应之一。有关如何在 JSF 中提供文件下载的通用启动示例,其中您需要集成此行,请访问此答案:How to provide a file download from a JSF backing bean? 使用 content type 的 application/x-pkcs12。
【讨论】:
代码如下:
public void cadastrar () throws Exception
{
byte[] encodedKeyStore = controlador.cadastrar(certificadoModel);
java.security.KeyStore keyStore = java.security.KeyStore.getInstance("PKCS12");
keyStore.load(new ByteArrayInputStream(encodedKeyStore), certificadoModel.getPassword().toCharArray());
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
ec.responseReset();
ec.setResponseContentType("application/x-pkcs12");
//ec.setResponseContentLength(contentLength);
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + certificadoModel.getUsername() + ".p12" + "\"");
OutputStream output = ec.getResponseOutputStream();
keyStore.store(output, certificadoModel.getPassword().toCharArray());
fc.responseComplete();
}
【讨论】: