在 AWS Elastic Beanstalk 中存储外部 API 密钥
Storing external API key in AWS Elastic Beanstalk
我在 AWS 上存储外部 API(我正在 Spring 引导应用程序上从中获取一些数据)私钥时遇到问题。它在那里存储为弹性 beantalk 环境 属性。我在本地测试了下面的代码并且它有效但是当我使用测试环境(AWS)时 属性 被正确加载,但是我收到一个错误:
Illegal base64 character a at a
私钥就是:
-----BEGIN PRIVATE KEY-----
(...)
-----END PRIVATE KEY-----
每当我从文件或作为 spring 属性 加载它时,它都运行良好。但是当它在 AWS 上加载为 env 变量时,我得到了提到的错误。
@Component
@Slf4j
public class SfKeyLoaderImpl implements SfKeyLoader {
@Value("${access-token-params.private-key}")
private String privateKeyString;
@Override
public PrivateKey loadKey() {
PrivateKey privateKey = null;
try {
String formattedKey = privateKeyString
.replaceAll("\r\n", "")
.replace("-----BEGIN PRIVATE KEY-----", "")
.replace("-----END PRIVATE KEY-----", "")
.replaceAll(" ", "");
KeyFactory factory = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(formattedKey));
privateKey = factory.generatePrivate(privSpec);
} catch (Exception ex) {
log.error("Error while loading key for salesforce jwt generation", ex);
}
return privateKey;
}
}
代码抛出错误的部分:
PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(formattedKey));
两天后我们发现 .replaceAll("\r\n", "") 在 Windows 上就足够了,我们遇到了问题,因为在 Linux 上也 .replaceAll(" \n", "") 应添加到存储在 AWS 上的私钥的过程中。
我在 AWS 上存储外部 API(我正在 Spring 引导应用程序上从中获取一些数据)私钥时遇到问题。它在那里存储为弹性 beantalk 环境 属性。我在本地测试了下面的代码并且它有效但是当我使用测试环境(AWS)时 属性 被正确加载,但是我收到一个错误:
Illegal base64 character a at a
私钥就是:
-----BEGIN PRIVATE KEY-----
(...)
-----END PRIVATE KEY-----
每当我从文件或作为 spring 属性 加载它时,它都运行良好。但是当它在 AWS 上加载为 env 变量时,我得到了提到的错误。
@Component
@Slf4j
public class SfKeyLoaderImpl implements SfKeyLoader {
@Value("${access-token-params.private-key}")
private String privateKeyString;
@Override
public PrivateKey loadKey() {
PrivateKey privateKey = null;
try {
String formattedKey = privateKeyString
.replaceAll("\r\n", "")
.replace("-----BEGIN PRIVATE KEY-----", "")
.replace("-----END PRIVATE KEY-----", "")
.replaceAll(" ", "");
KeyFactory factory = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(formattedKey));
privateKey = factory.generatePrivate(privSpec);
} catch (Exception ex) {
log.error("Error while loading key for salesforce jwt generation", ex);
}
return privateKey;
}
}
代码抛出错误的部分:
PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(formattedKey));
两天后我们发现 .replaceAll("\r\n", "") 在 Windows 上就足够了,我们遇到了问题,因为在 Linux 上也 .replaceAll(" \n", "") 应添加到存储在 AWS 上的私钥的过程中。