当前位置 : 主页 > 网页制作 > Nodejs >

webservice客户端oauth2 Bearer 认证

来源:互联网 收集:自由互联 发布时间:2021-06-24
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和测试用例,打开In

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);
}
网友评论