下边就可以根据一个服务器类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);","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的公钥
解密服务器端返回来的信息。