如何在Java中通过AWS IOT X.509证书路径

时间:2019-04-19 12:46:00

标签: java x509certificate aws-iot

我正在使用aws lambda函数,该函数在内部使用aws iot核心将消息发布到已连接的设备。我已经从aws控制台生成了x509证书,并将其保留在spring boot项目的resources文件夹中,但是调用了lambda函数抛出FileNotFoundException

我已经尝试了以下方法:

1)

    ClassLoader classLoader = getClass().getClassLoader();
    File cityFile = new 
    File(classLoader.getResource("test.txt").getFile());

2)在构建标签内的pom.xml中添加了以下资源标签

    <resources>
        <resource>
            <directory>resources</directory>
                <targetPath>${project.build.outputDirectory}</targetPath>
                <includes>
                    <include>7913fa14e2-certificate.pem.crt</include>
                    <include>7913fa14e2-private.pem.key</include>
                </includes>
        </resource>
    </resources>

3)我的字段:

    private static final String certificateFile = "src/main/resources/7913fa14e2-certificate.pem.crt";

4)代码:

    File file = new File(certificateFile);
    if (!file.exists()) {
        System.out.println("Certificate file: " + filename + " is not found.");
        return null;
    }

    try (BufferedInputStream stream = new BufferedInputStream(new 
    FileInputStream(file))) {
        final CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        return (List<Certificate>) certFactory.generateCertificates(stream);
    } catch (IOException | CertificateException e) {
        System.out.println("Failed to load certificate file " + filename);
    }
    return null;

5)错误:

    File file = new File(filename);

此行抛出FileNotFoundException

0 个答案:

没有答案