如何在Spring ImapiderChannel Adapter中使用加密存储uri

如何在Spring ImapiderChannel Adapter中使用加密存储uri,spring,spring-integration,imap,Spring,Spring Integration,Imap,弹簧配置示例如下所示 <int-mail:imap-idle-channel-adapter id="mailAdapter" store-uri="imaps://${"username"}:${"password"}@imap-server:993/INBOX" java-mail-properties="javaMailProperties" channel="emails" should-delete-messages="false" sho

弹簧配置示例如下所示

<int-mail:imap-idle-channel-adapter id="mailAdapter"
    store-uri="imaps://${"username"}:${"password"}@imap-server:993/INBOX"
    java-mail-properties="javaMailProperties"
    channel="emails"
    should-delete-messages="false"
    should-mark-messages-as-read="true">
</int-mail:imap-idle-channel-adapter>
还创建了空的errorChannel、outputChannel等。我注意到Spring创建了两个实例,一个是xml配置,另一个是java@Configuration。在这里,它应该只使用java配置。如果我删除xml配置标记 然后,它为我的mailReceiver提供sigle imap实例,但只运行一次,不会定期运行。也不显示IMAPS日志


只是想知道我是否需要做这么多来加密密码。我的方法有问题。

使用Java配置而不是XML

@Configuration
public class MyConfigClass {

    @Bean
    public MyMailReceiver receiver() {
        ...
    }

    @Bean
    public ImapIdleChannelAdapter adapter() {
        ImapIdleChannelAdapter adapter = new ImapIdleChannelAdapter(receiver());
        ...
        return adapter;
    }

}
如果您将XML用于其他所有内容,只需将此类作为
添加到您的XML中即可

编辑

这里有一个对我来说很好的例子

@SpringBootApplication
public class So42298254Application {

    public static void main(String[] args) {
        SpringApplication.run(So42298254Application.class, args);
    }

    @Bean
    public TestMailServer.ImapServer imapServer() {
        return TestMailServer.imap(0);
    }

    @Bean
    public ImapMailReceiver receiver() {
        ImapMailReceiver imapMailReceiver = new ImapMailReceiver(imapUrl("user", "pw"));
        imapMailReceiver.setHeaderMapper(new DefaultMailHeaderMapper()); // converts the MimeMessage to a String
        imapMailReceiver.setUserFlag("testSIUserFlag"); // needed by the SI test server
        Properties javaMailProperties = new Properties();
        javaMailProperties.put("mail.debug", "true");
        imapMailReceiver.setJavaMailProperties(javaMailProperties);
        return imapMailReceiver;
    }

    private String imapUrl(String user, String pw) {
        return "imap://"
                + user + ":" + pw
                + "@localhost:" + imapServer().getPort() + "/INBOX";
    }

    @Bean
    public ImapIdleChannelAdapter adapter() {
        ImapIdleChannelAdapter adapter = new ImapIdleChannelAdapter(receiver());
        adapter.setOutputChannelName("handleMail");
        return adapter;
    }

    @ServiceActivator(inputChannel = "handleMail")
    public void handle(String mail, @Header(MailHeaders.FROM) Object from) {
        System.out.println(mail + " from:" + from);
        imapServer().resetServer(); // so we'll get the email again
    }

}

我的意图是在属性文件中使用加密密码。 因此,我改变了进入电子邮件接收课程的方式。我添加了继承的PropertyPlaceHolderConfigure并实现了方法convertPropertyValue(),如下所示

public class EncryptationAwarePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    private static final Logger logger  = LoggerFactory.getLogger(EncryptationAwarePropertyPlaceholderConfigurer.class);

    @Override
    protected String convertPropertyValue(String originalValue) {
        if (originalValue.contains("{<ENC>}") && originalValue.contains("{</ENC>}")) {
            String encryptedTaggedValue = originalValue.substring(originalValue.indexOf("{<ENC>}"), originalValue.indexOf("{</ENC>}") + 8);
            String encryptedValue = originalValue.substring(originalValue.indexOf("{<ENC>}") + 7, originalValue.indexOf("{</ENC>}"));

            try {
                String decryptedValue = EncrypDecriptUtil.decrypt(encryptedValue);//EncrypDecriptUtil is my class for encription and decryption
                originalValue = originalValue.replace(encryptedTaggedValue, decryptedValue);
            } catch (GeneralSecurityException e) {
                logger.error("failed to decrypt property returning original value as in properties file.", e);
            }
        }
        return originalValue;
    }
}
公共类EncryptionAwaRepropertyPlaceHolderConfigure扩展了PropertyPlaceHolderConfigure{
私有静态最终记录器记录器=LoggerFactory.getLogger(EncryptionAwarepropertyPlaceHolderConfigure.class);
@凌驾
受保护的字符串convertPropertyValue(字符串原始值){
if(originalValue.contains(“{}”)&&originalValue.contains(“{}”)){
字符串encryptedTaggedValue=originalValue.substring(originalValue.indexOf(“{}”)、originalValue.indexOf(“{}”)+8);
字符串encryptedValue=originalValue.substring(originalValue.indexOf(“{}”)+7,originalValue.indexOf(“{}”);
试一试{
String decryptedValue=EncrypDecriptUtil.decrypt(encryptedValue);//EncrypDecriptUtil是我的加密和解密类
originalValue=originalValue.replace(encryptedTaggedValue,decryptedValue);
}捕获(一般安全性例外e){
logger.error(“未能解密返回属性文件中原始值的属性。”,e);
}
}
返回原始值;
}
}
并将属性文件更改为在custuom ENC标记中包含加密值 作为

mail.imap.task.url=imap://username:{}encryptedPassword{}@imap.googlemail.com:993/inbox

上述解决方案不起作用。如果我做错了什么,请告诉我。谢谢你的回复,在实施你的解决方案后编辑了问题,你能建议我哪里做错了吗。你的配置看起来不错;您需要从XML中删除适配器。我刚刚编写了一个快速启动应用程序,在spring集成测试中对测试电子邮件服务器进行测试,它对我来说运行良好-请参阅对我答案的编辑,我刚刚对GMail帐户进行了测试,空闲功能运行良好。谢谢Garry,但它对我来说不起作用,可能是因为一些xml配置相互冲突
public class EncryptationAwarePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    private static final Logger logger  = LoggerFactory.getLogger(EncryptationAwarePropertyPlaceholderConfigurer.class);

    @Override
    protected String convertPropertyValue(String originalValue) {
        if (originalValue.contains("{<ENC>}") && originalValue.contains("{</ENC>}")) {
            String encryptedTaggedValue = originalValue.substring(originalValue.indexOf("{<ENC>}"), originalValue.indexOf("{</ENC>}") + 8);
            String encryptedValue = originalValue.substring(originalValue.indexOf("{<ENC>}") + 7, originalValue.indexOf("{</ENC>}"));

            try {
                String decryptedValue = EncrypDecriptUtil.decrypt(encryptedValue);//EncrypDecriptUtil is my class for encription and decryption
                originalValue = originalValue.replace(encryptedTaggedValue, decryptedValue);
            } catch (GeneralSecurityException e) {
                logger.error("failed to decrypt property returning original value as in properties file.", e);
            }
        }
        return originalValue;
    }
}
    mail.imap.task.url=imap://username:{<ENC>}encryptedPassword{</ENC>}@imap.googlemail.com:993/inbox