简体   繁体   English

如何在Java中使用LDAP身份验证进行Exchange Web服务连接?

[英]How to use LDAP authentication for the Exchange Web Services connection in Java?

I try to write a Java application that access an Exchange Web Services in order to read emails. 我尝试编写一个访问Exchange Web服务的Java应用程序,以便阅读电子邮件。 Thus, I use the Exchange Web Services ( EWS ) Java API provided by Microsoft. 因此,我使用Microsoft提供的Exchange Web服务( EWS )Java API。

I already had several issues with it , and I finally found that the authentication should be done using LDAP. 我已经遇到了几个问题 ,我终于发现应该使用LDAP完成身份验证。 Unfortunately, I'm not sure how to do such a thing. 不幸的是,我不知道怎么做这样的事情。 Does the EWS API allows to configure the authentication scheme to be used when connecting to the Exchange server ? EWS API是否允许配置连接到Exchange服务器时要使用的身份验证方案? If yes, how to configure that? 如果是,如何配置?

This is the code I use for connection, but it uses the default authentication scheme, ie NTLM : 这是我用于连接的代码,但它使用默认的身份验证方案,即NTLM

String url = "https//my-server/EWS/exchange.asmx";
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.setTraceEnabled(true);
service.setCredentials(new WebCredentials("user", "password"));
service.setUrl(url.toURI());

Mailbox mailbox = new Mailbox("foo@bar.com");
FolderId folder = new FolderId(WellKnownFolderName.Inbox, mailbox);
ItemView view = new ItemView(10);
view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Descending);
FindItemsResults<Item> items = service.findItems(folder, view);

We resolved this issue. 我们解决了这个问题。 In fact, we had 2 solutions for that: 事实上,我们有两个解决方案:

In the Microsft EWS API, the class NTLM was wrong. 在Microsft EWS API中,类NTLM是错误的。 So we re-built the JAR with the following code for the class: 所以我们用类的以下代码重新构建了JAR:

private class NTLM {
    /** Character encoding */
    public static final String DEFAULT_CHARSET = "ASCII";

    /**
    * The character was used by 3.x's NTLM to encode the username and
    * password. Apparently, this is not needed in when passing username,
    * password from NTCredentials to the JCIFS library
    */
    private String credentialCharset = DEFAULT_CHARSET;

    void setCredentialCharset(String credentialCharset) {
           this.credentialCharset = credentialCharset;
    }

    private static final int TYPE_1_FLAGS = NtlmFlags.NTLMSSP_NEGOTIATE_NTLM
                 | NtlmFlags.NTLMSSP_NEGOTIATE_UNICODE
                 | NtlmFlags.NTLMSSP_NEGOTIATE_NTLM2;

    private String generateType1Msg(String host, String domain) {
           jcifs.ntlmssp.Type1Message t1m = new jcifs.ntlmssp.Type1Message(
                        TYPE_1_FLAGS, domain, host);
           return jcifs.util.Base64.encode(t1m.toByteArray());
    }

    private String generateType3Msg(String username, String password,
                 String host, String domain, String challenge) {
           jcifs.ntlmssp.Type2Message t2m;
           try {
                 t2m = new jcifs.ntlmssp.Type2Message(
                               jcifs.util.Base64.decode(challenge));
           } catch (IOException e) {
                 throw new RuntimeException("Invalid Type2 message", e);
           }

           final int type2Flags = t2m.getFlags();
           final int type3Flags = type2Flags
                        & (0xffffffff ^ (NtlmFlags.NTLMSSP_TARGET_TYPE_DOMAIN | NtlmFlags.NTLMSSP_TARGET_TYPE_SERVER));

           jcifs.ntlmssp.Type3Message t3m = new jcifs.ntlmssp.Type3Message(
                        t2m, password, domain, username, host, type3Flags);
           return jcifs.util.Base64.encode(t3m.toByteArray());
    }
}

Another solution is to use the JWebServices library (commercial). 另一种解决方案是使用JWebServices库(商业)。

We had the same issue and although changing the NTLM class (as romaintaz suggested) works, it was breaking at some other point. 我们遇到了同样的问题,虽然改变了NTLM类(如romaintaz建议的那样),但它在其他方面有所突破。

However, there is a newer version of the EWS Java library, hostet at github: https://github.com/OfficeDev/ews-java-api 但是,有一个更新版本的EWS Java库,github上的hostet: https//github.com/OfficeDev/ews-java-api

It now uses apache httpclient 4.4.1, which has a good NTLM-implementation. 它现在使用apache httpclient 4.4.1,它具有良好的NTLM实现。

Using this library, we had no more issues regarding NTLM authentication so far. 使用此库,到目前为止我们没有关于NTLM身份验证的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Exchange Web服务Kerberos身份验证 - Exchange web services Kerberos Authentication 如何通过Java Web Service客户端连接Exchange Web Services? - How to connect Exchange Web Services via java web service client? JAVA Web应用程序中的LDAP身份验证 - LDAP authentication in JAVA web application 如何检索消息和任务主体 - Exchange Web服务 - Java - How to retrieve Message and Task body - Exchange Web Services - Java 如何使用Java在Android上的Exchange上使用NTLM身份验证? - How to use NTLM authentication with Exchange on Android using Java? Java中Web服务的用户身份验证 - User authentication for web services in Java 在 Java Web 应用程序中使用 LDAP 进行 SSO 身份验证 - SSO Authentication Using LDAP in Java Web Application 如何在Web Services Java EE中使用EJB - How to use an EJB in a Web Services Java EE 通过Extranet公开Sql Server Reporting Services 2008 R2,同时与具有LDAP身份验证的Java Web应用程序集成 - Exposing Sql Server Reporting Services 2008 R2 over Extranet while integrating with Java web application with LDAP authentication Exchange Web服务Java APi + RESTful推送通知侦听器 - Exchange Web Services Java APi + RESTful Push Notifications Listener
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM