1

我正在使用 Spring (Java) 开发一个微服务,它需要针对外部 Web 服务进行身份验证才能执行一些 REST API 调用。

外部 Web 服务给了我两个文件,一个安全证书 (.cer) 和一个 PEM 文件 (.pem)

现在我想了解,如何将给定的证书存储在信任库中?所以当我调用他们的 API 端点时,我不会得到状态码 401。

我试图使用 cacerts,但可能我并不完全了解事情是如何运作的。

这就是我现在所处的位置:

public class SSLLoader
{

    private static final Logger log = Logger.getLogger(SSLLoader.class);

    public SSLLoader() throws Exception
    {
        loadSSLCerts();
    }

    private void loadSSLCerts() throws Exception
    {
        try
        {
            SslContextUtils.mergeWithSystem(SSLLoader.class.getResourceAsStream("/sa/cacerts"));             
            log.info("Loaded SSL from sa/cacerts");
        }
        catch (Throwable t)
        {
            throw new Exception("Failed to load SSL certs: " + t.getMessage(), t);
        }
    }

    private InputStream fileToStream(String file) throws Exception
    {
        File initialFile = new File(file);
        InputStream resourceAsStream = new FileInputStream(initialFile);
        if (null == resourceAsStream)
        {
            throw new Exception("File cannot be found - " +file);
        }
        return resourceAsStream;
    }
}
4

0 回答 0