2017-02-17 7 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> 

私はパスワードフィールドを暗号化してプロパティファイルに保存し、コード内でそれを復号化したいと考えています。 ImapMailReceiverのカスタムバージョンにImapIdleChannelAdapterのmailReceiverプロパティを設定する方法がわかりません。 これを行う方法がある場合は教えてください。

すべての私の設定は、上記のように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など私は春がJava @ConfigurationとXML設定および他の2つのインスタンスを作成しますことを観察しました。 Javaの設定だけを使用する予定だった場所。私がxml設定タグ を削除した場合、それは私のmailReceiverでsigle imapインスタンスを提供しますが、一度だけ実行すると定期的に実行されません。 IMAPSログも表示されません。

私はパスワードを暗号化するために多くのことをする必要があるかどうかは疑問です。私のアプローチでは何かが間違っていますか?

答えて

0

代わりにXMLを使用するJava設定...

@Configuration 
public class MyConfigClass { 

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

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

} 

は、あなたが他のすべてのためにXMLを使用している場合は、単にあなたのXMLへ<bean/>として、このクラスを追加します。

EDIT

は、ここで私の意図は、プロパティファイルに暗号化されたパスワードを使用していた

@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

としてcustuomのENCタグ で暗号化された値を囲むために提出します。私が何か悪いことをしているかどうか教えてください。 – Chandra

+0

あなたの解決策を実装した後で質問を編集しましたが、私はどこが間違っているのかをお答えください。 – Chandra

+0

設定はOKです。アダプターを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; 
    } 
} 

そして、変更されたプロパティが動作しませんでした溶液上の

mail.imap.task.url=imap://username:{<ENC>}encryptedPassword{</ENC>}@imap.googlemail.com:993/inbox 
関連する問題