Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authorization bearer soap request

I need to somehow include in my soap service envelope a header whose values are "Authorization" and "Bearer xXsomeCode123xX", this is the part of the code I need to edit with the new header, which then returns me an xml document with some data requested through the soap web service.

            //SERVICE
            Service service = new Service();
            Call call = (Call) service.createCall();    
            call.setTargetEndpointAddress(new java.net.URL(endPointURL));
            call.setSOAPActionURI(soapActionURI);   

            //INVOKE
            SOAPEnvelope response = (SOAPEnvelope) call.invoke(new Message(soap.asXML()));              
            Document doc = new DOMReader().read(response.getAsDocument());  
            return doc;

This is what i see in SoapUI (and works with the header set like that)

This is what i see in SoapUI

I know the question is confusing, and I am, I've researched on the internet but almost every header is made of username:password fields, while i only have this "Authorization" to work on

like image 513
GiLA3 Avatar asked Sep 20 '25 09:09

GiLA3


1 Answers

If you are using WebServiceTemplate you can do the method marshalSendAndReceive with a WebServiceMessageCallback , in these callback you can add RequestHeader in that you can put the Authorization and bearer + token, your method doWithMessage must looks like as bellow:

public void doWithMessage(WebServiceMessage webServiceMessage) throws IOException, TransformerException {
                    TransportContext context = TransportContextHolder.getTransportContext();
                    HeadersAwareSenderWebServiceConnection connection = (HeadersAwareSenderWebServiceConnection) context.getConnection();
                    connection.addRequestHeader("Authorization", String.format("Bearer %s", "********"));

                }

More info in this link

like image 85
user2581462 Avatar answered Sep 23 '25 20:09

user2581462