@tedkong
Please try using following code snippets with NTLM authentication:
NTLM authentication with SAAJ Authenticator for single user (JDK 1.8):
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
static Authenticator getAuthenticator(String user, String pw, String domain) {
final String username = domain + "\\" + user;
final String password = pw;
System.out.println("New Credentials " + username);
return new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
System.out.println("Authenticate " + username);
return new PasswordAuthentication(username, password.toCharArray());
}
};
}
@Test
public void test() throws MalformedURLException {
EWSClient.useSAAJAPI(true);
System.setProperty("http.auth.preference", "NTLM");
Authenticator.setDefault(getAuthenticator("slxntest001@ricoh-gse.com", "6Q=A9c~PYmKPjN*5!\"d)!L:{6e?>", ""));
IEWSClient client = EWSClient.getEWSClient(new URL("https://mail.ricoh-gse.com/EWS/Exchange.asmx"));
}
NTLM authentication with SAAJ Authenticator for multiple users (JDK 1.8):
static Authenticator getAuthenticator() {
// This block is written for suppressing a bug in sun implementation.
// In Sun Impl client doesn't authenticate user for each connection,
// uses cached credentials instead.
sun.net.www.protocol.http.AuthCacheValue.setAuthCache(new sun.net.www.protocol.http.AuthCache() {
public void remove(String pkey, sun.net.www.protocol.http.AuthCacheValue entry) {
}
public void put(String pkey, sun.net.www.protocol.http.AuthCacheValue value) {
}
public sun.net.www.protocol.http.AuthCacheValue get(String pkey, String skey) {
return null;
}
});
return new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return getEmbeddedCredentials(getRequestingURL());
}
PasswordAuthentication getEmbeddedCredentials(URL url) {
if (url == null) {
return null;
}
String userInfo = url.getUserInfo();
int colon = userInfo == null ? -1 : userInfo.indexOf(":");
if (colon == -1) {
return null;
} else {
String userName = rawURLDecode(userInfo.substring(0, colon));
String pass = rawURLDecode(userInfo.substring(colon + 1));
System.out.println("Authenticate " + userInfo);
return new PasswordAuthentication("\\" + userName, pass.toCharArray());
}
}
};
}
static String rawURLEncode(String value) {
try {
return java.net.URLEncoder.encode(java.util.Base64.getEncoder().encodeToString(value.getBytes()), "UTF-8");
} catch (UnsupportedEncodingException e) {
return value;
}
}
static String rawURLDecode(String value) {
try {
return new String(java.util.Base64.getDecoder().decode(java.net.URLDecoder.decode(value, "UTF-8")), "UTF-8");
} catch (UnsupportedEncodingException e) {
return value;
}
}
static URL getURL(String url, String user, String pw, String domain) throws Exception {
String host = new URL(url).getHost();
URL endpoint = new URL(url.replace(host, rawURLEncode(user) + ":" + rawURLEncode(pw) + "@" + host));
return endpoint;
}
@Test
public void test() throws Exception {
Authenticator.setDefault(getAuthenticator());
EWSClient.useSAAJAPI(true);
System.setProperty("http.auth.preference", "NTLM");
IEWSClient client = EWSClient.getEWSClient(getURL("https://mail.ricoh-gse.com/EWS/Exchange.asmx", "slxntest001@ricoh-gse.com", "6Q=A9c~PYmKPjN*5!\"d)!L:{6e?>", ""));
}
Since Java 9, we can set Authenticator for connection:
static Map<String, Authenticator> authInfo = new HashMap<String, Authenticator>();
static URL getURL(String url, final String user, final String pw, final String domain) throws MalformedURLException {
URL endpoint = new URL(new URL(url), "", new URLStreamHandler() {
protected URLConnection openConnection(URL url) throws IOException {
URL target = new URL(url.toString());
HttpURLConnection connection = (HttpURLConnection) target.openConnection();
// Cache for User@Url
Authenticator auth = authInfo.get(user + "@" + url);
if (auth == null) {
auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
System.out.println("Authenticate " + user);
return new PasswordAuthentication(domain + "\\" + user, pw.toCharArray());
}
};
authInfo.put(user + "@" + url, auth);
}
connection.setAuthenticator(auth);
return connection;
}
});
return endpoint;
}
@Test
public void test() throws Exception {
EWSClient.useSAAJAPI(true);
System.setProperty("http.auth.preference", "NTLM");
IEWSClient client = EWSClient.getEWSClient(getURL("https://mail.ricoh-gse.com/EWS/Exchange.asmx", "slxntest001@ricoh-gse.com", "6Q=A9c~PYmKPjN*5!\"d)!L:{6e?>", ""));
}
Note:
The JAXB APIs are considered to be Java EE APIs, and therefore are no longer contained on the default class path in Java SE 9.
Maven JAXB dependencies:
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.messaging.saaj</groupId>
<artifactId>saaj-impl</artifactId>
<version>1.5.0</version>
</dependency>