【问题标题】:AWS SES implement In spring boot java without using credentialAWS SES在spring boot java中实现而不使用凭证
【发布时间】:2023-03-13 11:18:02
【问题描述】:

我是后端开发人员(Spring boot),DevOps 团队希望我实现 AWS 的 SES 和 SNS 服务,但不使用凭证,因为它们与 IAM 角色在 aws 中存储相同的凭证,因此他们希望我通过仅在代码中托管并将凭据部分留空,它将向特定收件人发送电子邮件。(根据他们的关注)

正如他们所说,我确实尝试在 java 中使用空凭据,但没有成功,它说身份验证无效

我不知道如何,但他们确实使用 PHP SDK 实现了同样的事情,他们将凭证部分留空,并且它适用于 IAM 角色凭证

这是我的代码 我将凭证部分留空的地方

private boolean sendAwsMail() {
        String result = null;
        boolean flag = true;
        try {
            Properties props = System.getProperties();
            props.put("mail.transport.protocol", varMap.get("protocol"));
            props.put("mail.smtp.port", Integer.parseInt(varMap.get("port")));
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.auth", "true");
            Session session = Session.getDefaultInstance(props);
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("no-reply@lottoweaver.com", "lottoweaver.com"));
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(varMap.get("destinationMailId")));
            msg.setSubject(varMap.get("subject"));
            msg.setContent(varMap.get("content"), "text/html");
            Transport transport = session.getTransport();
            transport.connect("email-smtp.us-west-2.amazonaws.com", "", "");
            transport.sendMessage(msg, msg.getAllRecipients());
            AppLogger.writeLog("INFO", "MAIL_SEND", "send_mail", this.getClass().getName(),
                    Thread.currentThread().getStackTrace()[1].getMethodName(),
                    "toMailId:" + varMap.get("destinationMailId"), "subject:" + varMap.get("subject"), "", "", null,
                    null, "");
            result = varMap.get("content");
            transport.close();
        } catch (Exception ex) {
            AppLogger.writeLog("WARNING", "Issue_In_Connection", "send_mail", this.getClass().getName(),
                    Thread.currentThread().getStackTrace()[1].getMethodName(),
                    "toMailId:" + varMap.get("destinationMailId"), "subject:" + varMap.get("subject"), "", "", null, ex,
                    "");
            flag = false;
            result = ex.toString();
        } finally {
            this.flag = flag;
            dumpList.add(new MessageDataDump(varMap.get("destinationMailId"), result, varMap, "AWS-EMAIL"));
            return flag;
        }
    }

我替换了msg.setFrom(new InternetAddress(varMap.get("fromEmail"), varMap.get("fromName"))); msg.setFrom(new InternetAddress("no-reply@lottoweaver.com", "lottoweaver.com"));

并且还将 transport.connect(varMap.get("HOST"), varMap.get("SMTP_USERNAME"), varMap.get("SMTP_PASSWORD")); 的 credentail 部分替换为 transport.connect("email-smtp.us-west-2.amazonaws.com", "", "");

请指出一个正确的方法来实现这一点。

提前致谢

【问题讨论】:

标签: java amazon-web-services spring-boot amazon-ses


【解决方案1】:

当您执行代码时,AWS 开发工具包将使用凭证链来查找您的凭证: https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html

SDK 将沿着这条链工作,试图找到凭据。对于在本地计算机上工作的开发人员,您的凭据通常由 a) aws 凭据文件或 b) 将 AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY 设置为环境变量。

当您在服务器上运行代码时,情况会略有不同。在这种情况下,凭证将应用于实例角色。基本上,不需要传递任何凭证,因为代码位于 AWS 服务器上,AWS 知道它拥有哪些权限,这是通过 IAM 实例角色设置的。

简而言之,您在本地时需要传递凭据,但不用担心,如果您的同事正确设置了服务器,那么您在服务器上时不需要传递凭据。

编辑:您将需要使用 Java SDK 来访问凭证链 https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-sdk-java.html

【讨论】:

  • 我也按照你的说法尝试了服务器,但仍然得到相同的错误javax.mail.AuthenticationFailedException,它在 PHP 上运行良好
  • 抱歉,我错过了您没有使用 AWS Java 开发工具包这一事实。您应该使用它来访问凭证链。 docs.aws.amazon.com/ses/latest/DeveloperGuide/…
  • SDK 将使用来自服务器的实例配置文件设置,假设您没有在凭证链中进一步设置任何方法。 (实例配置文件凭证在链中排名第 6)
  • 感谢您的帮助...能否请您再帮我一些忙,例如我无法将主机附加到我的 java SDK 代码docs.aws.amazon.com/ses/latest/DeveloperGuide/… 在这里...我的意思是我应该把主机放在哪里
猜你喜欢
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
  • 1970-01-01
  • 2019-03-27
  • 1970-01-01
  • 2018-04-07
  • 2020-01-23
相关资源
最近更新 更多