如何在Web服务中添加身份验证标头?

时间:2014-07-15 09:14:50

标签: java web-services authentication soap wsdl

我有一个网络服务

`<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <AuthenticationHeader xmlns="http://tempuri.org/">
      <UserID>int</UserID>
      <Password>string</Password>
    </AuthenticationHeader>
  </soap:Header>
  <soap:Body>
    <InsertPartnerComplaint xmlns="http://tempuri.org/">
      <ServicePartner>
        <PartnerID>int</PartnerID>
        <VCNo>string</VCNo>
        <MobileNo>string</MobileNo>
        <Remarks>string</Remarks>
        <ReferenceNo>string</ReferenceNo>
        <ComplaintNo>decimal</ComplaintNo>
        <Status>short</Status>
        <ErrorDescription>string</ErrorDescription>
        <SyncStatus>short</SyncStatus>
        <ClientIPAddress>string</ClientIPAddress>
      </ServicePartner>
    </InsertPartnerComplaint>
  </soap:Body>
</soap:Envelope>`

我无法在其中添加身份验证标头,我正在采用这种方法

`PartnerComplaintServices partnerComplaintServices=new PartnerComplaintServices();
PartnerComplaintServicesSoap partnerComplaintServicesSoap=partnerComplaintServices.getPartnerComplaintServicesSoap();
        ServicePartner servicePartner=new ServicePartner();
        System.out.println(partnerComplaintServices.getHandlerResolver());
        servicePartner.setPartnerID(1);
        servicePartner.setVCNo("01507400066");
        servicePartner.setMobileNo("9872345648");
        servicePartner.setRemarks("test");
        servicePartner.setReferenceNo("test");
partnerComplaintServicesSoap.insertPartnerComplaint(servicePartner);`

有一个包含身份验证的objectfactory。 wldl的网址是 http://180.179.201.148:9443/Services/ServicePartner/PartnerComplaintServices.asmx?WSDL

1 个答案:

答案 0 :(得分:2)

我在类中创建了那些静态方法,但是特定于轴。

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;

import org.apache.axis.client.Stub;
import org.apache.axis.message.PrefixedQName;
import org.apache.axis.message.SOAPHeaderElement;

public class SoapHeaderCreator {
    /**
     * 
     * @param stub
     * @param user
     * @param password
     * @throws SOAPException
     */
    public static void addAuthenticationOASIS(Stub stub, String user, String password) throws SOAPException{
        //Define security header
        QName qname = new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd","Security","wsse");
        SOAPHeaderElement header = new SOAPHeaderElement(qname);
        String token = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText";
        header.setMustUnderstand(true);
//      header.setActor("");
        SOAPElement node = header.addChildElement("UsernameToken");         
        SOAPElement node2 = node.addChildElement("Username");
        node2.addTextNode(user);
        SOAPElement node3 = node.addChildElement("Password");
        node3.addTextNode(password);
        PrefixedQName name = new PrefixedQName("", "Type", "");
        node3.addAttribute(name, token);
        stub.setHeader(header);
    }
}
相关问题