zoukankan      html  css  js  c++  java
  • CXF客户端添加header权限认证

    https://blog.csdn.net/showgood119/article/details/78856038

    cxf作为web service客户端,有以下几种方式,分别说明在方式下怎样添加header权限认证

    假设服务端已经设置了权限认证,并且头部信息为

    1.  
      <soapenv:Header>
    2.  
      <auth>
    3.  
      <name>admin</name>
    4.  
      <password>123456</password>
    5.  
      </auth>
    6.  
      </soapenv:Header>

    方式一

    1. 先添加统一的拦截器

    1.  
      public class ClientAuthInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
    2.  
       
    3.  
      private static final String NAME = "admin";
    4.  
       
    5.  
      private static final String PASSWORD = "123456";
    6.  
       
    7.  
      public ClientAuthInterceptor() {
    8.  
      //准备发送阶段
    9.  
      super(Phase.PREPARE_SEND);
    10.  
      }
    11.  
       
    12.  
      @Override
    13.  
      public void handleMessage(SoapMessage message) throws Fault {
    14.  
      List<Header> headers = message.getHeaders();
    15.  
       
    16.  
      Document doc = DOMUtils.createDocument();
    17.  
       
    18.  
      Element auth = doc.createElement("auth");
    19.  
       
    20.  
      Element name = doc.createElement("name");
    21.  
      name.setTextContent(NAME);
    22.  
       
    23.  
      Element password = doc.createElement("password");
    24.  
      password.setTextContent(PASSWORD);
    25.  
       
    26.  
      auth.appendChild(name);
    27.  
      auth.appendChild(password);
    28.  
       
    29.  
      headers.add(new Header(new QName(""), auth));
    30.  
      }
    31.  
      }

    2. 编写客户端

    1.  
      public static void main(String args[]) {
    2.  
      JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
    3.  
      Client client = clientFactory.createClient("http://localhost:8080/admin/cxfService?wsdl");
    4.  
      try {
    5.  
      client.getOutInterceptors().add(new ClientAuthInterceptor());
    6.  
      Object[] result = client.invoke("sayHello", "KEVIN");
    7.  
      System.out.println(result[0]);
    8.  
      } catch (Exception e) {
    9.  
      e.printStackTrace();
    10.  
      }
    11.  
      }



    方式二

    1. 先添加客户端的拦截器,跟方式一中的相同

    2. 使用cxf提供的代码生成工具 wsdl2java 生成代码,wsdl2java的使用方式,请谷歌

    wsdl2java -p com -d ../src -client -encoding utf-8  http://localhost:8080/admin/cxfService?wsdl

    3.编写客户端

    1.  
      public static void main(String args[]) {
    2.  
      HelloWorldImplService ss = new HelloWorldImplService();//自动生成的类
    3.  
      HelloWorld port = ss.getHelloWorldImplPort();//自动生成的类
    4.  
      Client client= ClientProxy.getClient(port);
    5.  
      client.getOutInterceptors().add(new ClientAuthInterceptor());
    6.  
      System.out.println(port.sayHello("hello,world"));
    7.  
      }



  • 相关阅读:
    CSS 之 样式优先级机制
    SQL Server 之 修改时不允许保存更改
    IP_TOS选项
    eXosip2代码分析
    Ubuntu下关闭apache和mysql的开机启动
    linphone3.4.0代码分析
    XMPP/Jingle Vs SIP/SIMPLE 的简单介绍
    git命令常见问题总结
    正向代理与反向代理的区别
    SIP中OPTIONS方法的用法及示例
  • 原文地址:https://www.cnblogs.com/linus-tan/p/13680143.html
Copyright © 2011-2022 走看看