SOAP客户端请求,标题中包含用户名和密码

托比·德鲁姆(Toby Derrum)

我具有以下用于发出SOAP请求的Java客户端:

package com.example.petstore.test;

import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.example.petstore.schema.ProcessUpdateResponse;
import com.example.petstore.schema.SyncProcessDAO;

public class TestUtility {

    public static void main(String[] args) {

        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();        

        // Use the URL defined in the soap address portion of the WSDL
        factory.setAddress("http://192.168.1.4:8080/MySyncService/services/SyncProcessDAOPort"); 

        // Utilize the class which was auto-generated by Apache CXF wsdl2java
        factory.setServiceClass(SyncProcessDAO.class);        

        Object client = factory.create();

        try {        

            // Call the Web Service to perform an operation
            GregorianCalendar gregory = new GregorianCalendar();
            XMLGregorianCalendar xmlgregory = DatatypeFactory.newInstance()
                    .newXMLGregorianCalendar(gregory);
            ProcessUpdateResponse response = ((SyncProcessDAO)client).gatherFunctionAttributes("hello1", "hello2", "hello3", 1, 2, xmlgregory, xmlgregory, "hello4", "hello5");    
             System.out.println("hahahaha");
            System.out.println(response);    

          } catch (SecurityException e) {

            e.printStackTrace();

          } catch (IllegalArgumentException e) {

            e.printStackTrace();

          } catch (DatatypeConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }        
    }
}

我需要对其进行修改,以将用户名和密码作为WS安全标头的一部分。我将如何去做呢?

如果有用,我还附加了我的WSDL文件:

    <?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="SyncProcessDAOService" targetNamespace="http://example.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://example.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
  <wsdl:types>
    <schema xmlns="http://www.w3.org/2001/XMLSchema">
  <import namespace="http://example.com/" schemaLocation="my_schema1.xsd"/>
</schema>
  </wsdl:types>
  <wsdl:message name="gatherFunctionAttributesResponse">
    <wsdl:part name="parameters" element="tns:gatherFunctionAttributesResponse">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="gatherFunctionAttributes">
    <wsdl:part name="parameters" element="tns:gatherFunctionAttributes">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="SyncProcessDAO">
    <wsdl:operation name="gatherFunctionAttributes">
      <wsdl:input name="gatherFunctionAttributes" message="tns:gatherFunctionAttributes">
    </wsdl:input>
      <wsdl:output name="gatherFunctionAttributesResponse" message="tns:gatherFunctionAttributesResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="SyncProcessDAOServiceSoapBinding" type="tns:SyncProcessDAO">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="gatherFunctionAttributes">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="gatherFunctionAttributes">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="gatherFunctionAttributesResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="SyncProcessDAOService">
    <wsdl:port name="SyncProcessDAOPort" binding="tns:SyncProcessDAOServiceSoapBinding">
      <soap:address location="http://localhost:8080/MySyncService/services/SyncProcessDAOPort"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>
潘卡·潘迪(Pankaj Pandey)

在客户端,您需要编写以下代码以将use / pwd传递为

SyncProcessDAO client = (SyncProcessDAO)factory.create();
Map<String, Object> requestContext =   ((BindingProvider)client).getRequestContext();                              
Map<String, List<String>> requestHeaders = new HashMap<String, List<String>>();
requestHeaders.put("username", "user");
requestHeaders.put("Password", "pwd");
requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, requestHeaders);

请先加密密码,然后再发送到服务器

请点击以下链接以获取服务器端更改的更多详细信息

http://examples.javacodegeeks.com/enterprise-java/jws/application-authentication-with-jax-ws/

如果您想实现jax WS-Security,请点击此链接

http://cxf.apache.org/docs/ws-security.html

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

获取连接到Web服务器的客户端的用户名

如何将具有用户名和密码的Java mqtt客户端连接到emqttd(EMQ)代理?

使用用户名令牌和客户端证书的WCF SOAP调用

使用ruby的用户名和密码的http请求

REST授权:授权标题中的用户名/密码与JSON正文

“用户=身份验证(请求,用户名=用户名,密码=密码)”用户为无

如何使用Java在SOAP UI中使用用户名和密码发送SOAP请求

如何实现Grpc用户名/密码认证。Python客户端,Java服务器

如何在Java客户端中使用用户名和密码(无SSL)连接到Elasticsearch?

.Net Core 2.1获取客户端计算机的Windows用户名

在C#asp.net core 2.1中通过用户名和密码对MQTT服务器上的MQTT客户端进行身份验证

使用mod_jk将用户名和客户端证书从apache传递到tomcat

将JavaScript客户端添加到IdeniityServer4并使用用户名和密码登录

如何允许客户端使用用户名和密码登录Samba共享?

Spring Security Oauth2资源所有者密码流:当我发送REST请求时,我的用户详细信息服务始终获取客户端ID而不是用户名

OutputCache阻止我的用户名显示在标题中

是否可以使用WIF客户端从客户端传递用户名和密码来登录ADFS联合身份验证?

OpenSSH客户端中用户名后的整数,以冒号分隔

僵尸网络客户端如何访问用户的用户名和密码?

输入Alamofire请求的用户名和密码

Yii:登录时更改标题中的用户名样式

smtp客户端并从配置代码访问用户名和密码

用户名和密码检查

如何在 python 中的 Twisted 客户端服务器应用程序中添加身份验证(用户名/密码)?

使用用户名和密码将 Twilio 注册为扩展/端点 SIP 客户端

Linux中的套接字TCP - 客户端无法插入用户名

节点 express.js 和客户端 js 中的实时用户名可用性检查

当需要对客户端进行身份验证时,使用长期令牌还是存储加密的用户名和密码?

重新连接身份验证用户名和密码的 Hivemq java 客户端不成功它正在尝试使用用户名和密码重新连接