---
title: "wss4j和axis2实现WS-Security（2）"
author: "conkeyn"
date: "2011-12-29T12:13:37Z"
tags: []
categories: ["编程语言"]
slug: "wss4j-axis2-ws-security-2"
draft: false
lastmod: "2011-12-29T12:13:37Z"
id: 1329318
url: "https://conkeyn.iteye.com/blog/1329318"
---

转自：[http://hi.baidu.com/zhaopengletter/blog/item/44a3bdfa9eda29244e4aea48.html](http://hi.baidu.com/zhaopengletter/blog/item/44a3bdfa9eda29244e4aea48.html)

 

**4.3   生成 .aar 服务包**

下边就可以根据一个服务器类 SimpleService 一个 service.xml 打包生成一个 wsc.aar 做为服务器端的程序 。 首先将这个 SimpleService 类打包 , 然后把 service.xml 放在打包后的 MATE-INFO 下边

这样服务器端程序 wsc.aar 就完成了 。 下边把 wsc.aar   copy 到 D:\\program\\Tomcat6.0\\webapps\\axis2\\WEB-INF\\services 目录下 ( 这里是以我的机器做为例子的 )

然后重启 tomcat 输入 [http://127.0.0.1:8080/axis2/services/listServices](http://127.0.0.1:8080/axis2/services/listServices)

就可以看到我们部署到服务器上的服务了。

**4.4   编写模拟第三方测试程序**

下面我写一个模拟第三方的程序调用 webservice 的一个例子

在包 com.neusoft.wss4j.rempart.demo.client 中的 Client

它的内容如下 :

package com.neusoft.wss4j.rempart.demo.client;

import java.io.Reader;

import java.io.StringReader;

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMFactory;

import org.apache.axiom.om.OMNamespace;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import org.apache.axis2.client.ServiceClient;

import org.apache.axis2.context.ConfigurationContext;

import org.apache.axis2.context.ConfigurationContextFactory;

import org.jdom.Document;

import org.jdom.Element;

import org.jdom.input.SAXBuilder;

public class Client

{

     public static void main(String\[] args) throws Exception

     {

         ConfigurationContext ctx = ConfigurationContextFactory

                 .createConfigurationContextFromFileSystem(

                         "D:/eclipse3.2/workspace/wsc/WebRoot/WEB-INF",             "D:/eclipse3.2/workspace/wsc/WebRoot/WEB-INF/conf/axis2.xml" );

         ServiceClient client = new ServiceClient(ctx, null);

         Options options = new Options();

         options.setAction(" urn:echo ");

         options.setTo(new EndpointReference(

                 " http://localhost:8080/wsc/services/wsc "));

          client.setOptions(options);

         OMElement response = client.sendReceive(getPayload(" (\*^\_\_^\*) 嘻嘻…… " ));

         OMElement element = response.getFirstElement();

         // 把返回的 OMElement 对象转换为 xml 数据

         SAXBuilder builder = new SAXBuilder();

         Reader in = new StringReader(element.toString());

         Document doc = null;

         try

         {

             doc = builder.build(in);

             Element Element = doc.getRootElement();

             String aa = Element.getTextTrim();

             System.out.println(aa);

          }

         catch (Exception e)

         {

             System.out.println(e.getMessage());

         }

     }

     private static OMElement getPayload(String value)

     {

         OMFactory factory = OMAbstractFactory.getOMFactory();

         OMNamespace ns = factory.createOMNamespace(

         " http://services.demo.rempart.wss4j.neusoft.com/xsd ","ns1");

         OMElement elem = factory.createOMElement(" echo ", ns);

         OMElement childElem = factory.createOMElement("param0", null);

         childElem.setText(value);

          elem.addChild(childElem);

         return elem;

     }

}

这个测试类就不多说了， 粉色 的部分是需要注意的地方。

还有个客户端的 axis2.xml 需要说明一下

他的主要内容如下只要把这部分粘贴到原来的axis2.xml即可 ：

&lt;module ref="rampart" /&gt;

&lt;parameter name="OutflowSecurity"&gt;  
       &lt;action&gt;  
         &lt;items&gt;Timestamp Signature&lt;/items&gt;  
         &lt;user&gt;client&lt;/user&gt;  
         &lt;signaturePropFile&gt;keys/client.properties&lt;/signaturePropFile&gt;  
         &lt;passwordCallbackClass&gt;com.neusoft.wss4j.rempart.demo.services.PWCBHandler&lt;/passwordCallbackClass&gt;  
         &lt;signatureKeyIdentifier&gt;DirectReference&lt;/signatureKeyIdentifier&gt;  
       &lt;/action&gt;  
     &lt;/parameter&gt;

     &lt;parameter name="InflowSecurity"&gt;  
       &lt;action&gt;  
         &lt;items&gt;Timestamp Signature&lt;/items&gt;  
         &lt;signaturePropFile&gt;keys/client.properties&lt;/signaturePropFile&gt;  
       &lt;/action&gt;  
     &lt;/parameter&gt;

注意一下 红色 的部分他是当客户端向服务器端发送数据时，首先访问 com.neusoft.wss4j.rempart.demo.services.PWCBHandler 这个类，得到访问权限和加密信息的文件密码，然后通过加密信息的文件密码和 keys/client.properties 文件找到加密需要的文件 client.jks 把信息加密发送给服务器端， 粉色 部分是通过 keys/client.properties 文件找到解密需要的文件 client.jks 来解密服务器端返回的加密信息。

Keys 文件下的 client.properties 内容如下：

org.apache.ws.security.crypto.provider = org.apache.ws.security.components.crypto.Merlin

org.apache.ws.security.crypto.merlin.keystore.type = jks

org.apache.ws.security.crypto.merlin.keystore.password = apache

org.apache.ws.security.crypto.merlin.file = keys/client.jks

 

五、总    结

**整理一下思路**

**1** **客户端发送消息给服务器端** : 如果客户端想请求服务器端首先读取客户端配置文件 axis2.xml 文件，得到访问的用户 &lt;user&gt;client&lt;/user&gt; 然后找到 com.neusoft.wss4j.rempart.demo.services.PWCBHandler 类，看用户是否有访问服务的权限，如果有则把 client.jks 文件的密码给用户 client ， client 通过密码在 axis2.xml 文件中找到 &lt;signaturePropFile&gt;keys/client.properties&lt;/signaturePropFile&gt; 找到 client.properties 文件，在 client.properties 文件中找到 client.jks 文件，使用该文件的 client 私钥 从而实现把传送的信息加密，然后把加密的信息发送到服务器端。

**2** **服务器端接收客户端发送来的消息** **:** 服务器端接收到消息，然后读取 service.xml 文件找到 &lt;signaturePropFile&gt;keys/service.properties&lt;/signaturePropFile&gt; 从而找到 service.properties 文件，通过该文件找到 service.jks 文件使用该文件的 client 的公钥

解密客户端传送来的信息。

**3** **服务器端返回信息给客户端** **:** 获得客户端传送过来的明文信息后，从 service.xml 文件

得到加密的用户 &lt;user&gt;service&lt;/user&gt; 通过

&lt;passwordCallbackClass&gt;

com.neusoft.wss4j.rempart.demo.services.PWCBHandler

&lt;/passwordCallbackClass&gt;

找到验证类 PWCBHandler 得到加密需要的 service.jks 的加密密码 apache

通过 &lt;signaturePropFile&gt;

     keys/service.properties

     &lt;/signaturePropFile&gt; 找到 service.properties 文件，通过该文件找到 service.jks 文件，通过该文件的 service 的私钥 把需要发送给客户端的信息加密。然后发送给客户端

**4** **客户端接收服务器端返回的消息** **:** 客户端端接收到消息，然后读取 axis2.xml 文件找到 &lt;signaturePropFile&gt;keys/service.properties&lt;/signaturePropFile&gt; 从而找到 client.properties 文件，通过该文件找到 client.jks 文件使用该文件的 service 的公钥

解密服务器端返回来的信息。