Java calls Web Service Add Authentication Header (Soapenv:header)

Source: Internet
Author: User
Tags soapui wsdl

Preface

Sometimes invoking Web service will appear

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

Such a mistake.

As a result of the SOAPUI call, the following return appears


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

For example, you may need to add the following authentication 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, the specific username and pass are also provided by the server)

When the Sopaui is called, the call succeeds, plus a similar.


In the SOAPUI call, can be done in the above way. After converting WSDL to Java, how do you add the header information for authentication?


cxf Plus Authentication Header

(Above the authentication head, more close to the CXF method of invocation.) )

If you are using the code for the CXF production client.

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

Add the following code to the _client call: (before the code of the method call)

    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 Authentication Header

For the above authentication header in the Java file generated by Axis2 how to add it (Axis2 automatically generated Java files will not automatically produce the main test file, you need to write your own. Xxxxproxy.java This is the class file for the call. However, these are not related to the addition of the certification head)

The 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. Add XML-like headers, _call_addheader

Plus, then trigger the method _call.invoke



Java calls Web Service Add Authentication Header (Soapenv:header)

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.