如何在基于SOAP的Web服务客户端中进行抢占式认证

时间:2015-01-05 13:07:00

标签: java web-services authentication soap

我正在开发一个需要创建Web服务客户端元素的项目。我使用SAOPFACTORYCONNECTION创建了简单的Web服务客户端元素。我成功地创建了它,这是该客户端的代码。

//import java.util.Date;

//import com.audium.server.AudiumException;
//import com.audium.server.session.ActionElementData;
//import com.audium.server.voiceElement.ActionElementBase;
//import com.ef.clients.warid.custom.Repository;

import javax.xml.soap.*;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;

//import com.ef.clients.warid.ucip.UcipUtility;

/**
 * @author 
 * 
 */
public class MainClass {


    public static void main (String[] arg)
    {

        // Repository repo=new Repository();

//      String splan = (String) data.getSessionData("SPLANID");
//      String ani = (String) data.getSessionData("ANI");
        String ani="03342323232";
        String splan="dfdf";
        String response_code = "";

        try {

            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();


            String url="http://192.108.100.47:9090/CBR/services/callerStatus?wsdl"  ;
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(ani, splan), url);

            System.out.println("So after the URL ");
            SOAPBody soapBody = soapResponse.getSOAPBody();

            Source sourceContent = soapResponse.getSOAPPart().getContent();

            StreamResult result = new StreamResult(System.out);
            transformer.transform(sourceContent, result);

//          while (iterator.hasNext()) {
//              System.out.println("In the loop");
//              SOAPBodyElement bodyElement = (SOAPBodyElement) iterator.next();
//              response_code = bodyElement.getTextContent();
//          //  System.out.println("Response code is "+response_code);
//          }

            soapConnection.close();

        } catch (Exception ex)
        {
            System.out.println("Here is the exception "+ex.toString());

        } finally {

            // unica.cleanRepositories();
        }
    }

    public static SOAPMessage createSOAPRequest(String subno, String splan)
            throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();

        SOAPPart soapPart = soapMessage.getSOAPPart();


        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
//      setOptionalHeaders(soapMessage, envelope);
        envelope.addNamespaceDeclaration("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
        envelope.addNamespaceDeclaration("ws", "http://ws.services.cbr.ef.com/");

            // SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement GetTagStatus=  soapBody.addChildElement("getCallerStatus");

        SOAPElement ani = GetTagStatus.addChildElement("ani");

        ani.addTextNode("1234567");


        soapMessage.saveChanges();

        /* Print the request message */
        //System.out.print("Request SOAP Message = ");
        soapMessage.writeTo(System.out);

        return soapMessage;
    }

}

此代码工作正常,现在我需要创建另一个客户端,但在该客户端有一个不同的,在该客户端我需要添加抢占式身份验证。我怎样才能做到这一点。 ??这是我的问题

如果我在SOAPUI工具中访问该Web服务URL,那么我需要从Auth中选择Preeptive类型并给出用户名密码,它可以正常工作。现在我需要在Java代码中使用相同的代码,我该怎么做。

2 个答案:

答案 0 :(得分:0)

检查您的SoapUI HTTP日志。看看SoapUI发送的标题类型等等。 我还被其中一个Web服务提供商的帮助部分中的“抢占式身份验证”需求所误导。 事实上,我需要在SOAP请求中包含SOAPAction标头 - 这解决了问题。

答案 1 :(得分:-1)

是的,添加SOAPAction。我也面临同样的问题。在我的例子中,SOAPAction是操作名称。

MimeHeaders hd = soapMessage.getMimeHeaders();
hd.addHeader("SOAPAction", "getUserDetails");
String authorization = new sun.misc.BASE64Encoder().encode((username+":"+password).getBytes());
hd.addHeader("Authorization", "Basic " + authorization);
相关问题