An error occurred when verifying security for the message

An error occurred when verifying security for the message

I generated the java client using axis2, for a secured wsdl. I am facing following errors on testing the client
org.apache.axis2.AxisFault: An error occurred when verifying security for the message.
at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:512)
at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:370)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:416)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:228)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:163)
at us.tx.state.txdps.records.cch.SearchServiceStub.SearchBySid(SearchServiceStub.java:1015)
at us.tx.state.txdps.records.cch.Test.testStub(Test.java:40)
at us.tx.state.txdps.records.cch.Test.main(Test.java:143)

My client code is
view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
                        SearchServiceStub proxy = new SearchServiceStub();  
            Options opt = proxy._getServiceClient().getOptions();  
              
            opt.setUserName("abc");  
            opt.setPassword("123");  
            proxy._getServiceClient().setOptions(opt);  
                          
            SearchServiceStub.SearchBySid sid = new SearchServiceStub.SearchBySid();   
            sid.setSid("1111");  
            SearchServiceStub.SearchBySidResponse response = proxy.SearchBySid(sid); // Place where the error is thrown  
            System.out.println("Response: " + response.toString()); 
                        SearchServiceStub proxy = new SearchServiceStub();
   Options opt = proxy._getServiceClient().getOptions();
   
   opt.setUserName("abc");
   opt.setPassword("123");
   proxy._getServiceClient().setOptions(opt);
      
   SearchServiceStub.SearchBySid sid = new SearchServiceStub.SearchBySid();
   sid.setSid("1111");
   SearchServiceStub.SearchBySidResponse response = proxy.SearchBySid(sid); // Place where the error is thrown
   System.out.println("Response: " + response.toString());

Also tried
view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
                          
                        HttpTransportProperties.Authenticator basicAuthentication = new HttpTransportProperties.Authenticator();  
            basicAuthentication.setUsername("abc");  
            basicAuthentication.setPassword("123");  
            basicAuthentication.setPreemptiveAuthentication(true);  
 
            proxy._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE, basicAuthentication);  
            proxy._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.CHUNKED, "false");  
                        SearchServiceStub.SearchBySid sid = new SearchServiceStub.SearchBySid();   
            sid.setSid("1111");  
            SearchServiceStub.SearchBySidResponse response = proxy.SearchBySid(sid); // Place where the error is thrown  
            System.out.println("Response: " + response.toString()); 
                       
                        HttpTransportProperties.Authenticator basicAuthentication = new HttpTransportProperties.Authenticator();
   basicAuthentication.setUsername("abc");
   basicAuthentication.setPassword("123");
   basicAuthentication.setPreemptiveAuthentication(true);

   proxy._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE, basicAuthentication);
   proxy._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.CHUNKED, "false");
                        SearchServiceStub.SearchBySid sid = new SearchServiceStub.SearchBySid();
   sid.setSid("1111");
   SearchServiceStub.SearchBySidResponse response = proxy.SearchBySid(sid); // Place where the error is thrown
   System.out.println("Response: " + response.toString());

Also tried
view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
                        OMFactory omFactory = OMAbstractFactory.getOMFactory();  
            OMElement omSecurityElement = omFactory.createOMElement(new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security", "wsse"), null);  
            OMElement omusertoken = omFactory.createOMElement(new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "UsernameToken", "wsu"), null);  
            OMElement omuserName = omFactory.createOMElement(new QName("", "Username", "wsse"), null);  
            omuserName.setText("abc");  
 
            OMElement omPassword = omFactory.createOMElement(new QName("", "Password", "wsse"), null);  
            omPassword.addAttribute("Type","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText",null );  
            omPassword.setText("123");  
 
            omusertoken.addChild(omuserName);  
            omusertoken.addChild(omPassword);  
            omSecurityElement.addChild(omusertoken);  
            proxy._getServiceClient().addHeader(omSecurityElement); 
                        OMFactory omFactory = OMAbstractFactory.getOMFactory();
   OMElement omSecurityElement = omFactory.createOMElement(new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security", "wsse"), null);
   OMElement omusertoken = omFactory.createOMElement(new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "UsernameToken", "wsu"), null);
   OMElement omuserName = omFactory.createOMElement(new QName("", "Username", "wsse"), null);
   omuserName.setText("abc");

   OMElement omPassword = omFactory.createOMElement(new QName("", "Password", "wsse"), null);
   omPassword.addAttribute("Type","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText",null );
   omPassword.setText("123");

   omusertoken.addChild(omuserName);
   omusertoken.addChild(omPassword);
   omSecurityElement.addChild(omusertoken);
   proxy._getServiceClient().addHeader(omSecurityElement);
But with no success.

I tested the wsdl using SOAPUI and it works good.

Appreciate if someone could tell where I am going wrong.

You need to set the header QName as below

new QName("http:-//docs.-oasis-open.-org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "UsernameToken", "wsu")

Copyright © 2007-2012 www.chuibin.com Chuibin Copyright