簡體   English   中英

在Android中使用kso​​ap2將SAML令牌添加到標頭

[英]Adding SAML token to Header with ksoap2 in Android

我正在用SOAP WS開發一個Android應用程序,當我從請求中獲取屬性時,它運行良好,但是當我向Header添加SAML令牌時,服務器每次都會返回相同的錯誤。

Error Reading XMLStreamReader

發送標頭元素時的WS請求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wss="...URL....">
   <soapenv:Header>
      <wsse:Security .....>
         <saml:Assertion ID=""........></Assertion>
      </wsse:Security>
   </soapenv:Header>
   <soapenv:Body>
      <wss:consultaUnitats>
         <!--Optional:-->
         <identificadorMissatge></identificadorMissatge>
         <data></data>
      </wss:consultaUnitats>
   </soapenv:Body>
</soapenv:Envelope>

而我的Java代碼:

private boolean doUnits(String tokenID, String data) throws IOException {
    request = new SoapObject(NAMESPACE, METHOD_NAME);       request.addProperty("identificadorMissatge", ""); //Optional
    request.addProperty("data", data);      

    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.headerOut = new Element[1];
    envelope.headerOut[0] = getHeader();

    envelope.setAddAdornments(false);
    envelope.implicitTypes = true;
    envelope.setOutputSoapObject(request);

    androidHttpsTransport = new HttpTransportSE(URL);
    androidHttpsTransport.debug = true;
    try {           
        androidHttpsTransport.call(SOAP_ACTION, envelope);

        Log.i("ENVELOPE", androidHttpsTransport.requestDump);
        Log.i("ENVELOPE", androidHttpsTransport.responseDump);

        if (envelope.bodyIn instanceof SoapFault) {
            String str = ((SoapFault) envelope.bodyIn).faultstring;
        Log.i("", str);
        } else {
            response = (SoapObject) envelope.bodyIn;
        Log.d("WS", String.valueOf(response));
        }
return result;
}

    private Element getHeader() {
        Element header = new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "wsse:Security");
        header.addChild(Node.TEXT, tokenID);
        return header;
    }

我的問題:

也許在創建新元素時需要添加元素而不是文本? * tokenID是一個字符串,我在里面添加了SAML。 這是問題嗎?

最后,我修復並完美工作。

我將我的代碼共享給另一個有相同問題的人。

如果將SAML代碼放在String中,則需要解析並重新解析為Document。 怎么樣? 這是我的代碼:

private Element getHeader() throws XmlPullParserException, IOException {
    // First Parse
        XmlPullParserFactory factory = null;
        factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput((Reader)new StringReader(tokenID));

    // Second Parse and create a Document        
        Document doc = new Document();
        doc.parse(xpp);

    //Create Element and add Child        
        Element header = new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security");
        header.addChild(Node.ELEMENT, doc.getChild(0));
        return header;
    }

將標題添加到Envelope.headerOut:

envelope.headerOut = new Element[1];
envelope.headerOut[0] = getHeader();

我希望這段代碼可以幫助其他開發人員。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM