Grails 4:邮件插件在 Elastic Beanstalk 上运行时无法读取“密码”属性

Posted

技术标签:

【中文标题】Grails 4:邮件插件在 Elastic Beanstalk 上运行时无法读取“密码”属性【英文标题】:Grails 4: Mail plugin fails to read 'password' property when running on Elastic Beanstalk 【发布时间】:2021-12-27 04:40:54 【问题描述】:

我的 Grails 4 应用程序在本地运行良好,但在 Elastic Beanstalk 中运行时无法发送电子邮件。我正在使用嵌入式(非外部)Tomcat 服务器在 Java 11 平台上使用可运行的 WAR 文件。

我通过GRAILS_MAIL_PASSWORD 环境变量在外部设置grails.mail.password 属性。

我正在检查BootStrap.groovyEmailsService 中的配置值,并且该值设置正确。

引导带

class BootStrap 

    GrailsApplication grailsApplication

    def init =  servletContext ->
        String config = "\n"
        grailsApplication.config.each 
            config += " --> $it.key : $grailsApplication.config.getProperty(it.key)\n"
        
        log.info(config)

电子邮件服务

class EmailService 

    @Value('$cloudcard.email.doNotSend')
    boolean doNotSend

    @Value('$grails.mail.username')
    String grailsMailUsername

    @Value('$grails.mail.password')
    String grailsMailPassword

    @PostConstruct
    void init() 
        log.info("grailsMailUsername: $grailsMailUsername.take(2)****$grailsMailUsername.reverse().take(2).reverse()")
        log.info("grailsMailPassword: $grailsMailPassword.take(4)****$grailsMailPassword.reverse().take(4).reverse()")
    

邮件服务日志输出

Nov 16 02:48:32 ip-172-31-31-36 web: 2021-11-16 02:48:32.946  INFO --- [io-5000-exec-10] us.cloudcard.api.EmailService            : grailsMailUsername: AK****G2
Nov 16 02:48:32 ip-172-31-31-36 web: 2021-11-16 02:48:32.946  INFO --- [io-5000-exec-10] us.cloudcard.api.EmailService            : grailsMailPassword: BI****5Z
Nov 16 02:48:33 ip-172-31-31-36 web: 2021-11-16 02:48:33.311 ERROR --- [pool-4-thread-1] grails.plugins.mail.MailMessageBuilder   : Failed to send email
Nov 16 02:48:33 ip-172-31-31-36 web: org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: 
failed to connect, no password specified?
Nov 16 02:48:33 ip-172-31-31-36 web: at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:440)
Nov 16 02:48:33 ip-172-31-31-36 web: at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:361)
Nov 16 02:48:33 ip-172-31-31-36 web: at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:356)
Nov 16 02:48:33 ip-172-31-31-36 web: at org.springframework.mail.javamail.JavaMailSender$send$0.call(Unknown Source)
Nov 16 02:48:33 ip-172-31-31-36 web: at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
Nov 16 02:48:33 ip-172-31-31-36 web: at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
Nov 16 02:48:33 ip-172-31-31-36 web: at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:127)
Nov 16 02:48:33 ip-172-31-31-36 web: at grails.plugins.mail.MailMessageBuilder$_sendMessage_closure1.doCall(MailMessageBuilder.groovy:124)
Nov 16 02:48:33 ip-172-31-31-36 web: at grails.plugins.mail.MailMessageBuilder$_sendMessage_closure1.doCall(MailMessageBuilder.groovy)
Nov 16 02:48:33 ip-172-31-31-36 web: at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Nov 16 02:48:33 ip-172-31-31-36 web: at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
Nov 16 02:48:33 ip-172-31-31-36 web: at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Nov 16 02:48:33 ip-172-31-31-36 web: at java.base/java.lang.reflect.Method.invoke(Method.java:566)
Nov 16 02:48:33 ip-172-31-31-36 web: at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:101)
Nov 16 02:48:33 ip-172-31-31-36 web: at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:323)
Nov 16 02:48:33 ip-172-31-31-36 web: at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:263)
Nov 16 02:48:33 ip-172-31-31-36 web: at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1041)
Nov 16 02:48:33 ip-172-31-31-36 web: at groovy.lang.Closure.call(Closure.java:405)
Nov 16 02:48:33 ip-172-31-31-36 web: at groovy.lang.Closure.call(Closure.java:399)
Nov 16 02:48:33 ip-172-31-31-36 web: at groovy.lang.Closure.run(Closure.java:486)
Nov 16 02:48:33 ip-172-31-31-36 web: at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
Nov 16 02:48:33 ip-172-31-31-36 web: at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
Nov 16 02:48:33 ip-172-31-31-36 web: at java.base/java.lang.Thread.run(Thread.java:829)
Nov 16 02:48:33 ip-172-31-31-36 web: Caused by: javax.mail.AuthenticationFailedException: failed to connect, no password specified?
Nov 16 02:48:33 ip-172-31-31-36 web: at javax.mail.Service.connect(Service.java:400)
Nov 16 02:48:33 ip-172-31-31-36 web: at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:518)
Nov 16 02:48:33 ip-172-31-31-36 web: at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:437)
Nov 16 02:48:33 ip-172-31-31-36 web: ... 22 common frames omitted

但是……

...如果我将用户名和密码硬编码到application.groovy,一切正常,我会在收件箱中收到电子邮件。我不确定这是否重要,但这是我在application.groovy 中配置的唯一内容之一。几乎所有其他内容都在 application.yml 中。

您知道为什么 Grails 邮件插件可能会忽略我的外部配置吗?

【问题讨论】:

【参考方案1】:

作为一种解决方法,我使用外部 Tomcat 容器平台(Amazon Linux 2;Tomcat 8.5;Corretto Java 11)重新部署了相同的应用程序,并且该应用程序现在尊重环境变量。

【讨论】:

以上是关于Grails 4:邮件插件在 Elastic Beanstalk 上运行时无法读取“密码”属性的主要内容,如果未能解决你的问题,请参考以下文章

Grails 4:不读取 Elastic Beanstalk 中的环境变量

在 Grails 的 Spring Security 插件中使用电子邮件和用户名作为登录名

在 AWS Elastic Beanstalk 上启动期间 Grails 4 应用程序停止

Grails 的用户管理插件/框架?

插件安装后 Grails Apache 中断?

如何在 grails messages.properties 中使用 HTML 进行邮件发送