Is Spring Integration Mail IMAP support OAuth2?

耗尽温柔 提交于 2021-01-29 16:15:00

问题


I'm using Spring integration mail for imap idle events. I want to implement a IMAP gmail client using the token instead of using username and password.

Is Spring Integration Mail IMAP able to connect to gmail store with access token?

Here's the code I used

IntegrationFlow flow = IntegrationFlows
            .from(Mail.imapIdleAdapter("imaps://[username]:[password]@imap.gmail.com:993/inbox")
                    .javaMailProperties(p -> p
                            .put("mail.debug", "false")
                    .autoStartup(false)
                    .shouldReconnectAutomatically(true)
                    .userFlag("testSIUserFlag")
                    .headerMapper(new DefaultMailHeaderMapper()))
            .handle((Message<?> message) -> {
                logger.info("message: " + message);
            })
            .get();

回答1:


Well, sounds like we just need to perform some REST request to get a token and together with some Java Mail properties set it into the URL instead of that [password] placeholder: https://kgiann78.github.io/java/gmail/2017/03/16/JavaMail-send-mail-at-Google-with-XOAUTH2.html

 Properties props = new Properties();
 props.put("mail.imap.ssl.enable", "true"); // required for Gmail
 props.put("mail.imap.sasl.enable", "true");
 props.put("mail.imap.sasl.mechanisms", "XOAUTH2");
 props.put("mail.imap.auth.login.disable", "true");
 props.put("mail.imap.auth.plain.disable", "true");

For this purpose it sounds like you should use dynamic flow registration, so you can perform token request first and then register that Mail.imapIdleAdapter() based on the result of the first request: https://docs.spring.io/spring-integration/docs/current/reference/html/dsl.html#java-dsl-runtime-flows



来源:https://stackoverflow.com/questions/61508715/is-spring-integration-mail-imap-support-oauth2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!