简体   繁体   中英

SAAJ java client - add authentication

i'm making a webservice client with SAAJ java. I'n the client i ask info of the webservice. But the webservice is protected with a username and password.

And i don't no how i must add these 2.

I tried it in my code ( see commands ) but no results ..

// SAAJ - SOAP Client Testing public static void main(String args[]) {

    String soapEndpointUrl = "https://gtstvs01:8443/aeosws";
    String soapAction = "";

    callSoapWebService(soapEndpointUrl, soapAction);
}

private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {
    SOAPPart soapPart = soapMessage.getSOAPPart();

    String myNamespace = "sch";
    String myNamespaceURI = "http://www.nedap.com/aeosws/schema";

    // SOAP Envelope
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);



    //SOAPFactory soapFactory = SOAPFactory.newInstance();
   // SOAPElement authHeaderElement = soapFactory.createElement("AuthenticationHeader", "administrator", "aeosrules");
    //SOAPElement sessionIdElement = soapFactory.createElement("SessionID", "nsprefix", "nsuri");
    //sessionIdElement.addTextNode(MY_SESSION_ID);
    //authHeaderElement.addChildElement(sessionIdElement);

    //SOAPHeader soapHeader = envelope.addHeader();
    //soapHeader.addChildElement(authHeaderElement);


    // SOAP Body
    SOAPBody soapBody = envelope.getBody();
    SOAPElement soapBodyElem = soapBody.addChildElement("EmployeeSearchInfo", myNamespace);
    SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("EmployeeInfo", myNamespace);
    SOAPElement soapBodyElem2 = soapBodyElem1.addChildElement("FirstName", myNamespace);
    soapBodyElem2.addTextNode("Jens");
}

private static void callSoapWebService(String soapEndpointUrl, String soapAction) {
    try {
        // Create SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();


        // Send SOAP Message to SOAP Server
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);

        // Print the SOAP Response
        System.out.println("Response SOAP Message:");
        soapResponse.writeTo(System.out);
        System.out.println();

        soapConnection.close();
    } catch (Exception e) {
        System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
        e.printStackTrace();
    }
}

private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();

    createSoapEnvelope(soapMessage);

    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("SOAPAction", soapAction);

    soapMessage.saveChanges();

    /* Print the request message, just for debugging purposes */
    System.out.println("Request SOAP Message:");
    soapMessage.writeTo(System.out);
    System.out.println("\n");

    return soapMessage;
}

From what I understand, you have to add it to the header.

In your code for CreateSoapRequest you need 3 additional lines.

MimeHeaders headers = soapMessage.getMimeHeaders();
String encoded = new sun.misc.BASE64Encoder().encode((username+":"+password).getBytes());
String authString = "Basic " + encoded;
headers.addHeader("Authorization", authString);
headers.addHeader("SOAPAction", soapAction);

I think it matters what type of encoding the site has. So this probably won't work in a cut and paste you have to figure out what it requires.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM