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);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值