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());
}
}
}