设为首页 收藏本站
查看: 1085|回复: 0

[经验分享] [webservice] webservice拦截器

[复制链接]

尚未签到

发表于 2017-3-1 09:56:36 | 显示全部楼层 |阅读模式
CXF的拦截器

理解


  • 为什么设计拦截器?


  • 为了在webservice请求过程中,能动态操作请求和响应数据, CXF设计了拦截器.


  • 拦截器分类:


  • 按所处的位置分:服务器端拦截器,客户端拦截器
  • 按消息的方向分:入拦截器,出拦截器
  • 按定义者分:系统拦截器,自定义拦截器
  基于jdk的webservice没有拦截器的功能实现。
  接口方法:



import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface HelloInt {
@WebMethod
public String sayHello(String name );
}
  实现:



import javax.jws.WebService;
@WebService
public class HelloImpl implements HelloInt {
@Override
public String sayHello(String name) {
return "hello,"+name;
}
}
  测试方法:



import javax.xml.ws.Endpoint;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;

public class ServerTest {
public static void main(String[] args) {
String address = "http://192.168.1.64:8989/d01ws/hello";
Endpoint point=Endpoint.publish(address,new HelloImpl());
EndpointImpl impl = (EndpointImpl) point;
//添加系统日志拦截器
impl.getInInterceptors().add(new LoggingInInterceptor());
impl.getInInterceptors().add(new LoggingOutInterceptor());
System.out.println("webservice发布成功");
}
}
  浏览器中输入发布地址:



2014-12-31 22:07:52 org.apache.cxf.services.HelloImplService.HelloImplPort.HelloInt
信息: Inbound Message
----------------------------
ID: 8
Address: http://192.168.1.64:8989/d01ws/hello?wsdl
Http-Method: GET
Content-Type:
Headers: {Accept=[text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2], connection=[keep-alive], Content-Type=[null], Host=[192.168.1.64:8989], User-Agent=[Java/1.6.0_10-rc2]}
--------------------------------------
2014-12-31 22:07:52 org.apache.cxf.services.HelloImplService.HelloImplPort.HelloInt
信息: Inbound Message
----------------------------
ID: 9
Address: http://192.168.1.64:8989/d01ws/hello?wsdl
Http-Method: GET
Content-Type:
Headers: {Accept=[text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2], connection=[keep-alive], Content-Type=[null], Host=[192.168.1.64:8989], User-Agent=[Java/1.6.0_10-rc2]}
--------------------------------------
  
利用eclipse 自带的webservice 浏览器添加请求参数 kity,



2014-12-31 22:09:46 org.apache.cxf.services.HelloImplService.HelloImplPort.HelloInt
信息: Inbound Message
----------------------------
ID: 11
Address: http://192.168.1.64:8989/d01ws/hello
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[application/soap+xml, application/dime, multipart/related, text/*], Cache-Control=[no-cache], connection=[close], Content-Length=[325], content-type=[text/xml; charset=UTF-8], Host=[192.168.1.64:8989], Pragma=[no-cache], SOAPAction=[""], User-Agent=[IBM Web Services Explorer]}
Payload: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://wa.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<q0:sayHello>
<arg0>kity</arg0>
</q0:sayHello>
</soapenv:Body>
</soapenv:Envelope>
--------------------------------------
  
另外附上wsdl的文档结构图:



<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://wa.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloImplService" targetNamespace="http://wa.com/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://wa.com/" elementFormDefault="unqualified" targetNamespace="http://wa.com/" version="1.0">
<xs:element name="sayHello" type="tns:sayHello"/>
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xs:complexType name="sayHello">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayHelloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="HelloInt">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello">
</wsdl:input>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloImplServiceSoapBinding" type="tns:HelloInt">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloImplService">
<wsdl:port binding="tns:HelloImplServiceSoapBinding" name="HelloImplPort">
<soap:address location="http://192.168.1.64:8989/d01ws/hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
  二、客户端调用webservice:
  先用wsdl2java 生成客户端代码:



import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import com.wa.HelloImplService;
import com.wa.HelloInt;
/**
* 客户端进行测试
*
*/
public class ClientTest {
public static void main(String[] args) {
HelloImplService service = new HelloImplService();
HelloInt hello = service.getHelloImplPort();
//客户端拦截器
Client client =ClientProxy.getClient(hello);
client.getInInterceptors().add(new LoggingInInterceptor());
client.getInInterceptors().add(new LoggingOutInterceptor());
System.out.println(hello.sayHello("what do you say?please say agin."));
}
}
  可以看到:
  服务端的日志信息为:



----------------------------
ID: 1
Address: http://192.168.1.64:8989/d01ws/hello?wsdl
Encoding: UTF-8
Http-Method: GET
Content-Type: text/xml
Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], content-type=[text/xml], Host=[192.168.1.64:8989], Pragma=[no-cache], User-Agent=[Apache CXF 2.5.9]}
--------------------------------------
2014-12-31 22:27:16 org.apache.cxf.services.HelloImplService.HelloImplPort.HelloInt
信息: Inbound Message
----------------------------
ID: 2
Address: http://192.168.1.64:8989/d01ws/hello
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[210], content-type=[text/xml; charset=UTF-8], Host=[192.168.1.64:8989], Pragma=[no-cache], SOAPAction=[""], User-Agent=[Apache CXF 2.5.9]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://wa.com/"><arg0>what do you say?please say agin.</arg0></ns2:sayHello></soap:Body></soap:Envelope>
--------------------------------------
  客户端日志信息为:



