gpt4 book ai didi

java - 无法使用 API Java SSL 和证书调用 EWS 服务

转载 作者:行者123 更新时间:2023-11-30 11:02:32 27 4
gpt4 key购买 nike

我实际上正在研究 EWS Java API。目标是访问日历中的数据。因此,我开始学习如何使用 EWS Java API。

从示例开始,我发现了一些问题:

ExchangeService service = new ExchangeService();
ExchangeCredentials credentials = new WebCredentials("me@company.com", "password");
service.setCredentials(credentials);
//WebProxy webProxy = new WebProxy("proxy.domain.company", 8080);
//There's no credentials for the proxy
//service.setWebProxy(webProxy);
try {
service.setUrl(new URI("https://domain.company/ews/exchange.asmx"));
} catch (URISyntaxException e) {
e.printStackTrace();
}
EmailMessage msg;
try {
msg = new EmailMessage(service);
msg.setSubject("hello world");
msg.setBody(MessageBody.getMessageBodyFromText("Sent using the EWS API"));
msg.getToRecipients().add("my.boss@company.com");
msg.send();
} catch (Exception e) {
e.printStackTrace();
}

执行代码首先给了我与依赖项相关的错误,然后是第二次与认证和 SSL 相关的错误,这是控制台的痕迹:

    ------------------------------------------------------------------------
Building ol-v01 1.0-SNAPSHOT
------------------------------------------------------------------------

--- exec-maven-plugin:1.2.1:exec (default-cli) @ ol-v01 ---
microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. The request failed. sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase.int...
Caused by: microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
....
... 38 more
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 7.891s
Finished at: Tue Jun 09 17:49:04 CEST 2015
Final Memory: 5M/119M
------------------------------------------------------------------------

Si 我正在努力解决这个问题:

我发现了一些同样问题的问题,我按照步骤将浏览器下载的证书用keytool安装到JAVA_HOME\lib\security\cacerts

我将它添加到第三方证书颁发机构下的 mmc.exe 工具中。

所以我可以看到安装的证书

有什么办法可以解决吗?有没有我没有进行的步骤?我还想知道 EWS 是否允许我禁用 SSL 检查?

注意:服务器是 Entreprise Exchange 2010 SP2,所以我将使用我的帐户进行测试,我还在 VM 上安装了一个本地服务器以查看差异和问题,因为我的代理存在一些问题。

此外,我想知道服务器的管理员是否需要激活某些东西才能使用 EWS?

感谢您的帮助。

更新2

这是在 MS Server 2012 VM 上安装我自己的 Exchange 2010 服务器后的结果。

我可以通过 https://192.168.1.59/owa/ 访问 OWA

在一些问题和教程中,我将证书从浏览器导出到一个文件中。名称是 verifyfail.WIN-NS09AI4QBB8 CN = verifyfail.WIN-NS09AI4QBB8

我不知道为什么叫这个名字,但没关系。

文件名为verifyfail.WIN-NS09AI4QBB8.cer

然后我使用 key 工具将这个证书添加到 JAVA_HOME\lib\security\cacerts。我用作别名 verifyfail.WIN-NS09AI4QBB8

我还通过第三方证书颁发机构下的 mmc.exe 工具添加了它。

代码变成这样:

    ExchangeService service = new ExchangeService();
ExchangeCredentials credentials = new WebCredentials("ab@domain.com", "P@ssw0rd");
service.setCredentials(credentials);
try {
service.setUrl(new URI("https://192.168.1.59/ews/exchange.asmx"));
} catch (URISyntaxException e) {
e.printStackTrace();
}
EmailMessage msg;
try {
msg = new EmailMessage(service);
msg.setSubject("hello world");
msg.setBody(MessageBody.getMessageBodyFromText("Sent using the EWS API"));
msg.getToRecipients().add("pp@domain.com");
msg.send();
} catch (Exception e) {
e.printStackTrace();
}

我的 POM 文件包含这些依赖项:

    <dependency>
<groupId>com.microsoft.ews-java-api</groupId>
<artifactId>ews-java-api</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4.1</version>
</dependency>

作为输出,我收到此错误:请求失败。请求失败。主机名“192.168.1.59”与对方提供的证书主题不匹配(CN=WIN-NS09AI4QBB8)

执行轨迹:

------------------------------------------------------------------------
Building ol-v01 1.0-SNAPSHOT
------------------------------------------------------------------------

--- exec-maven-plugin:1.2.1:exec (default-cli) @ ol-v01 ---
microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. The request failed. Host name '192.168.1.59' does not match the certificate subject provided by the peer (CN=WIN-NS09AI4QBB8)
at microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:74)
at microsoft.exchange.webservices.data.core.request.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:158)
at microsoft.exchange.webservices.data.core.ExchangeService.internalCreateItems(ExchangeService.java:594)
at microsoft.exchange.webservices.data.core.ExchangeService.createItem(ExchangeService.java:653)
at microsoft.exchange.webservices.data.core.service.item.Item.internalCreate(Item.java:245)
at microsoft.exchange.webservices.data.core.service.item.EmailMessage.internalSend(EmailMessage.java:147)
at microsoft.exchange.webservices.data.core.service.item.EmailMessage.send(EmailMessage.java:258)
at com.soprahr.ol.v01.T10.testMethod(T10.java:46)
at com.soprahr.ol.v01.T10.main(T10.java:24)
Caused by: microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. Host name '192.168.1.59' does not match the certificate subject provided by the peer (CN=WIN-NS09AI4QBB8)
at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.getEwsHttpWebResponse(ServiceRequestBase.java:729)
at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.validateAndEmitRequest(ServiceRequestBase.java:639)
at microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:62)
... 8 more
Caused by: javax.net.ssl.SSLPeerUnverifiedException: Host name '192.168.1.59' does not match the certificate subject provided by the peer (CN=WIN-NS09AI4QBB8)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.verifyHostname(SSLConnectionSocketFactory.java:465)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:395)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134)
at org.apache.http.impl.conn.BasicHttpClientConnectionManager.connect(BasicHttpClientConnectionManager.java:338)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at microsoft.exchange.webservices.data.core.request.HttpClientWebRequest.executeRequest(HttpClientWebRequest.java:292)
at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.getEwsHttpWebResponse(ServiceRequestBase.java:720)
... 10 more
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 2.145s
Finished at: Wed Jun 10 04:34:14 CEST 2015
Final Memory: 5M/119M
------------------------------------------------------------------------

更新 3

在证书的详细信息选项卡中,Oject Key 的值 CN = verifyfail.WIN-NS09AI4QBB8 不同于 CN=WIN-NS09AI4QBB8

我使用浏览器下载的证书包含verifyfail.WIN-NS09AI4QBB8。

在这种情况下,我应该怎么做才能避免这个问题?

enter image description here

最佳答案

错误与 DNS 问题或服务器证书中的名称问题有关。

确保证书 (WIN-NS09AI4QBB8) 的 CN 属性 中的名称可解析并返回正确的 IP 地址 (192.168.1.59)。

CN 属性值中的名称必须在 DNS 中解析为服务器的 IP 地址。错误信息

The request failed. The request failed. Host name '192.168.1.59' does not match the certificate subject provided by the peer (CN=WIN-NS09AI4QBB8)

发生是因为DNS名称无法解析,或者解析到错误的IP。

关于java - 无法使用 API Java SSL 和证书调用 EWS 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30737432/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com