Java Invoke Web Service Add Authentication Header (Soapenv:header) __220-soa

Source: Internet
Author: User
Tags soapui wsdl
Preface

Sometimes calling a Web service will appear

Message does not conform to configured policy [Authenticationtokenpolicy (S)]: No security Header found

Such a mistake.

In view of the results of the SOAPUI call, the following returns


This error occurs because the WebService server needs to provide a SOAP-authenticated header.

For example, you might need to add the following certification headers:

        <soapenv:Header>
          <wsse:security xmlns:wsse= "http://docs.oasis-open.org/wss/2004/01/ Oasis-200401-wss-wssecurity-secext-1.0.xsd "soapenv:mustunderstand=" 1 ">
            <wsse:UsernameToken>
              <wsse:Username>UserName</wsse:Username>
              <wsse:password type= "http://docs.oasis-open.org/wss/ 2004/01/oasis-200401-wss-username-token-profile-1.0#passwordtext ">Password</wsse:Password>
            </ wsse:usernametoken>
          </wsse:Security>
        </soapenv:Header>
(The format of this detail is related to the requirements of the service side, and the specific username and pass are also provided by the service side)

When the Sopaui is called, the call succeeds with a similar addition.


In the SOAPUI call, you can do it in the above way. How do you add authentication headers after you have converted the WSDL to Java?


CXF Plus certified head

(The above authentication head, compare close to Cxf call way.) )

If you are using the code for the client that CXF produces.

(How to build, reference CXF Generate Web Service Client (translate WSDL into Java code))

Add the following code at the time of the _client call: (before the code called by the method)

	    map<string, object> props = new hashmap<string, object> ();
	    Props.put (Wshandlerconstants.action, wshandlerconstants.username_token);	  
	    Props.put (wshandlerconstants.password_type,wsconstants.pw_text);
	    Props.put (Wshandlerconstants.user, "UserName");
	    Props.put (Wshandlerconstants.pw_callback_class, PasswordHandler.class.getName ());
	    Wss4joutinterceptor wssout = new Wss4joutinterceptor (props);
	    Client client = clientproxy.getclient (port);
	    Client.getoutinterceptors (). Add (Wssout);

In the client's Java file, add the following inner class

	public static class Passwordhandler implements CallbackHandler
	{public
	    void handle ( Javax.security.auth.callback.callback[] Callbacks {for
	        (int i = 0; i < callbacks.length; i++) {
	            WSPASSWORDCA Llback pc = (wspasswordcallback) callbacks[i];
	            Pc.setpassword ("password");}}

	


AXIS2 Plus certified head

For the above authentication header in Axis2 generated Java file How to add it (Axis2 automatically generated Java file does not automatically generate main test file, you need to write.) Xxxxproxy.java This is the class file for the call. However, these are not related to the addition of the certification head

Axis2 will produce a Xxxxportbindingstub.java file. The content of this is the actual method body.

Find the method body that we need to call:

Before the method call, add the following code:

Begin add for Header String auth_prefix = "Wsse";
         String Auth_ns = "Http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";        
            try{soapelement Wssecheaderelm = soapfactory.createelement ("Security", Auth_prefix, Auth_ns);          
            SoapElement Usernametokenelm = soapfactory.createelement ("UsernameToken", Auth_prefix, Auth_ns);         
            SoapElement Usernameelm = soapfactory.createelement ("Username", Auth_prefix, Auth_ns);                 
            SoapElement Passwdelm = soapfactory.createelement ("Password", Auth_prefix, Auth_ns); Passwdelm.setattribute ("Type", "http://docs.oasis-open.org/wss/2004/01/
          
	   Oasis-200401-wss-username-token-profile-1.0#passwordtext ");
	   Usernameelm.addtextnode ("vend_bmc01");
			
	  Passwdelm.addtextnode ("MediaTek");
	  Usernametokenelm.addchildelement (Usernameelm);
	  Usernametokenelm.addchildelement (Passwdelm); Wssecheaderelm.addchildelemENT (Usernametokenelm);
	  Soapheaderelement soapheaderelement = new Soapheaderelement (Wssecheaderelm);
	  Soapheaderelement.setmustunderstand (TRUE);  
        _call.addheader (soapheaderelement);
        }catch (Exception e) {e.printstacktrace ();
 //end add for Header = = = "Call" method Java.lang.Object _resp = _call.invoke (XXXX);

The principle is simple. Plus XML-like headers, _call_addheader

Plus, then trigger the method _call.invoke



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.