Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AWS SES在spring boot java中实现,无需使用凭据_Java_Amazon Web Services_Spring Boot_Amazon Ses - Fatal编程技术网

AWS SES在spring boot java中实现,无需使用凭据

AWS SES在spring boot java中实现,无需使用凭据,java,amazon-web-services,spring-boot,amazon-ses,Java,Amazon Web Services,Spring Boot,Amazon Ses,我是一名后端开发人员(Spring boot),DevOps团队希望我实现AWS的SES和SNS服务,但不使用凭据,因为它们与AWS中的IAM角色存储相同的凭据,因此他们希望我在代码中传递唯一的主机,并将凭据部分留空,然后它将向特定收件人发送电子邮件。(根据他们的关注) 所以,正如他们所说,我在java中尝试了空凭据,但没有成功,这表明身份验证无效 我不知道他们是如何用PHPSDK实现同样的功能的,他们将凭证部分留空,它可以很好地使用IAM角色凭证 这是我的代码,我将凭证部分留空 private

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

所以,正如他们所说,我在java中尝试了空凭据,但没有成功,这表明身份验证无效

我不知道他们是如何用PHPSDK实现同样的功能的,他们将凭证部分留空,它可以很好地使用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(新的InternetAddress(varMap.get(“fromEmail”)、varMap.get(“fromName”);
使用
msg.setFrom(新的InternetAddress(“no-reply@lottoweaver.com,“lottowaver.com”);

并且在credentail部分中也将其替换为
transport.connect(varMap.get(“主机”)、varMap.get(“SMTP_用户名”)、varMap.get(“SMTP_密码”);
transport.connect(“email SMTP.us-west-2.amazonaws.com”),,,;

请给我指出一个实现这一目标的正确方法


提前感谢执行代码时,AWS SDK将使用凭证链查找您的凭证:

SDK将沿着这条链工作,尝试查找凭据。对于在本地计算机上工作的开发人员,您的凭据通常由a)aws凭据文件或b)设置
aws\u访问密钥\u ID
aws\u密码\u访问密钥
作为环境变量获取

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

简言之,当您在本地时,您需要传递凭据,但不要担心,如果您的同事已正确设置服务器,则当您在服务器上时,您不需要传递凭据

编辑:您需要使用JavaSDK来访问凭证链

请参考下面的链接,使用实例角色AWS SDK;正如您所说,我也尝试过使用服务器,但仍然得到相同的错误
javax.mail.AuthenticationFailedException
,并且它在PHPSorry上运行良好,我忽略了您没有使用AWS Java SDK的事实。您应该使用它来访问凭证链。SDK将使用服务器上的实例配置文件设置,前提是您不设置凭证链中的任何方法。(实例配置文件凭据是链中的第6个)感谢您的帮助…您能再帮我一点吗,例如我无法将主机连接到我的java SDK代码在这里。。。。我是说我该把主人放在哪里