【问题标题】:Storing external API key in AWS Elastic Beanstalk在 AWS Elastic Beanstalk 中存储外部 API 密钥
【发布时间】:2021-05-24 23:12:48
【问题描述】:

我在 AWS 上存储外部 API(我从中获取 Spring Boot 应用程序上的一些数据)私钥时遇到问题。它作为弹性豆茎环境属性存储在那里。我在本地测试了以下代码,它可以工作,但是当我使用测试环境(AWS)时,属性已正确加载,但出现错误:

Illegal base64 character a at a

私钥只是:

-----BEGIN PRIVATE KEY-----
(...)
-----END PRIVATE KEY-----

每当我从文件或作为弹簧属性加载它时,它都能很好地工作。但是,当它在 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));

【问题讨论】:

    标签: java amazon-web-services spring-boot passwords amazon-elastic-beanstalk


    【解决方案1】:

    两天后我们发现 .replaceAll("\r\n", "") 在 Windows 上已经足够了,但我们遇到了一个问题,因为在 Linux 上也应该添加 .replaceAll("\n", "")存储在 AWS 上的私钥的过程。

    【讨论】:

      猜你喜欢
      • 2016-10-12
      • 2018-06-30
      • 2015-09-20
      • 2019-12-14
      • 2020-08-24
      • 1970-01-01
      • 2011-08-03
      • 2019-08-13
      • 2018-06-27
      相关资源
      最近更新 更多