java 标题中带有用户名和密码的 SOAP 客户端请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34921812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:32:34  来源:igfitidea点击:

SOAP client request with username and password in header

javawsdlws-security

提问by Toby Derrum

I have the the following Java client for making a SOAP request:

我有以下用于发出 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();
        }        
    }
}

I need to modify this to include a username and password as part of the WS security header. How would I go about doing this ?

我需要修改它以包含用户名和密码作为 WS 安全标头的一部分。我该怎么做呢?

In case it is useful I have also attached my WSDL file:

如果有用,我还附上了我的 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>

回答by Pankaj Pandey

In Client Side you need to write below code to pass use/pwd as

在客户端,您需要编写以下代码以将 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);

Please encrypt password before sending to server

发送到服务器前请加密密码

Please follow below link for more details for server side changes

请按照以下链接了解有关服务器端更改的更多详细信息

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

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

if you want to implement jax WS-Security then please follow this link

如果你想实现 jax WS-Security 那么请点击这个链接

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

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