java webservice用户名密码,使用AXIS2将用户/密码添加到SOAPHeader以进行WebService客户端调用...

Please help: I am trying to call a WebService from SOAPUI and I notice that the service requires username and password which I am providing through the request parameters. I notice that raw XML contains user/password snippet added to SOAPHeader. The snippet is as below:

testusertestpassword&RYadQak91mr7dB+5hyt8yw==2011-10-24T20:13:43.039Z

Now the same thing I want to achieve by adding user/password details as in the below code: code snippet is:

org.tempuri.myService.MyServiceStub stub = new org.tempuri.myService.MyServiceStub();

ServiceClient sc = stub._getServiceClient();

HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();

auth.setUsername("testuser");

auth.setPassword("password$");

sc.getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE,auth);

org.tempuri.myService.MyServiceDocument myService4 = (org.tempuri.myService.MyServiceDocument)getTestObject(org.tempuri.myService.MyServiceDocument.class);

MyService lval = MyService4.addNewMyService();

MyServiceParameters lvParams = lval.addNewParameters();

lvParams.setA("24");

lvParams.setB("10");

lval.setParameters(lvParams);

myService4.setMyService(lval);

But I get following Axis fault exception, Need help in the mistake I am doing with the above code. Axis Fault Exception details:

org.apache.axis2.AxisFault: Exception occurred while executing service 'MyService'.

at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:531)

at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:375)

at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:421)

at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)

at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)

at org.tempuri.myService.MyServiceStub.myService(MyServiceStub.java:182)

at org.tempuri.myService.MyServiceTest.main(MyServiceTest.java:55)

MyServiceResponseDocument lvdoc = stub.myService(myService4);

解决方案

I resolved the issue myself, this snippet might help some one who want to add additional parameters (atleast this works for me):

The code snippet is as below:

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("myusername");

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("mypassword");

omusertoken.addChild(omuserName);

omusertoken.addChild(omPassword);

omSecurityElement.addChild(omusertoken);

stub._getServiceClient().addHeader(omSecurityElement);

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
调用 Java WebService 时也需要提供认证信息。下面是一个使用 Java 调用 WebService 的示例代码,包含了认证信息: ``` import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.BindingProvider; import com.example.webservice.MyService; import com.example.webservice.MyServicePortType; public class MyWebServiceClient { public static void main(String[] args) { try { // WebService 的 URL URL url = new URL("https://www.example.com/webservice?wsdl"); // WebService 的 QName QName qname = new QName("http://example.com/webservice", "MyService"); // 创建 WebService 客户 MyService service = new MyService(url, qname); MyServicePortType port = service.getMyServicePort(); // WebService 的认证信息 String username = "your_username"; String password = "your_password"; // 设置认证信息 BindingProvider bindingProvider = (BindingProvider) port; bindingProvider.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username); bindingProvider.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password); // 调用 WebService String result = port.getData("value1", "value2"); // 处理返回的数据 System.out.println("Result: " + result); } catch (Exception e) { e.printStackTrace(); } } } ``` 在这个示例中,我们使用Javajavax.xml.ws 包提供的类来创建 WebService 客户。我们还将用户名密码作为参数传递给 BindingProvider.getRequestContext().put() 方法,以实现身份认证。最后,我们调用 WebService 的方法并处理返回的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值