jax-ws,jax-rs规范的webservice客户端调用方式及soap安全验证.doc_第1页
jax-ws,jax-rs规范的webservice客户端调用方式及soap安全验证.doc_第2页
jax-ws,jax-rs规范的webservice客户端调用方式及soap安全验证.doc_第3页
jax-ws,jax-rs规范的webservice客户端调用方式及soap安全验证.doc_第4页
jax-ws,jax-rs规范的webservice客户端调用方式及soap安全验证.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Java调用webservice方式的总结柿子当然要拿软的捏,笔者先讲基于http协议的jax-rs规范的webservice的调用方式。客户端调用WebService的方式: 1.通过wximport生成代码 2.通过客户端编程方式(同第一种是一样都是本地调用) 3.通过ajax调用方式(可能存在跨域 jax-rs)4. 通过 URL Connection 方式调用5. 通过HttpClient方式调用6. xfire框架下生成的客户端(不用)1. wximport根据wsdl文档生成客户端代码,再调用在eclipse中,根据操作生成客户端代码,Eg:调用helloWS方法即可2. 客户单编程方式(和第一种方式一样)先生成客户端代码后,调用以下是经测试后的实例:URL url = new URL(http:/localhost:88/webServiceWS/wsWSPort?wsdl); QName sname = new QName(http:/ws.webservice.suan/, wsWSService); Service service = Service.create(url,sname); WsWSDelegate ms = service.getPort(WsWSDelegate.class); System.out.println(ms.helloWS(suansuan); catch (MalformedURLException e) e.printStackTrace(); 第二种方式中,还可以直接创建了SOAP消息后使用dispatch便可以进行传递,通过extractConentAsDocument方法得到Document类型的返回值参考网页:/wanghuan203/article/details/92195653. 使用ajax+xml+js的方式调用具体使用方法,参考整理的ajax跨域文档4. URL Connection方式/服务的地址 /服务的地址 URL wsUrl = new URL(http:/localhost:88/webServiceWS/wsWSPort); HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestMethod(POST); conn.setRequestProperty(Content-Type, text/xml;charset=UTF-8); OutputStream os = conn.getOutputStream(); /创建SOAPMessage SOAPMessage msg=MessageFactory.newInstance().createMessage(); SOAPEnvelope envelope =msg.getSOAPPart().getEnvelope(); SOAPBody body=envelope.getBody(); /创建QName来指定消息中传递数据 QName ename=new QName(http:/ws.webservice.suan/,HelloWS,wsWSService); / SOAPBodyElement ele=body.addBodyElement(ename); ele.addChildElement(arg0).setValue(suansuan); String soap1=soap.toSoapString(msg); os.write(soap1.getBytes(); InputStream is = conn.getInputStream(); byte b = new byte1024; int len = 0; String s = ; while(len = is.read(b) != -1) String ss = new String(b,0,len,UTF-8); s += ss; System.out.println(s); is.close(); os.close(); conn.disconnect();5. httpclient方式需要commons-codec-1.3.jar,commons-logging-1.0.4.jar,commons-httpclient-3.1.jar/定义一个PostMethod,这时需要指定web服务的Url/soapRequestData是传递的soap协议的信息,可以通过soap建立,也可以直接StringPostMethod postMethod =new PostMethod(http:/localhost:88/webServiceWS/wsWSPort);byte b = soapRequestData.getBytes(utf-8);InputStream is = new ByteArrayInputStream(b,0,b.length);/RequestEntity re = new InputStreamRequestEntity(is,b.length,application/soap+xml; charset=utf-8);/不能设置后面的内容,设置了报415错误RequestEntity re = new InputStreamRequestEntity(is,b.length);postMethod.setRequestEntity(re);HttpClient httpClient = new HttpClient();int statusCode = httpClient.executeMethod(postMethod);/请求状态 200 okString result = postMethod.getResponseBodyAsString();/返回的字符串形式的soap,进一步解析System.out.println(statusCode);System.out.println(result.toString();6. xfire框架下生成的客户端也是通过wsdl生成客户端程序后调用,至于soap header的验证,使用map加入验证信息后验证具体的操作,在我工作文档中,有一个短信平台接口文档有详细的xfire的使用过程,通用性不错注意,以上关于soap信息,我是根据生成的本地调用类的注释,编写soap信息,此外可以直接使用String类型的字符串,只要你熟悉soap的格式,就可以手动编写传递的soap消息。Soap协议的消息 JAX-WS规范是一组XML web services的JAVA API,在 JAX-WS中,一个远程调用可以转换为一个基于XML的协议例如SOAP,在使用JAX-WS过程中,开发者不需要编写任何生成和处理SOAP消息的代码。JAX-WS的运行时实现会将这些API的调用转换成为对应的SOAP消息。 JAX-WS 也提供了一组针对底层消息进行操作的API调用,你可以通过Dispatch 直接使用SOAP消息或XML消息发送请求或者使用Provider处理SOAP或XML消息。SOAP Header 元素可包含有关 SOAP 消息的应用程序专用信息(比如认证、支付等)SAAJ构建SOAP消息范例:/创建SOAPMessage SOAPMessage msg=MessageFactory.newInstance().createMessage(); SOAPEnvelope envelope =msg.getSOAPPart().getEnvelope(); QName ename1=new QName(http:/ws.webservice.suan/,HelloWS1,wsWSService1); /header的targetspace和方法,入口 SOAPHeader header=msg.getSOAPHeader(); SOAPHeaderElement hele=header.addHeaderElement(ename1); hele.addChildElement(name).setValue(suan); hele.addChildElement(password).setValue(njau1918121); /配置的参数 QName ename=new QName(http:/ws.webservice.suan/,HelloWS,wsWSService); SOAPBody body=envelope.getBody(); /创建QName来指定消息中传递数据 / SOAPBodyElement ele=body.addBodyElement(ename); ele.addChildElement(arg0).setValue(suansuan); /ele.addChildElement(SecondB).setValue(33); /msg.writeTo(System.out); System.out.println(soap.toSoapString(msg);Soap信息里面的参数配置:这是本地生成的客户端信息,targetNamespace,webParam arg0 QName ename=new QName(http:/ws.webservice.suan/,HelloWS,wsWSService);这里面的三个数据,空间,方法名,服务名 参数名arg0认识发布的webserviceCxf框架:其中wsWs是服务名,Hello是方法名,http:/ws.webservice.suan/是定义的目标空间单击,进入wsdl文档,其中wsWSImplPort是进入的端口名Jdk自带框架:目标空间,方法名,端口名,服务名都在了注,了解发布的webservice,有助于创建soap信息Webservice的加密的方案举例(1)对webservice发布的方法,在入参中增加一个或多个字符串序列; 这里的字符串可以要求必须满足指定的格式,字符串可以再通过客户端传参数的时候加密,服务端解密;扯点题外话,说起加密解密,我想起了页面级的自动登录功能,使用cookie保存密码,所谓的加密,是为了不让其他用户直接看到加密前的密码,而被直接通过登录功能登录。那这一块接口的字符串加密的意义何在,我本来就是程序调用接口,不管加密不加密,还是一样的调用,又没有额外的客户端直接让你输未加密密码验证的(所以,以我现在的理解能力,觉得这个所谓的加密,除了能分辨使用对象之外,毫无安全性可言)(2)对webservice发布的方法,入参中加上用户名和密码,然后服务端通过数据库校验;参数中加入用户名和密码,验证不通过提示非法请求之类的,是一种方法参考网页:/luking/archive/2011/03/04/1970592.html (3) 对webservice发布的方法,通过handler/chain方式来实现验证(用户名&密码校验/IP地址校验等);主要讲这种常用的验证方式参考网页cxf验证:/blog/792236#(4) 对webservice发布的方法,采用webservice的users.lst来进行验证;(5) 对webservice发布的服务,通过servlet的Filter来实现验证;(6) 对webservice传输过程中的数据进行加密;数据加密后,在服务器端解密注:关于axis框架下的两种验证方式参考网页Axis框架下的handler验证:/java-pan/archive/2012/09/11/webservice_security2.htmlAxis框架下的users.lst验证:/java-pan/archive/2012/09/10/2478263.htmlWebservice的安全机制 Handler验证机制 WebService有两种安全机制,一是利用WS-Security将签名和加密头加入SOAP消息,另一个是利用数字证书和数字签名认证。此篇文章介绍利用cxf实现WS-Security验证。 参考网页 Cxf:/blog/792236# 1. 服务器端创建handler步骤,结合软件操作,免去注释的学习本人通过myeclipse操作,只适用cxf框架开发的jax-ws规范的webservice,在*Impl.java右键jax-ws tools-create jax-ws handler根据要求填写相关类,把验证类所需的handler chain文件,建在webservice包下新建完毕,一个是修改*Impl.java原始的注入路径有问题Handlers.xml文件内容如下: authHandler suan.webservice.ws.AuthValidationHandler 2. 书写服务器端验证类:根据soap header里面的信息结构分两种处理方式1.name&password /随意,可使用name|password,name-password /auth 节点名称,可自定义 suan,user.2. 跟body中的结构一致name /arg0 可以自定义password 和 UserOrgID Hubs1 password 先讲创建的区别:QName qname_user=new QName(http:/ws.webservice.suan/,auth);/authSOAPHeaderElement helem_user=hdr.addHeaderElement(qname_user);helem_user.addTextNode(admin&admin);QName qname_user=new QName(http:/ws.webservice.suan/,authentication);/authSOAPHeaderElement helem_user=hdr.addHeaderElement(qname_user);helem_user.addChildElement(userorgid).setValue(suansuan); helem_user.addChildElement(userid).setValue(*); helem_user.addChildElement(userpsw).setValue(*); 现在讲服务器端处理方式针对第一种:Boolean outbound = (Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); if (!outbound.booleanValue() SOAPMessage soapMessage = context.getMessage(); try SOAPEnvelope soapEnvelope = soapMessage.getSOAPPart().getEnvelope();SOAPHeader soapHeader = soapEnvelope.getHeader();if(soapHeader = null)generateSoapFault(soapMessage, No Message Header.);/报错Iterator it = soapHeader.extractHeaderElements(SOAPConstants.URI_SOAP_1_2_ROLE_NEXT);if(it = null | !it.hasNext()generateSoapFault(soapMessage, No Header block for role next);/报错Node node = (Node)it.next();String value = node = null ? null : node.getValue();if(value = null)generateSoapFault(soapMessage, No authation info in header blocks);/报错/value就是获取到的header中的数据 catch (SOAPException e) e.printStackTrace(); 第二种:Boolean outboundProperty = (Boolean) messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); if (!outboundProperty) / InBound Message String userOrgID = ; String userID = ; String userPSW = ; SOAPMessage message = messageContext.getMessage(); try SOAPHeader soapHeader = message.getSOAPHeader(); NodeList nodeList = soapHeader.getChildNodes(); /获取节点 for (int i = 0; i nodeList.getLength(); i+) Node nodeAuth = nodeList.item(i); if (nodeAuth.getNodeType() = Node.ELEMENT_NODE & Authentication.equals(nodeAuth.getNodeName() /判断节点名称,可以不判断,跟第一种一样 for (Node node = nodeAuth.getFirstChild(); node != null; node = node.getNextSibling() if (node.getNodeType() = Node.ELEMENT_NODE) if (UserOrgID.equals(node.getNodeName() & node.getFirstChild() != null) userOrgID = node.getFirstChild().getTextContent(); else if (UserID.equals(node.getNodeName() & node.getFirstChild() != null) userID = node.getFirstChild().getTextContent(); else if (UserPSW.equals(node.getNodeName() & node.getFirstChild() != null) userPSW = node.getFirstChild().getTextContent(); catch (SOAPException e) log.warn(e); throw new RuntimeException(e); 以下是服务器端的具体实例,是第一种结构public class AuthValidationHandler implements SOAPHandler public Set getHeaders() / TODO Auto-generated method stubreturn null;public void close(MessageContext context) public boolean handleFault(SOAPMessageContext context) return false;public boolean handleMessage(SOAPMessageContext context) HttpServletRequest request = (HttpServletRequest)context.get(AbstractHTTPDestination.HTTP_REQUEST);System.out.println(客户端IP:+request.getRemoteAddr();Boolean outbound = (Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); if (!outbound.booleanValue() SOAPMessage soapMessage = context.getMessage(); try SOAPEnvelope soapEnvelope = soapMessage.getSOAPPart().getEnvelope();SOAPHeader soapHeader = soapEnvelope.getHeader();if(soapHeader = null)generateSoapFault(soapMessage, No Message Header.);Iterator it = soapHeader.extractHeaderElements(SOAPConstants.URI_SOAP_1_2_ROLE_NEXT);if(it = null | !it.hasNext()generateSoapFault(soapMessage, No Header block for role next);Node node = (Node)it.next();String value = node = null ? null : node.getValue();if(value = null)generateSoapFault(soapMessage, No authation info in header blocks);String infos = value.split(&);return authValidate(infos0, infos1); catch (SOAPException e) e.printStackTrace(); return false;private boolean authValidate(String userName,String password)if(userName = null | password = null)return false;if(admin.equals(userName) & admin.equals(password)return true;return false;private void generateSoapFault(SOAPMessage soapMessage,String reasion)try SOAPBody soapBody = soapMessage.getSOAPBody();SOAPFault soapFault = soapBody.getFault();if(soapFault = null)soapFault = soapBody.addFault();soapFault.setFaultString(reasion);throw new SOAPFaultException(soapFault); catch (SOAPException e) / TODO Auto-generated catch blocke.printStackTrace();以下是未通过验证的报错:3客户端soap信息及验证类的书写 不管怎样,还是要回归客户端调用这一块的。 主要两个方向,一个是直接客户端生成本地调用,问题是怎么添加header信息头;另一个就是生成soap信息,通过httpclient,url connection以及soap dispatch的方式传递soap信息。1. 调用本地客户端的形式这个需要AuthenticationHandler()类的支持,里面是编写的soapheader信息(具体事例至如

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论