我在基于JAVA的Web应用程序中使用了几个RESTful Web服务。我正在使用RESTeasy客户端访问我的Web服务。这里客户端和服务之间的所有通信都是通过XML(JAX-B xml带注释的详细信息类)。以下是以下代码
String serviceURL = "https://service.company.com/Service/getService"
ServiceRequestDetail serviceRequestDetail = getServiceRequestAsDetailClass();
ServiceResponseDetail serviceResponseDetail = new ServiceResponseDetail();
ClientRequest clientRequest = new ClientRequest(serviceURL);
clientRequest.accept(MediaType.APPLICATION_XML);
clientRequest.body(MediaType.APPLICATION_XML, serviceRequestDetail);
ClientResponse<ServiceRequestDetail> response =
clientRequest.post(ServiceRequestDetail.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : " +
response.getStatus());
}
ServiceResponseDetail serviceResponseDetail =
response.getEntity(ServiceResponseDetail.class);
当我尝试访问我的服务时,我收到“Peer not Authenticated”错误
javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
...
有没有办法在RESTeasy客户端中添加SSL配置详细信息?我们也欢迎任何其他解决这个问题的建议
提前致谢
答案 0 :(得分:3)
我找到了答案,但我很抱歉迟到的回复。
要回答我的问题,RESTeasy客户端确实支持TLS / SSL。事实上,问题是我错过了将证书安装到JVM中。
keytool -import -alias <Replace certificate Alias name> -keystore $JAVA_HOME\jre\lib\security\cacerts -file <Replace your Certificate file location>
这解决了“Peer Not Authenticated”的问题。希望能帮助到你。奖励
答案 1 :(得分:0)
如果您不想将证书添加到JVM并将此证书分开。您可以将证书作为代码的一部分加载,如下所示。
` //加载证书 InputStream fis = this.getClass()。getResourceAsStream(“ file / path / to / your / certificate.crt”); CertificateFactory cf = CertificateFactory.getInstance(“ X.509”); 证书cert = cf.generateCertificate(fis);
// load the keystore that includes self-signed cert as a "trusted" entry
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
keyStore.setCertificateEntry("cert-alias", cert);
tmf.init(keyStore);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, tmf.getTrustManagers(), null);`
然后附加到像
这样的简单构建器上 resteasyClientBuilder.sslContext(sslContext)