`
conkeyn
  • 浏览: 1507982 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

wss4j和axis2实现WS-Security(2)

 
阅读更多

转自: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

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

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即可

<module ref="rampart" />

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

     <parameter name="InflowSecurity">
       <action>
         <items>Timestamp Signature</items>
         <signaturePropFile>keys/client.properties</signaturePropFile>
       </action>
     </parameter>

注意一下 红色 的部分他是当客户端向服务器端发送数据时,首先访问 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 文件,得到访问的用户 <user>client</user> 然后找到 com.neusoft.wss4j.rempart.demo.services.PWCBHandler 类,看用户是否有访问服务的权限,如果有则把 client.jks 文件的密码给用户 client client 通过密码在 axis2.xml 文件中找到 <signaturePropFile>keys/client.properties</signaturePropFile> 找到 client.properties 文件,在 client.properties 文件中找到 client.jks 文件,使用该文件的 client 私钥 从而实现把传送的信息加密,然后把加密的信息发送到服务器端。

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

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

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

得到加密的用户 <user>service</user> 通过

<passwordCallbackClass>

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

</passwordCallbackClass>

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

通过 <signaturePropFile>

     keys/service.properties

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

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

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics