java使用cxf调用https方式的webservice

第一步:入cxf必须的jar包,当前使用的cxf的版本为3.0.1

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-ws-security</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.wss4j</groupId>
            <artifactId>wss4j-ws-security-dom</artifactId>
            <version>2.0.1</version>
        </dependency>

第二步:导出网站证书

https区别于http即https需要证书,接下来我们导出证书。
以下操作方式为谷歌内核浏览器操作方式,各大浏览器均支持导出证书,具体不了解的自行百度。
浏览器输入接口地址https://xxxxx/abc/ws/soap/sendHis?wsdl
在接口地址上f12进入开发者模式
点击sercurty选择 view certificate

保存证书
选择详细信息。点击保存证书

格式选择二进制
格式选择二进制,到此我们得到一个.cer文件-这个就是客户端的证书

第三步:生成key

确保本地安装了jdk,我们需要使用jdk中的keytool工具。
在cmd命令行,输入

>keytool -import -alias nciic -file d:\zs.cer -keystore d:\nciic.keystore

生成key,生成过程中会需要你输入key的密码,密码记录下来这个到后面会有用。

第四步:调用

        System.setProperty("javax.net.ssl.trustStore","d://nciic.keystore"); //key路径
        System.setProperty("javax.net.ssl.trustStorePassword","your pwd");//密码
        System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol"); 
        java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 

        DynamicClientFactory factory = DynamicClientFactory.newInstance();
        Client client = factory.createClient("https://xxxxx/abc/ws/soap/sendHis?wsdl");

其实很简单,就是在我们实际调用之前,添加了4行代码

Object[] results = client.invoke("login","your json");//调用方法

至此,使用Java调用Https方式的接口调用成功。

发布了15 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Himly_Zhang/article/details/81109080
今日推荐