webservice客户端oauth2 Bearer 认证

1. axis2方式

     a. 生成client端代码:

      wsdl2java -uri F:\nantian\PSS\PSS_1.0_WSDL_Files\PSS_1.0_WSDL_Files\WSDLs\InventoryService.wsdl -p -g -t -s -o F:\temp\2016-4-15\axis2


这样就能生成Inventory的client和测试用例,打开InventoryServiceTest测试,这个地方要部分修改

InventoryServiceStub stub = new InventoryServiceStub();
//认证开始-----------
org.apache.axis2.client.ServiceClient client = null;
org.apache.axis2.client.Options option = null;
client = stub._getServiceClient();
option = client.getOptions();
List<org.apache.commons.httpclient.Header> list = new ArrayList<org.apache.commons.httpclient.Header>();
org.apache.commons.httpclient.Header header = new org.apache.commons.httpclient.Header();
header.setName("Authorization");
header.setValue("Bearer " + "t4wmagv86n8v8bt3y2k9kxvj");
list.add(header);
option.setProperty(
		org.apache.axis2.transport.http.HTTPConstants.HTTP_HEADERS,
		list);
//认证结束------------
InventoryServiceStub.CustomerInventoryRequest customerInventoryRequest14 = (InventoryServiceStub.CustomerInventoryRequest) 
		getTestObject(InventoryServiceStub.CustomerInventoryRequest.class);
// TODO : Fill in the customerInventoryRequest14 here
//传参数开始-------------
CustomerIdsRequest ids = new CustomerIdsRequest();
ids.setCustomerIds(new String[] {});
customerInventoryRequest14.setCustomerInventoryRequest(ids);</span>
//传参数结束-------------
CustomersInventoryResponseE response = stub
		.getCustomersInventoryIds(customerInventoryRequest14);
System.out.println(response);


以上红色部分就是生成的代码基础上新增的,这就完成Bearer认证,如果是基本认证就换成Basic就可以


2. CXF方式

    a. 生成client端代码:

    wsdl2java -p com.client -d F:\nantian\workspace\3.7.2\cxf\src -client       

         F:\nantian\PSS\PSS_1.0_WSDL_Files\PSS_1.0_WSDL_Files\WSDLs\InventoryService.wsdl

生成后如下:



修改以http_Client结尾的java文件:

InventoryServicePortType port = ss.getInventoryServiceSOAP11PortHttp();
//认证开始
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Authorization",
		Arrays.asList("Bearer g3ssxwhud5385bzjt37us946"));
Client client = ClientProxy.getClient(port);
client.getRequestContext().put(Message.PROTOCOL_HEADERS, headers);
//认证结束
{
	System.out.println("Invoking getCustomersInventoryIds...");
	com.cisco.inventoryservice.CustomerIdsRequest _getCustomersInventoryIds_parameters = null;
	_getCustomersInventoryIds_parameters = new CustomerIdsRequest();
	_getCustomersInventoryIds_parameters.getCustomerIds();//调用add增加相应参数
	com.cisco.inventoryservice.CustomersInventoryResponse _getCustomersInventoryIds__return = port
			.getCustomersInventoryIds(_getCustomersInventoryIds_parameters);
	System.out.println("getCustomersInventoryIds.result="
			+ _getCustomersInventoryIds__return);
}

add by 2017-10-31

可以不生成client,用JaxWsDynamicClientFactory动态生成客户端代理,只要知道wsdl的地址:

  1. public static void main(String[] args) {  
  2.         JaxWsProxyFactoryBean bean = new JaxWsProxyFactoryBean();  
  3.         bean.setServiceClass(HelloWorldService.class);  
  4.         bean.setAddress("http://localhost:9090/helloWorldService");  
  5.         HelloWorldService helloWorldService = (HelloWorldService)bean.create();  
  6.         String result = helloWorldService.sayHello("Kevin");  
  7.         System.out.println(result);  
  8.     } 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值