Cxf生成web service加入安全性验证

Cxf.Security.Wsse4j.Demo

Cxf生成web service加入安全性验证。(参见:http://cxf.apache.org/docs/ws-security.html)

官网代码没有完善,完整代码如下:(SRC)
package com.zte.eip.common.test;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;

import com.zte.eip.common.gl.DemoSrv.DemoSrvOutputCollection;
import com.zte.eip.common.gl.DemoSrv.DemoSrvOutputItem;
import com.zte.eip.common.gl.DemoSrv.DemoSrvRequest;
import com.zte.eip.common.gl.DemoSrv.DemoSrvResponse;
import com.zte.eip.common.gl.InquiryDemoSrv.DemoSrv;
import com.zte.eip.common.gl.InquiryDemoSrv.DemoSrv_Service;
import com.zte.eip.common.wsse.ClientConstants;
import com.zte.eip.common.wsse.ClientPasswordHandler;

/**
 * This class was generated by Apache CXF 2.1.3 Thu Sep 03 09:47:26 CST 2009
 * Generated source version: 2.1.3
 *
 */

/**
 * @author
 *
 */
public final class TestClient {

 private static final QName SERVICE_NAME = new QName(
   "http://eip.zte.com/common/gl/InquiryDemoSrv", "InquiryDemoSrv");

 private TestClient() {
 }

 @SuppressWarnings("unchecked")
 public static void main(String args[]) throws Exception {
  URL wsdlURL = DemoSrv_Service.WSDL_LOCATION;
  if (args.length > 0) {
   File wsdlFile = new File(args[0]);
   try {
    if (wsdlFile.exists()) {
     wsdlURL = wsdlFile.toURI().toURL();
    } else {
     wsdlURL = new URL(args[0]);
    }
   } catch (MalformedURLException e) {
    e.printStackTrace();
   }
  }

  DemoSrv_Service ss = new DemoSrv_Service(wsdlURL, SERVICE_NAME);
  DemoSrv port = ss.getDemoSrvPort();

  Client client = ClientProxy.getClient(port);
  Endpoint cxfEndpoint = client.getEndpoint();
  Map outProps = new HashMap();
  outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
  outProps.put(WSHandlerConstants.USER, ClientConstants.USERNAME);
  outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
  outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, ClientPasswordHandler.class.getName());
  WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
  cxfEndpoint.getOutInterceptors().add(wssOut);

  DemoSrvRequest request = new DemoSrvRequest();
  DemoSrvResponse response = port.process(request);
  out(response);

 }

 public static void out(DemoSrvResponse response) {
  DemoSrvOutputCollection collection = response.getDemoSrvOutputCollection();
  List<DemoSrvOutputItem> items = collection.getDemoSrvOutputItem();
  for (DemoSrvOutputItem item : items) {
   System.out.println(item.getORGNAME());
  }
 }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值