Web service client with Java application and SSL(具有 Java 应用程序和 SSL 的 Web 服务客户端)
问题描述
我正在开发一个 Java 桌面应用程序,我想在其中使用一个 Web 服务.Web 服务需要使用 binarysecuritytoken
的消息级安全性双向 SSL 连接.我将 NetBeans IDE 6.9.1 与 JDK 1.6.0.23 和 JAX-WS 用作 ws 包装器.如何在不使用客户端计算机上的任何 Web 服务器的情况下与 ws 进行通信.我阅读的大部分内容都需要在客户端机器上安装 tomcat 或其他一些 Web 服务器(在 tomcat 中配置密钥库……).有可能吗?请为 Java 桌面应用程序推荐一些基于 SSL 的 ws 客户端的文章.
I am developing a Java desktop application and I want to consume a web service in it. The web service requires two-way SSL connection with message level security using binarysecuritytoken
. I am using NetBeans IDE 6.9.1 with JDK 1.6.0.23 and JAX-WS as ws wrapper. How can I communicate with the ws without using any web server on client machine. Most of the stuff I read needs to have tomcat or some other web server on client machine (configuring the keystore in tomcat or so...). Is it possible to do? Please suggest some article for SSL based ws client for Java desktop application.
推荐答案
这里有两种处理 WS over SSL http://ws.apache.org/xmlrpc/ssl.html.
正确的方法是为 SE 和 EE 解决方案配置和使用您的密钥库.
下一个快速解决方案也适用于我:
Here are two ways to deal with WS over SSL http://ws.apache.org/xmlrpc/ssl.html.
Correct way is to configure and use your keystore for both SE and EE solutions.
Next quick solution also works for me:
package client;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.xml.namespace.QName;
import ws.MyService1;
import ws.MyService1ServiceLocator;
public class Client {
public static void main(String[] args) throws Exception {
test();
}
public static void test() throws Exception {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
// Trust always
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
// Trust always
}
} };
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
// Create empty HostnameVerifier
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
};
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(hv);
// use secured service
QName qname = new QName("http://ws", "MyService1Service");
String url = "https://127.0.0.1:7002/MyService/wsdl/MyService1.wsdl";
MyService1 service = new MyService1ServiceLocator(url, qname).getMyService1();
System.out.println(service.getMessage());
}
}
这篇关于具有 Java 应用程序和 SSL 的 Web 服务客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:具有 Java 应用程序和 SSL 的 Web 服务客户端


- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 获取数字的最后一位 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 转换 ldap 日期 2022-01-01