2014-12-31 22:27:15 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
信息: Creating Service {http://wa.com/}HelloImplService from WSDL: http://192.168.1.64:8989/d01ws/hello?wsdl
2014-12-31 22:27:16 org.apache.cxf.services.HelloImplService.HelloImplPort.HelloInt
信息: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {Content-Length=[236], content-type=[text/xml;charset=UTF-8], Server=[Jetty(7.5.4.v20111024)]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://wa.com/"><return>hello,what do you say?please say agin.</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>
--------------------------------------
hello,what do you say?please say agin.
  webservice调用完成。
  三、自定义拦截器的实现:



import java.util.List;
import javax.xml.namespace.QName;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.xml.utils.DOMHelper;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* 自定义拦截器的实现
*
*/
public class CheckUserInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
private String username ;
private String password;
public CheckUserInterceptor(String username,String password) {
super(Phase.PRE_PROTOCOL);
this.username=username;
this.password=password;
}
@Override
public void handleMessage(SoapMessage msg) throws Fault {
List<Header> headers= msg.getHeaders();   
Document document = DOMHelper.createDocument();
Element root = document.createElement("xwolf");
Element name= document.createElement("name");
name.setTextContent(username);
root.appendChild(name);
Element pwd= document.createElement("pwd");
pwd.setTextContent(password);
root.appendChild(pwd);
headers.add(new Header(new QName("xwolf"),root));
System.out.println("client端用户身份校验拦截器实现.....");
}
}
  客户端代码:



import java.util.List;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.message.Message;
import com.wa.CheckUserInterceptor;
import com.wa.HelloImplService;
import com.wa.HelloInt;
/**
* 客户端进行测试
*
*/
public class ClientTest {
public static void main(String[] args) {
HelloImplService service = new HelloImplService();
HelloInt hello = service.getHelloImplPort();
//客户端拦截器
Client client =ClientProxy.getClient(hello);
List<Interceptor<? extends Message>> lists =client.getOutInterceptors();
lists.add(new CheckUserInterceptor("xow0","11232"));
//client.getInInterceptors().add(new LoggingInInterceptor());
//client.getInInterceptors().add(new LoggingOutInterceptor());
        
System.out.println(hello.sayHello("what do you say?please say agin."));
}
}
  运行后,服务端输出信息:



2014-12-31 22:59:00 org.apache.cxf.services.HelloImplService.HelloImplPort.HelloInt
信息: Inbound Message
----------------------------
ID: 1
Address: http://192.168.1.64:8989/d01ws/hello?wsdl
Encoding: UTF-8
Http-Method: GET
Content-Type: text/xml
Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], content-type=[text/xml], Host=[192.168.1.64:8989], Pragma=[no-cache], User-Agent=[Apache CXF 2.5.9]}
--------------------------------------
2014-12-31 22:59:01 org.apache.cxf.services.HelloImplService.HelloImplPort.HelloInt
信息: Inbound Message
----------------------------
ID: 2
Address: http://192.168.1.64:8989/d01ws/hello
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[285], content-type=[text/xml; charset=UTF-8], Host=[192.168.1.64:8989], Pragma=[no-cache], SOAPAction=[""], User-Agent=[Apache CXF 2.5.9]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><xwolf><name>xow0</name><pwd>11232</pwd></xwolf></soap:Header><soap:Body><ns2:sayHello xmlns:ns2="http://wa.com/"><arg0>what do you say?please say agin.</arg0></ns2:sayHello></soap:Body></soap:Envelope>
--------------------------------------
  客户端消息:



2014-12-31 23:05:20 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
信息: Creating Service {http://wa.com/}HelloImplService from WSDL: http://192.168.1.64:8989/d01ws/hello?wsdl
client端用户身份校验拦截器实现.....
2014-12-31 23:05:20 org.apache.cxf.services.HelloImplService.HelloImplPort.HelloInt
信息: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {Content-Length=[236], content-type=[text/xml;charset=UTF-8], Server=[Jetty(7.5.4.v20111024)]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://wa.com/"><return>hello,what do you say?please say agin.</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>
--------------------------------------
hello,what do you say?please say agin.
  测试结束。
  2014年还有一个小时结束,祝福送上..........

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-348658-1-1.html 上篇帖子: 博客总目录 下篇帖子: Java学习路线(转)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表