How do I use LDAP authentication for exchange web service connections in Java?
•
Java
I try to write a Java application that accesses exchange web services to read e - mail Therefore, I use the exchange web services (EWS) Java API provided by Microsoft
I already had severe issues with it Unfortunately, I don't know how to do such a thing Does the EWS API allow you to configure the authentication scheme to use when connecting to the exchange server? If so, how?
This is the code I use to connect, but it uses the default authentication scheme, 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());
Mail@R_528_2419@ mail@R_528_2419@ = new Mail@R_528_2419@("foo@bar.com");
FolderId folder = new FolderId(WellKNownFolderName.In@R_528_2419@,mail@R_528_2419@);
ItemView view = new ItemView(10);
view.getOrderBy().add(ItemSchema.DateTimeReceived,SortDirection.Descending);
FindItemsResults<Item> items = service.findItems(folder,view);
Solution
We solved the problem In fact, we have two solutions:
In the microsft EWS API, the class NTLM is wrong So we rebuilt the jar with the following code of the class:
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,username,host,type3Flags);
return jcifs.util.Base64.encode(t3m.toByteArray());
}
}
Another solution is to use the j Web Services Library (commercial)
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
