代码之家  ›  专栏  ›  技术社区  ›  Stephane

如何让javamailsender在错误的邮件密码上加载应用程序上下文失败?

  •  0
  • Stephane  · 技术社区  · 6 年前

    我在一个sring引导应用程序中使用javamilsender,例如,如果一个属性(比如密码)是错误的,从而阻止了在邮件网关中的登录,那么它就不能自动连接bean,应用程序上下文也无法加载。

    Spring邮件属性:

    spring.mail.host=smtp.gmail.com
    spring.mail.port=465
    spring.mail.protocol=smtp
    spring.mail.username=xxx@gmail.com
    spring.mail.password=xxx
    spring.mail.from=xxx@gmail.com
    

    属性由属性读取器加载:

    public abstract class AbstractUserProperties implements ApplicationProperties {
    
        @Value("${" + PropertyNames.CONFIG_AUTH_TOKEN_PRIVATE_KEY + "}")
        private String authenticationTokenPrivateKey;
    
        @Value("${" + PropertyNames.CONFIG_MAIL_HOST + "}")
        private String host;
    
        @Value("${" + PropertyNames.CONFIG_MAIL_PORT + "}")
        private String port;
    
        @Value("${" + PropertyNames.CONFIG_MAIL_PROTOCOL + "}")
        private String protocol;
    
        @Value("${" + PropertyNames.CONFIG_MAIL_USERNAME + "}")
        private String username;
    
        @Value("${" + PropertyNames.CONFIG_MAIL_PASSWORD + "}")
        private String password;
    
        @Value("${" + PropertyNames.CONFIG_MAIL_FROM + "}")
        private String mailFrom;
    
        @Value("${" + PropertyNames.CONFIG_MAIL_TEST_CONNECTION + "}")
        private boolean mailTestConnection;
    
        @Value("${" + PropertyNames.CONFIG_MAILING_ENABLED + "}")
        private boolean mailingEnabled;
    
        public String getAuthenticationTokenPrivateKey() {
            return authenticationTokenPrivateKey;
        }
    
        @Override
        public String getHost() {
            return host;
        }
    
        @Override
        public String getPort() {
            return port;
        }
    
        @Override
        public String getProtocol() {
            return protocol;
        }
    
        @Override
        public String getUsername() {
            return username;
        }
    
        @Override
        public String getPassword() {
            return password;
        }
    
        @Override
        public String getMailFrom() {
            return mailFrom;
        }
    
        @Override
        public boolean getMailTestConnection() {
            return mailTestConnection;
        }
    
        @Override
        public boolean getMailingEnabled() {
            return mailingEnabled;
        }
    
    }
    

    如何使错误的邮件密码不妨碍应用程序启动?

    依赖项:

    <version>2.0.3.RELEASE</version>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    

    我在 Spring Boot 2.0.3 .

    以下是应用程序的邮件配置:

    @Configuration
    public class MailConfig {
    
        @Autowired
        private ApplicationProperties applicationProperties;
    
        @Bean
        public JavaMailSender javaMailSender() {
            String host = applicationProperties.getHost();
            String port = applicationProperties.getPort();
            String protocol = applicationProperties.getProtocol();
            String username = applicationProperties.getUsername();
            String password = applicationProperties.getPassword();
            if (!password.isEmpty() && !username.isEmpty() && !protocol.isEmpty() && !port.isEmpty() && !host.isEmpty()) {
                JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
                javaMailSender.setHost(host);
                javaMailSender.setPort(Integer.parseInt(port));
                javaMailSender.setProtocol(protocol);
                javaMailSender.setUsername(username);
                javaMailSender.setPassword(password);
                javaMailSender.setJavaMailProperties(getMailProperties());
                return javaMailSender;
            } else {
                return null;
            }
        }
    
        @Bean
        public SimpleMailMessage simpleMailMessage() {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom(applicationProperties.getMailFrom());
            return simpleMailMessage;
        }
    
        private Properties getMailProperties() {
            Properties properties = new Properties();
            properties.setProperty("mail.smtp.auth", "true");
            properties.setProperty("mail.smtp.starttls.enable", "false");
            properties.setProperty("mail.smtp.quitwait", "false");
            properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            properties.setProperty("mail.smtp.socketFactory.fallback", "false");
            properties.setProperty("mail.debug", "true");
            return properties;
        }
    
    }
    

    更新: 我已经把 spring.mail.test-connection=false 中的属性 application.properties 文件和控制台日志显示它已被复制:

    [INFO] Copying 1 resource
    [DEBUG] Copying file application.properties
    

    我也移除了 MailConfig 类,以便使用默认情况下由Spring提供的。 但是,错误消息仍然相同。 作为附带说明,我想知道如何在自己的bean配置中使用该属性,而不必使用默认的Springbean。

    更新: 我可以加上 mail.test-connection 配置bean中的属性,如:

    private Properties getMailProperties() {
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.smtp.starttls.enable", "false");
        properties.setProperty("mail.smtp.quitwait", "false");
        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.setProperty("mail.smtp.socketFactory.fallback", "false");
        properties.setProperty("mail.debug", "true");
        properties.setProperty("mail.test-connection", String.valueOf(applicationProperties.getMailTestConnection()));
        return properties;
    }
    
    @Bean
    public JavaMailSender javaMailSender() {
        String host = applicationProperties.getHost();
        String port = applicationProperties.getPort();
        String protocol = applicationProperties.getProtocol();
        String username = applicationProperties.getUsername();
        String password = applicationProperties.getPassword();
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost(host);
        javaMailSender.setPort(Integer.parseInt(port));
        javaMailSender.setProtocol(protocol);
        javaMailSender.setUsername(username);
        javaMailSender.setPassword(password);
        javaMailSender.setJavaMailProperties(getMailProperties());
        return javaMailSender;
    }
    

    现在,即使没有提供密码,没有检查连接,应用程序上下文也可以正常加载。

    1 回复  |  直到 6 年前
        1
  •  1
  •   rieckpil    6 年前

    application.properties

    spring.mail.test-connection=false

    :如果您想了解Spring Boot的工作原理,请查看 XYZAutoConfiguration MailSender 它们使用以下代码测试连接:

    /**
     * {@link EnableAutoConfiguration Auto configuration} for testing mail service
     * connectivity on startup.
     *
     * @author Eddú Meléndez
     * @author Stephane Nicoll
     * @since 1.3.0
     */
    @Configuration
    @AutoConfigureAfter(MailSenderAutoConfiguration.class)
    @ConditionalOnProperty(prefix = "spring.mail", value = "test-connection")
    @ConditionalOnSingleCandidate(JavaMailSenderImpl.class)
    public class MailSenderValidatorAutoConfiguration {
    
        private final JavaMailSenderImpl mailSender;
    
        public MailSenderValidatorAutoConfiguration(JavaMailSenderImpl mailSender) {
            this.mailSender = mailSender;
        }
    
        @PostConstruct
        public void validateConnection() {
            try {
                this.mailSender.testConnection();
            }
            catch (MessagingException ex) {
                throw new IllegalStateException("Mail server is not available", ex);
            }
        }
    
    }
    
    推荐文章