cxf实现的ws_client(带安全验证)

开发环境: apache-cxf-3.0.9 + jdk6.0

客户端java代码生成:

wsdl2java + wsdlfile

问题描述

jdk6最高支持ws2.1规范版本,在jdk6下生成wsdl2java后要加上-frontend jaxws21
java类中含有中文注释,wsdl2java后要加上-encoding utf-8

客户端代码
package com.ws.client;

import java.util.HashMap;
import java.util.Map;

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

import com.ws.service.IWSCall;
import com.ws.service.WSCallImplService;

public class WSCli {

	private static void client(){
		
		//获取服务接口
		WSCallImplService factory = new WSCallImplService();
		IWSCall wsCall = factory.getWSCallImplPort();		
		//配置验证信息
		Client client = ClientProxy.getClient(wsCall);
        Map outProps = new HashMap();
		outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
		outProps.put(WSHandlerConstants.USER, "admin");
		outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
		outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, ClientPwdCallback.class.getName());
		client.getOutInterceptors().add(new WSS4JOutInterceptor(outProps));  
		//调用服务
		String result = wsCall.call("ketty");
		System.out.println(result);
	}
	
	public static void main(String[] args) {
		client();
	}
}

客户端密码回调

package com.ws.client;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;

import org.apache.wss4j.common.ext.WSPasswordCallback;

public class ClientPwdCallback implements CallbackHandler 
{
	private Map manager = new HashMap();

	public ClientPwdCallback()
	{
		manager.put("admin", "123");
		manager.put("test", "test");
	}
	
	public void handle(Callback[] callbacks) throws IOException,
			UnsupportedCallbackException 
	{
		for(int i=0;i
依赖jar包

cxf实现的ws_client(带安全验证)_第1张图片


你可能感兴趣的:(webservice)