• webservice拦截器 查看消息包(soap)


    服务端:

        1.获取EndpointImpl对象

        2.调用EndpointImpl对象中的方法获取In拦截器
        3.调用EndpointImpl对象中的方法获取out拦截器

        4.添加自己的In拦截器与Out拦截器

          LogginInInterceptor:查看收到的消息包

          LoggOutInterceptor:查看发出去的消息包

    客户端:

        需要导入cxf的7个jar包:

        1.获取client对象

          Client client=ClientProxy.getClient("ws服务组件代理对象") ---ws服务组件 就是  new 服务接口().getXxxPort();  就是服务端服务接口的实现类

        2.调用client对象中的方法获取In拦截器
        3.调用client对象中的方法获取out拦截器

        4.添加自己的In拦截器与Out拦截器

          LogginInInterceptor:查看收到的消息包

          LoggOutInterceptor:查看发出去的消息包

    ------------------------添加权限拦截器-----------------------------------

    1.在soap包中添加Header元素,携带鉴定权限的数据

    <Header>

      <auth>

        <auth_id>账号</auth_id>

        <auth_pwd>密码</auth_pwd>

      </auth>

    </Header>

    2.客户端------在发送soap包之前修改header头元素需要添加out拦截器

    3.服务端------添加in拦截器解析soap包header头元素

    4.自定义拦截器--需要继承AbstractPhaseInterceprot;

    ----------------------------自定义拦截器案例----------------------------

    public class HeaderInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
    private String authId;
    private String pwd;

    public HeaderInterceptor(String authId, String pwd) {
    /** 在发送soap包之前该拦截器起作用 */
    super(Phase.PREPARE_SEND);
    this.authId = authId;
    this.pwd = pwd;
    }

    @Override
    public void handleMessage(SoapMessage soapMessage) throws Fault {

    /**
    * <auth>
    <auth_id>admin</auth_id>
    <auth_pwd>888888</auth_id>
    </auth>
    */
    // 创建Document
    Document doc = DOMUtils.createDocument();
    // 创建Element
    Element authEle = doc.createElement("auth");
    Element idEle = doc.createElement("auth_id");
    // 添加文本
    idEle.setTextContent(authId);
    Element pwdEle = doc.createElement("auth_pwd");
    // 添加文本
    pwdEle.setTextContent(pwd);

    // 追加元素
    authEle.appendChild(idEle);
    authEle.appendChild(pwdEle);

    // 创建Header头
    Header header = new Header(new QName("it"), authEle);

    // 获取所有的header头, 添加header头
    soapMessage.getHeaders().add(header);

    }

    public String getAuthId() {
    return authId;
    }

    public void setAuthId(String authId) {
    this.authId = authId;
    }

    public String getPwd() {
    return pwd;
    }

    public void setPwd(String pwd) {
    this.pwd = pwd;
    }

    }

  • 相关阅读:
    再学梳理数据指标体系
    mac下Robot Framework-demo脚本
    Robot Framework-环境搭建
    Tensorflow学习笔记No.11
    Tensorflow学习笔记No.10
    Tensorflow学习笔记No.9
    Tensorflow学习笔记No.8
    Tensorflow学习笔记No.7
    Tensorflow学习笔记No.6
    Tensorflow学习笔记No.4.2
  • 原文地址:https://www.cnblogs.com/ly-china/p/5444516.html
Copyright © 2020-2023  润新知