带有安全机制的axis2实现方式(二)

客户端

1、所需文件

client.jks,放在工程的src下   (与service.jks是一对,具体生成方法可上网查询)

 2、客户端生成方式

从apache官方网站上下载 axis2-1.4.1-bin.zip(如果所用的jar包是1.6的也行),然后解压缩,打开dos命令输入窗口(在运行中输入 cmd  ,然后回车即可),从窗口进入到解压缩的文件中,具体到bin目录下,例如:F:\axis2-1.4.1-bin\axis2-1.4.1\bin ,然后输入命令:

wsdl2java -uri http://localhost:8080/test/services/ReceiveMsgService?wsdl -p com.test.client -s -o stub

http://localhost:8080/axis2/services/SimpleService?wsdl 为需要调用的接口地址

com.test.client 为生成的代码所在的包结构
stub 为代码所在文件夹

通过该命令生成的代码保存在 bin 文件夹下,新建一个工程,将代码放入即可;生成的客户端的类为ReceiveMsgServerStub,里面包含了输入输出类,需要调用的方法等。

 如果该命令无法生成代码,提示jdk不可用,则表示本机的没有配置java环境变量,需要重新配置环境变量。

3、调用客户端的代码

      import java.rmi.RemoteException;
      import java.util.Properties;

      import org.apache.axis2.AxisFault;
      import org.apache.axis2.client.ServiceClient;
      import org.apache.axis2.context.ConfigurationContext;
      import org.apache.axis2.context.ConfigurationContextFactory;
      import org.apache.axis2.description.PolicyInclude;
      import org.apache.neethi.Policy;
      import org.apache.rampart.policy.model.CryptoConfig;
      import org.apache.rampart.policy.model.RampartConfig;

      public class ClientTest {

         public static void testMsg() throws AxisFault {
              ConfigurationContext ctx = ConfigurationContextFactory .createConfigurationContextFromFileSystem("rampart", null);

              ReceiveMsgServerStub stub = new ReceiveMsgServerStub(ctx, "http://localhost:8080/test/services/receiveMsgServer");

              ServiceClient sc = stub._getServiceClient();
              sc.engageModule("rampart");

              RampartConfig rampartConfig = new RampartConfig();
              rampartConfig.setUser("client");
              rampartConfig.setPwCbClass("client.PWCBHandler");

             CryptoConfig sigCrypto = new CryptoConfig();
             sigCrypto.setProvider("org.apache.ws.security.components.crypto.Merlin");

             Properties props = new Properties();
             props.setProperty("org.apache.ws.security.crypto.merlin.keystore.type", "JKS");
             props.setProperty("org.apache.ws.security.crypto.merlin.file", "client.jks");
             props.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", "clientPW");

             sigCrypto.setProp(props);

             rampartConfig.setSigCryptoConfig(sigCrypto);
             Policy policy = new Policy();
             policy.addAssertion(rampartConfig);

             sc.getAxisService().getPolicyInclude().addPolicyElement(PolicyInclude.AXIS_SERVICE_POLICY, policy);

             ReceiveMsgServerStub.Message message = new ReceiveMsgServerStub.Message();
             message.setMsgId("001");
             message.setMsgName("ZHANGRR");
             ReceiveMsgServerStub.ReceiveMessage messageRequest = new ReceiveMsgServerStub.ReceiveMessage();
             messageRequest.setParam0(message);

             ReceiveMsgServerStub.ReceiveMessageResponse response;
             try {
                      response = stub.receiveMessage(messageRequest);
                      System.out.println(response.get_return());
              } catch (RemoteException e) {
                      e.printStackTrace();
              }
        }

     }

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值