0

I'm working with spring-ws, I need to perform simple username and password validation on my webservice calls. For that, I need to obtain the default soap header attributes for username and password. I have no idea how to do that with spring-ws and I've spent a lot of time trying to figure it out.

I'm using SoapUI and sending the username and password by setting them in the "Aut" tab.

My interceptor intercepts my calls, but I've debugged the messageContext and I have no idea where those fields are.

This is my code, it never enters to the code in the while, so the iterator is return false in the hasNext.

 @Override
 public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
   logger.info("Intercepting call");
   SoapMessage soapMessage = (SoapMessage) messageContext.getRequest();
   SoapHeader soapHeader = soapMessage.getSoapHeader();

   Iterator<SoapHeaderElement> it = soapHeader.examineAllHeaderElements();
   while(it.hasNext()) {
    SoapHeaderElement element = it.next();
    QName qname = element.getName();
    logger.info("QName: " + qname.getLocalPart());
    logger.info("Value? " + soapHeader.getAttributeValue(qname));
  }

return true;
}

If you would kindly point me towards any documentation on the subject, or better yet, some examples, it will be a great help.

Thanks a lot.

Harry Potel
  • 83
  • 1
  • 11

2 Answers2

2

Ok, so I think that I've found the answer, my problem wasn't the code, but the eclipse plugin for SoapUI which was not really sending the authorization header. I've downloaded the desktop client, configured properly, and now I see the header.

With that enabled, I wrote this code, and was able to obtain the user and password:

TransportContext transportContext =  TransportContextHolder.getTransportContext();
HttpServletRequest servletRequest = ((HttpServletConnection) transportContext.getConnection()).getHttpServletRequest();
String authorizationHeader = servletRequest.getHeader("authorization");
String userAndPasswordBase64 = authorizationHeader.substring("Basic ".length());
String userAndPassword = new String(Base64.decodeBase64(userAndPasswordBase64));

int colonIndex = userAndPassword.indexOf(':');
String user = userAndPassword.substring(0, colonIndex);
String password = userAndPassword.substring(colonIndex + 1);

This question was the one that really helped me unlock this though: How to access HTTP headers in Spring-ws endpoint?

Thanks a l

Community
  • 1
  • 1
Harry Potel
  • 83
  • 1
  • 11
0
SOAPMessage soapMessage = context.getMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
soapEnvelope.getHeader();
sercasti
  • 550
  • 3
  • 7