如何在java中通过aws iot x.509证书路径
我正在使用aws lambda函数,该函数在内部使用aws iot core将消息发布到连接的设备。我从aws控制台生成了x509证书,并将其保存在spring boot项目的resources文件夹中,但调用lambda函数抛出如何在java中通过aws iot x.509证书路径,java,x509certificate,aws-iot,Java,X509certificate,Aws Iot,我正在使用aws lambda函数,该函数在内部使用aws iot core将消息发布到连接的设备。我从aws控制台生成了x509证书,并将其保存在spring boot项目的resources文件夹中,但调用lambda函数抛出FileNotFoundException 我已经尝试过以下方法: (一) 2) 在pom.xml内部构建标记中添加了以下资源标记 <resources> <resource> <direct
FileNotFoundException
我已经尝试过以下方法:
(一)
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>
4) 代码:
这一行抛出了
FileNotFoundException
您是否考虑过使用来避免存储和读取证书的需要?@blt我已经在使用AWS SDK,我们在AWS iot core SDK中有两个选项来创建awsIotClient对象。一种是通过传递certifcate和私有文件路径,另一种是使用awsAccessKeyId和awsSecretAccessKey。我可以通过第二种方法创建对象,但我不确定这是理想的还是安全的方法。这就是为什么我想探索第一种方法来获取这个awsIotClient对象。好的-您可能想尝试在路径中使用LAMBDA_TASK_ROOT来引用您的证书。链接到此环境变量:您考虑过使用为了避免存储和读取证书的需要?@blt我已经在使用AWS SDK,我们在AWS iot core SDK中有两个选项来创建awsIotClient对象。一种是通过传递certifcate和私有文件路径,另一种是使用awsAccessKeyId和awsSecretAccessKey。我可以通过第二种方法创建对象,但我不确定这是理想的还是安全的方法。这就是为什么我想探索第一种方法来获取此awsIotClient对象。好的-您可能希望尝试在路径中使用LAMBDA_TASK_ROOT来引用您的证书。链接到此环境变量:
<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>
private static final String certificateFile = "src/main/resources/7913fa14e2-certificate.pem.crt";
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;
File file = new File(filename);