Tomcat + 数字证书 部署webservice (客户端调用https webService)

关于tomcat +数字证书类例子网络上很多,使用keytool工具即可,配置可见:

http://blog.csdn.net/huzheaccp/article/details/8812826

 最后访问:https://localhost:8443   出现tomcat主页面 并且IE浏览器加锁图标出现

webService打包部署tomcat:

需要jar包:jaxws-2_0.jar  可到官网下载

WebService目录结构:


[java] view plain  copy
 print ?
  1. package com.huzhe.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.jws.WebMethod;  
  6. import javax.jws.WebParam;  
  7. import javax.jws.WebService;  
  8.   
  9. import com.huzhe.po.Student;  
  10.   
  11. @WebService  
  12. public interface IStudentService {  
  13.       
  14.      @WebMethod  
  15.      Student getStudentById(@WebParam(name="id")String id);   
  16. }  

[java] view plain  copy
 print ?
  1. package com.huzhe.service;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import javax.jws.WebService;  
  7.   
  8. import com.huzhe.po.Student;  
  9.   
  10. @WebService(endpointInterface="com.huzhe.service.IStudentService")  
  11. public class StudentImpl implements IStudentService {  
  12.   
  13.     @Override  
  14.     public Student getStudentById(String id) {  
  15.         return  new Student(id, "张三");  
  16.     }  
  17. }  

--------------------------------------------------------------------------------------------------------------------------------------

在执行下面之前,请确保webService可以正常访问,下面的内容主要涉及到tomcat部署以及https调用

--------------------------------------------------------------------------------------------------------------------------------------

配置:cmd   进入 该项目  clesses 目录下:执行:

[java] view plain  copy
 print ?
  1. wsgen -cp . com.xx.service.StudentImpl  

在web.xml中加入:

