ews-java-api-2.0 访问SSL异常?报错-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

ews-java-api-2.0 访问SSL异常?报错

爱吃鱼的程序员 2020-06-14 15:25:10 90

测试ews-java-api是否能和exchange服务通信,代码如下:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);

ExchangeCredentials credentials = new WebCredentials(username,
password);
 service.setCredentials(credentials);
service.setTraceEnabled(true);
        try {
service.setUrl(new URI("https://192.xxx.xxx.xxx/EWS/Exchange.asmx"));

 Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);//该行抛出异常
       System.out.println(inbox.getDisplayName());

} catch ( Exception e) {
e.printStackTrace();

}

jar包:

运行后 报错:

microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. The request failed. Host name '192.xxx.xxx.xxx' does not match the certificate subject provided by the peer (CN=exchange)

Java 网络安全
分享到
取消 提交回答
全部回答(1)
  • 爱吃鱼的程序员
    2020-06-14 15:25:27

    服务器证书和证书上的CN字段不匹配楼主怎么解决的?

    大神,这个问题解决了吗?


    我是通过修改域名验证器来解决,先新写一个类:

    publicclassExchangeServiceWithHostVerifyextendsExchangeService{  //不对任何域名做验证  privatefinalstaticHostnameVerifierhostnameVerifierWithOutVerfy=newHostnameVerifier(){    <aclass="referer"target="_blank">@Override    publicbooleanverify(Strings,SSLSessionsslSession){      returntrue;    }  };  publicExchangeServiceWithHostVerify(){    super();  }  publicExchangeServiceWithHostVerify(ExchangeVersionrequestedServerVersion){    super(requestedServerVersion);  }  protectedRegistry<ConnectionSocketFactory>createConnectionSocketFactoryRegistry(){    try{      returnRegistryBuilder.<ConnectionSocketFactory>create()          .register(EWSConstants.HTTP_SCHEME,newPlainConnectionSocketFactory())          .register(EWSConstants.HTTPS_SCHEME,EwsSSLProtocolSocketFactory.build(null,hostnameVerifierWithOutVerfy))          .build();    }catch(GeneralSecurityExceptione){      thrownewRuntimeException(          "CouldnotinitializeConnectionSocketFactoryinstancesforHttpClientConnectionManager",e      );    }  }}

    建立客户端的时候:

    ExchangeServiceservice=newExchangeServiceWithHostVerify(ExchangeVersion.Exchange2007_SP1);

     

     

    0 0
+ 订阅

云安全开发者的大本营

推荐文章
相似问题
推荐课程