TLS 1.2 と AutodiscoverService のサポート
Contents
[
Hide
]
Aspose.Email for Java は SAAJ API を使用して TLS 1.2 をサポートするようになりました。 EWSClient 静的プロパティを提供します useSAAJAPI TLS 1.2 を使用するために true に設定できます。Aspose.Email for Java もサポートしています AutodiscoverService TLS 1.2 用。以下のコードサンプルは SAAJ API の使用例を示します。 AutodiscoverService TLS 1.2 用。
SAAJ API を使用
SAAJAPI モードで使用される Java EE SOAP クライアント - https://docs.oracle.com/cd/E19651-01/817-2151-10/wsgjaxm.html。
以下のコードサンプルは、SAAJ API を設定して使用する方法を示しています。 EWSClient.useSAAJAPI プロパティを true に設定します。
次のコードスニペットでは Basic 認証ヘッダーが設定されます:
EWSClient.useSAAJAPI(true);
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testuser", "pw", "domain");
client.listMessages();
次のコードスニペットでは OAUTH2 認証ヘッダーが設定されます:
EWSClient.useSAAJAPI(true);
// token provider
/*ITokenProvider provider = new ITokenProvider() {
@Override
public void dispose() {
}
@Override
public OAuthToken getAccessToken(boolean ignoreExistingToken) {
return null;
}
@Override
public OAuthToken getAccessToken() {
return null;
}
};
NetworkCredential credentials = new OAuthNetworkCredential(provider);*/
// accessToken
NetworkCredential credentials = new OAuthNetworkCredential("accessToken");
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", credentials);
client.listMessages();
このモードでは、API が NTLM 認証プロセスを制御しないことに注意してください。NTLM 認証には Java Authenticator が必要です。
Java 8 用の NTLM 認証コードスニペット:
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());
}
};
}
EWSClient.useSAAJAPI(true);
System.setProperty("http.auth.preference", "NTLM");
Authenticator.setDefault(getAuthenticator("user", "pw", ""));
IEWSClient client = EWSClient.getEWSClient(new URL("https://domain.com/ews/Exchange.asmx"));
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 = userInfo.substring(0, colon);
String pass;
try {
pass = URLDecoder.decode(userInfo.substring(colon + 1), "UTF-8");
} catch (UnsupportedEncodingException e) {
pass = "";
}
System.out.println("Authenticate " + userInfo);
return new PasswordAuthentication("\\" + userName, pass.toCharArray());
}
}
};
}
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, user + ":" + URLEncoder.encode(pw, "UTF-8") + "@" + host));
return endpoint;
}
EWSClient.useSAAJAPI(true);
Authenticator.setDefault(getAuthenticator());
System.setProperty("http.auth.preference", "NTLM");
IEWSClient client1 = EWSClient.getEWSClient(getURL("https://domain.com/ews/Exchange.asmx", "user1", "pw", "domain"));
IEWSClient client2 = EWSClient.getEWSClient(getURL("https://domain.com/ews/Exchange.asmx", "user2", "pw", "domain"));
Java 9 以降、Authenticator を接続に設定できます:
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;
}
EWSClient.useSAAJAPI(true);
System.setProperty("http.auth.preference", "NTLM");
IEWSClient client1 = EWSClient.getEWSClient(getURL("https://domain.com/ews/Exchange.asmx", "user1", "pw", "domain"));
IEWSClient client2 = EWSClient.getEWSClient(getURL("https://domain.com/ews/Exchange.asmx", "user2", "pw", "domain"));
注: JAXB API は Java EE API とみなされるため、Java SE 9 以降のデフォルトクラスパスには含まれなくなりました。
Maven JAXB 依存関係:
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.messaging.saaj</groupId>
<artifactId>saaj-impl</artifactId>
</dependency>
AutodiscoverService を使用
以下のコードサンプルは、使用方法を示しています。 AutodiscoverService TLS 1.2 用
AutodiscoverService service = new AutodiscoverService();
service.setCredentials(new NetworkCredential("user", "password"));
GetUserSettingsResponse response = service.getUserSettings("userSmtpAddress", UserSettingName.ExternalEwsUrl, UserSettingName.UserDisplayName);