Question

I am trying to develop a generic SOAP client web service. I'm stuck in the HTTP Basic authentication part, I have a service that requires this type of authentication and I use SOAP UI API.

My problem is how to inject the username and password in the HTTP header for the basic type with SOAP UI API.

I tried with this code but I have this error : soap:mustUnderstand = 1

the main class of the authentication of the Web Service:

     public class BasicAuthAuthorizationInterceptor extends SoapHeaderInterceptor {
        @SuppressWarnings("unchecked")
        @Override
        public void handleMessage(Message message) throws Fault {

            Map<String, List<String>> requestHeaders = (Map<String,

 List<String>>) message
                .get(Message.PROTOCOL_HEADERS);
        List<String> authHeaderList = requestHeaders.get("Authorization");

        if (authHeaderList != null && authHeaderList.size() > 0) {

            if ((authHeaderList.get(0)).equals("Basic c2FicmluZTpzYWJyaW5lcGFzcw==")){
                System.out.println("The user is known +++++++");

            } else {
                sendErrorResponse(message, HttpURLConnection.HTTP_FORBIDDEN);
                System.out.println("The user is unknown -------");
            }
        } else {
            sendErrorResponse(message, HttpURLConnection.HTTP_UNAUTHORIZED);
            // System.out.println("No user ************");
            return;
        }

    }

method used by the Web service cLient to authenticate:

public List<SoapOutput> execute(String user, String pass) throws IOException {

if ((user!=null && pass!=null) && (!user.equals("") && !pass.equals("")))
            {
                // Add WSSecurity parameters
                wsdlRequest.setWssPasswordType("PasswordText");
                wsdlRequest.setUsername(user);
                wsdlRequest.setPassword(pass);
                wsdlRequest.setWssTimeToLive("10000");
            }


            Map<String, List<String>> requestHeaders  = wsdlRequest.getRequestHeaders();
            requestHeaders.put("username", Collections.singletonList(user));
            requestHeaders.put("Password", Collections.singletonList(pass));

            System.out.println("requestHeaders is: " + requestHeaders);

....
Was it helpful?

Solution

you can try the following code in the client above:

if ((user!=null && password!=null ) && (!user.equals("") && !password.equals(""))) {
StringToStringsMap requestHeaders = wsdlRequest.getRequestHeaders();
    String basicAuthentication = getBasicAuthentication(user, password);
    requestHeaders .put("Authorization", basicAuthentication);
    wsdlRequest.setRequestHeaders(requestHeaders );
}

private String getBasicAuthentication(String user, String password) {
    String token = user + ":" + password;
    try {
        return "Basic "
                + DatatypeConverter.printBase64Binary(token
                        .getBytes("UTF-8"));
    } catch (UnsupportedEncodingException ex) {
        throw new IllegalStateException("Cannot encode with UTF-8", ex);
    }
}

OTHER TIPS

Try the following code (groovy)

import com.eviware.soapui.support.types.StringToStringsMap 

def headers = new StringToStringsMap()
headers.put(testSuite.getPropertyValue("ExampleHeaderName"),testSuite.getPropertyValue("ExampleHeaderValue"))

testStep.getTestRequest().setRequestHeaders(headers)

More details on this approach has been discussed at http://forum.soapui.org/viewtopic.php?t=3746 or http://www.techinterviewpuzzles.com/2013/07/soapui-add-header-to-all-requests-using.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top