2017-02-17 39 views
0

彈簧配置示例如下。如何在Spring中使用加密的store-uri ImapIdleChannelAdapter

<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> 

我想保留屬性文件中的加密密碼字段,並在代碼解密。我不確定如何將ImapIdleChannelAdapter的mailReceiver屬性設置爲我的定製版本的ImapMailReceiver。 請讓我知道是否有任何方法可以做到這一點。

我所有的配置都是按照上述的XML格式。 以上解決方案添加defifnation不起作用可能是我做錯了什麼。然後我嘗試使用XML + Java配置,如下所示。

@Configuration 
public class EmailConfiguration { 
    @Bean 
    public ImapIdleChannelAdapter customAdapter() { 
     ImapIdleChannelAdapter adapter = new ImapIdleChannelAdapter(mailReceiver()); 
     adapter.setOutputChannel(outputChannel()); 
     adapter.setErrorChannel(errorChannel()); 
     adapter.setAutoStartup(true); 
     adapter.setShouldReconnectAutomatically(true); 
     adapter.setTaskScheduler(taskScheduler()); 
     return adapter; 
    } 

    @Bean 
    public TaskImapMailReceiver mailReceiver() { 
     TaskImapMailReceiver mailReceiver = new TaskImapMailReceiver("imaps://[username]:[password]@imap.googlemail.com:993/inbox"); 
     mailReceiver.setShouldDeleteMessages(false); 
     mailReceiver.setShouldMarkMessagesAsRead(true); 
     //mailReceiver.setJavaMailProperties(javaMailProperties()); 
     mailReceiver.setMaxFetchSize(Integer.MAX_VALUE); 
     return mailReceiver; 
    } 
} 

也造就空errorChannel,outputChannel等我觀察到春天創建兩個實例有一個XML配置和其它與Java @Configuration。預計只使用java配置的地方。如果我刪除XML配置標籤 然後它提供了與我的mailReceiver sigle imap實例,但只運行一次不會定期。也不顯示IMAPS日誌。

只是想知道如果我需要這麼做來加密密碼。我的方法有問題嗎?

回答

0

使用Java配置,而不是XML ...

@Configuration 
public class MyConfigClass { 

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

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

} 

如果使用XML對於一切,只需添加這個類爲<bean/>你的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 
    } 

} 
+0

上述方案沒有奏效。讓我知道如果我做錯了什麼。 – Chandra

+0

感謝您的回覆,在實施您的解決方案後編輯了該問題,能否請您建議我哪裏出錯。 – Chandra

+0

你的配置看起來不錯;您需要從XML中移除適配器。我剛剛寫了一個快速啓動應用程序來測試它在春季集成測試中的測試電子郵件服務器,並且它對我來說很好 - 請參閱編輯我的回答 –

0

我的目的是使用屬性文件加密的密碼。 所以我改變了進入電子郵件接收班的方法。我添加了繼承的PropertyPlaceholderConfigurer並實現了方法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; 
    } 
} 

而改變屬性文件附上在custuom ENC標籤 加密值

mail.imap.task.url=imap://username:{<ENC>}encryptedPassword{</ENC>}@imap.googlemail.com:993/inbox 
相關問題