Bader Javaid
Bader Javaid

Reputation: 21

How to add Authentication Header to WebService Stub?

I have generated stubs using Apache CXF, IBM Jax-WS and Axis as well in Eclipse and RAD 7.0 .

In all the 3 above scenarios it gives me the following exception

Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: ARERR [149] A user name must be supplied in the control record

After searching i concluded that i have to add Authentication Info to the Soap header created by Client Stubs.

i tried the answer on this link

How do you add a Soap Header defined in a wsdl to a web service client in CXF?

but couldn't succeed. I am newbie to SOAP and WebServices

So if anyone has worked on it kindly Help.

Upvotes: 2

Views: 4428

Answers (1)

Jupiter Jones
Jupiter Jones

Reputation: 863

I had this problem a few days ago and it was a headache for me. When generating the stubs you have to add a the flag -XadditionalHeaders to the wsimport command.

"C:\Program Files\Java\jdk1.X.X_XX\bin\wsimport.exe" -p com.company.package -keep -XadditionalHeaders -d folder1 http://mywsdllocation.com/doc.wsdl

Where: -p: package that will contain generated classes -keep: keep .java files (otherwise tou'll only get .class files) -XadditionalHeaders: classes for authentication will be created -d: Folder where generated classes will be placed.

After that, you only have to copy generated java files to your project, under the picked package (com.company.package in this case). Then you have you create an AuthenticationInfo object and inserting it in the stub method's call, something like this:

WSService service = new WSService(); 
WSPortTypePortType port = service.WSPortTypeSoap();
AuthenticationInfo auth = new AuthenticationInfo();
auth.setUserName(yourUsername);
auth.setPassword(yourPassword);
port.method(param1,param2,auth);

Hope it helps!

Upvotes: 2

Related Questions