CXF使用X509证书的WS-Security进行验证

client端
package com.tongtech.ti.cxf.demo.security.X509.client;

/**
 * Please modify this class to meet your needs
 * This class is not complete
 */

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.xml.namespace.QName;

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

import com.tongtech.ti.cxf.demo.security.service.ISecuriyDemo;
import com.tongtech.ti.cxf.demo.security.service.ISecuriyService;

/**
 * This class was generated by Apache CXF 2.4.0-SNAPSHOT Tue Oct 26 16:45:43 CST
 * 2010 Generated source version: 2.4.0-SNAPSHOT
 * 
 */

public final class Client {

	private static final QName SERVICE_NAME = new QName(
			"http://demo.ti.tongtech.com/security/", "ISecuriyService");

	private Client() {
	}

	public static void main(String args[]) throws Exception {
		URL wsdlURL = ISecuriyService.WSDL_LOCATION;

		ISecuriyService ss = new ISecuriyService(wsdlURL, SERVICE_NAME);
		ISecuriyDemo port = ss.getISecuriyServicePort();
		org.apache.cxf.endpoint.Client client = ClientProxy.getClient(port);
		Endpoint cxfEp = client.getEndpoint();

		// Clint Out
		Map<String, Object> outProp = new HashMap<String, Object>();
		outProp.put(WSHandlerConstants.ACTION, WSHandlerConstants.TIMESTAMP
				+ " " + WSHandlerConstants.SIGNATURE + " "
				+ WSHandlerConstants.ENCRYPT);
		outProp.put(WSHandlerConstants.USER, "clientprivatekey");
		outProp.put(WSHandlerConstants.ENCRYPTION_USER, "serverpublickey");
		outProp.put(WSHandlerConstants.PW_CALLBACK_CLASS,
				UTPasswordClientCallBack.class.getName());
		outProp.put(WSHandlerConstants.SIG_PROP_FILE,
				"cert/Client_Sign.properties");
		outProp.put(WSHandlerConstants.ENC_PROP_FILE,
				"cert/Client_Encrypt.properties");
		cxfEp.getOutInterceptors().add(new WSS4JOutInterceptor(outProp));

		// Client In(Return)
		Map<String, Object> inProp = new HashMap<String, Object>();
		inProp.put(WSHandlerConstants.ACTION, WSHandlerConstants.TIMESTAMP
				+ " " + WSHandlerConstants.SIGNATURE + " "
				+ WSHandlerConstants.ENCRYPT);
		inProp.put(WSHandlerConstants.PW_CALLBACK_CLASS,
				UTPasswordClientCallBack.class.getName());
		inProp.put(WSHandlerConstants.DEC_PROP_FILE,
				"cert/Client_Sign.properties");
		inProp.put(WSHandlerConstants.SIG_PROP_FILE,
				"cert/Client_Encrypt.properties");
		cxfEp.getInInterceptors().add(new WSS4JInInterceptor(inProp));

		{
			System.out.println("Invoking input...");
			java.lang.String _input_in = "Input Value!";
			java.lang.String _input__return = port.input(_input_in);
			System.out.println("input.result=" + _input__return);
		}

		System.exit(0);
	}

}


server端
package com.tongtech.ti.cxf.demo.security.X509.server;

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

import javax.xml.ws.Endpoint;

import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.ws.security.handler.WSHandlerConstants;

import com.tongtech.ti.cxf.demo.security.service.ISecuriyDemoImpl;

/**
 * This class was generated by Apache CXF 2.4.0-SNAPSHOT Tue Oct 26 16:45:43 CST
 * 2010 Generated source version: 2.4.0-SNAPSHOT
 * 
 */

public class Server {

	protected Server() throws Exception {
		System.out.println("Starting Server");
		Object implementor = new ISecuriyDemoImpl();
		String address = "http://localhost:8080/sec";
		EndpointImpl ep = (EndpointImpl) Endpoint.publish(address, implementor);
		org.apache.cxf.endpoint.Endpoint cxfEp = ep.getServer().getEndpoint();

		// ///////////////////////////////////////////////////////////////

		Map<String, Object> inProp = new HashMap<String, Object>();
		inProp.put(WSHandlerConstants.ACTION, WSHandlerConstants.TIMESTAMP
				+ " " + WSHandlerConstants.SIGNATURE + " "
				+ WSHandlerConstants.ENCRYPT);
		inProp.put(WSHandlerConstants.PW_CALLBACK_CLASS,
				UTPasswordServerCallBack.class.getName());
		inProp.put(WSHandlerConstants.SIG_PROP_FILE,
				"cert/Server_SignVerf.properties");
		inProp.put(WSHandlerConstants.DEC_PROP_FILE,
				"cert/Server_Decrypt.properties");
		cxfEp.getInInterceptors().add(new WSS4JInInterceptor(inProp));

		// /////////////////////////////////////////////////////////////////

		Map<String, Object> outProp = new HashMap<String, Object>();
		outProp.put(WSHandlerConstants.ACTION, WSHandlerConstants.TIMESTAMP
				+ " " + WSHandlerConstants.SIGNATURE + " "
				+ WSHandlerConstants.ENCRYPT);
		outProp.put(WSHandlerConstants.USER, "serverprivatekey");
		outProp.put(WSHandlerConstants.PW_CALLBACK_CLASS,
				UTPasswordServerCallBack.class.getName());
		outProp.put(WSHandlerConstants.ENCRYPTION_USER, "clientpublickey");
		outProp.put(WSHandlerConstants.SIG_PROP_FILE,
				"cert/Server_Decrypt.properties");// 私钥
		outProp.put(WSHandlerConstants.ENC_PROP_FILE,
				"cert/Server_SignVerf.properties");// 公钥
		cxfEp.getOutInterceptors().add(new WSS4JOutInterceptor(outProp));
	}

	public static void main(String args[]) throws Exception {
		new Server();
		System.out.println("Server ready...");

		Thread.sleep(60 * 60 * 1000);
		System.out.println("Server exiting");
		System.exit(0);
	}
}

你可能感兴趣的:(ws-security)