[html] view plain  copy
 print ?
  1. <login-config>  
  2.         <auth-method>CLIENT-CERT</auth-method>  
  3.         <realm-name>Client Cert Users-only Area</realm-name>  
  4.     </login-config>  
  5.     <security-constraint>  
  6.             
  7.         <web-resource-collection >  
  8.             <web-resource-name >SSL</web-resource-name>  
  9.             <url-pattern>/*</url-pattern>  
  10.         </web-resource-collection>  
  11.         <user-data-constraint>  
  12.             <transport-guarantee>CONFIDENTIAL</transport-guarantee>  
  13.         </user-data-constraint>  
  14. </security-constraint>  
  15.   
  16.   
  17. <listener>    
  18.         <listener-class>    
  19.             com.sun.xml.ws.transport.http.servlet.WSServletContextListener    
  20.         </listener-class>    
  21.     </listener>    
  22.     <servlet>    
  23.         <servlet-name>studentImpl</servlet-name>    
  24.         <servlet-class>    
  25.             com.sun.xml.ws.transport.http.servlet.WSServlet    
  26.         </servlet-class>    
  27.     </servlet>    
  28.     <servlet-mapping>    
  29.         <servlet-name>studentImpl</servlet-name>    
  30.         <url-pattern>/studentImpl</url-pattern>    
  31. </servlet-mapping>  
在web-inf下建立:sun-jaxws.xml
[html] view plain  copy
 print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"  
  3.     version="2.0">  
  4.     <endpoint name="StudentImplWS" implementation="com.huzhe.service.StudentImpl"  
  5.         url-pattern="/studentImpl" />  
  6. </endpoints>  

然后打包:war 使用maven或者直接Eclipse导出  war

得到:ws.war

 

放到:tomcat webapp下面启动服务器;

 

访问:http://localhost:8080/ws/studentImpl?wsdl

浏览器地址变为:https://localhost:8443/ws/studentImpl?wsdl

说明已经加密了

根据wsdl文件使用eclipse生成webService客户端

(wsdl文件可以使用命令生成,最简单的方法:访问上边的地址得到xml信息  直接复制  保存为 ws.wsdl文件即可)

 

[java] view plain  copy
 print ?
  1. IStudentServiceProxy p = newIStudentServiceProxy();  
  2. p.getIStudentService().getStudentById("001")  
  3.                   .getName()  


 

 

直接访问:出错   如下:

[html] view plain  copy
 print ?
  1. AxisFault  
  2.  faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException  
  3.  faultSubcode:   
  4.  faultString: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target  
  5.  faultActor:   
  6.  faultNode:   
  7.  faultDetail:   
  8.     {http://xml.apache.org/axis/}stackTrace:javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target  
  9.     at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)  
  10.     at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1649)  
  11.     at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:241)  
  12.     at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:235)  
  13.     at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1206)  
  14.     at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:136)  
  15.     at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:593)  
  16.     at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:529)  
  17.     at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:893)  
  18.     at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1138)  
  19.     at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1165)  
  20.     at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1149)  
  21.     at org.apache.axis.components.net.JSSESocketFactory.create(JSSESocketFactory.java:186)  
  22.     at org.apache.axis.transport.http.HTTPSender.getSocket(HTTPSender.java:191)  
  23.     at org.apache.axis.transport.http.HTTPSender.writeToSocket(HTTPSender.java:404)  
  24.     at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:138)  
  25.     at org.apache.axis.transport.http.HTTPSender.readFromSocket(HTTPSender.java:727)  
  26.     at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:144)  
  27.     at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)  
  28.     at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)  
  29.     at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)  
  30.     at org.apache.axis.client.AxisClient.invoke(AxisClient.java:165)  
  31.     at org.apache.axis.client.Call.invokeEngine(Call.java:2784)  
  32.     at org.apache.axis.client.Call.invoke(Call.java:2767)  
  33.     at org.apache.axis.client.Call.invoke(Call.java:2443)  
  34.     at org.apache.axis.client.Call.invoke(Call.java:2366)  
  35.     at org.apache.axis.client.Call.invoke(Call.java:1812)  
  36.     at com.huzhe.client.StudentImplServiceSoapBindingStub.getStudentById(StudentImplServiceSoapBindingStub.java:228)  
  37.     at com.huzhe.client.ClientTest.main(ClientTest.java:18)  
  38. Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target  
  39.     at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:323)  
  40.     at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:217)  
  41.     at sun.security.validator.Validator.validate(Validator.java:218)  
  42.     at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:126)  
  43.     at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:209)  
  44.     at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:249)  
  45.     at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1185)  
  46.     ... 24 more  
  47. Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target  
  48.     at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:174)  
  49.     at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:238)  
  50.     at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:318)  
  51.     ... 30 more  
  52.   
  53.     {http://xml.apache.org/axis/}hostname:ISS-03261128  
  54.   
  55. javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target  
  56.     at org.apache.axis.AxisFault.makeFault(AxisFault.java:101)  
  57.     at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:154)  
  58.     at org.apache.axis.transport.http.HTTPSender.readFromSocket(HTTPSender.java:727)  
  59.     at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:144)  
  60.     at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)  
  61.     at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)  
  62.     at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)  
  63.     at org.apache.axis.client.AxisClient.invoke(AxisClient.java:165)  
  64.     at org.apache.axis.client.Call.invokeEngine(Call.java:2784)  
  65.     at org.apache.axis.client.Call.invoke(Call.java:2767)  
  66.     at org.apache.axis.client.Call.invoke(Call.java:2443)  
  67.     at org.apache.axis.client.Call.invoke(Call.java:2366)  
  68.     at org.apache.axis.client.Call.invoke(Call.java:1812)  
  69.     at com.huzhe.client.StudentImplServiceSoapBindingStub.getStudentById(StudentImplServiceSoapBindingStub.java:228)  
  70.     at com.huzhe.client.ClientTest.main(ClientTest.java:18)  

原因是,客户端没有加入数字证书

在调用方法前加入代码:


[java] view plain  copy
 print ?
  1. public static void main(String[] args) {  
  2.           
  3.         try {  
  4.             IStudentServiceProxy p = new IStudentServiceProxy();  
  5.             System.setProperty("javax.net.ssl.keyStore",  
  6.                     "D:\\mykeystore\\test.keystore");  
  7.             System.setProperty("javax.net.ssl.keyStorePassword""mulepassword");  
  8.             System.setProperty("javax.net.ssl.trustStore",  
  9.                     "D:\\mykeystore\\test.keystore");  
  10.             System.setProperty("javax.net.ssl.trustStorePassword",  
  11.                     "mulepassword");  
  12.   
  13.             System.out.println(p.getIStudentService().getStudentById("001")  
  14.                     .getName());  
  15.   
  16.         } catch (RemoteException e) {  
  17.             // TODO Auto-generated catch block  
  18.             e.printStackTrace();  
  19.         }  
  20.     }  

成功!

[java] view plain  copy
 print ?
  1. D:\\mykeystore\\test.keystore  
和tomcat里配置的8443端口的keystore文件一样
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值