首页 > 解决方案 > 在 HTTP 标头中添加证书

问题描述

我想在 http 标头中添加一个证书。我的问题是:

  1. 我们必须将它作为字节数组发送吗?还是作为字符串?
  2. 如果是,java.securit.PublicKey 的 toString 方法是否构建了良好的字符串表示?
  3. 插入http头是不是太长了?谢谢

代码 :

public byte []   getCertificate() throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException {
    if (Configuration.getSignaturekeyStoreLocation()==null || ewalletConfiguration.getSignaturekeyStoreLocation().isEmpty()) {
            throw new IllegalArgumentException("Keystore url must not be null");
        }
        FileInputStream is = new FileInputStream(Configuration.getSignaturekeyStoreLocation());
        char[] keyPassword = ewalletConfiguration.getSignaturekeyStorePassword().toCharArray();
        KeyStore keystore = KeyStore.getInstance("JKS");
        //keyStore Password
        keystore.load(is, keyPassword);
        //
        final Certificate cert = (Certificate) keystore.getCertificate(Configuration.getSignatureCertificateAlias());

        return  cert.getEncoded();
}``

`

标签: javaspring-boot

解决方案


为什么在标头中有证书有不同的目的,所以如果你解释用例会更简单。

我们必须将它作为字节数组发送吗?还是作为字符串?

HTTP 标头始终是一个字符串,我假设您需要使用 base64 编码。

如果是,java.securit.PublicKey 的 toString 方法是否构建了良好的字符串表示?

不,公钥只是一个公钥,而不是整个证书(证书包含公钥)。获取 (x509) 证书表示的最简单方法是使用getEncoded方法

插入http头是不是太长了?谢谢

它可能。通常http服务器有一定的头缓冲区(例如apache tomat有/有默认的8kB缓冲区,IIS 16kB)所以如果返回的编码响应会更大,服务器可能会返回一个错误响应。即使我知道一些客户端/浏览器有不同的限制。

我很好奇您想要实现什么,也许有更强大的方法,例如在有效负载中发送必要的证书或仅发送证书签名


推荐阅读