一个简单的示例
这个计算器服务通过执行EJB来备份。当然,你可以有另外的选择,如选择JavaBeans等。下面展示的是EJB的界面:
package com.dev.ws.security.ejb;
public interface Calculator extends javax.ejb.EJBObject {
public float multiply(float a, float b) throws java.rmi.RemoteException;
}
The EJB bean simply implements the multiply method as follows:
package com.dev.ws.security.ejb;
public class CalculatorBean implements javax.ejb.SessionBean {
...
public float multiply(float a, float b){
return a*b;
}
} |
此服务的WSDL显示如下。注意,它使用了一个标准的SOAP/HTTP绑定,并且使这个服务端点在URL中是可用的。
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions ...>
<wsdl:types>
...
<wsdl:portType name="Calculator">
<wsdl:operation name="multiply">
<wsdl:input message="intf:multiplyRequest"name="multiplyRequest"/>
<wsdl:output message="intf:multiplyResponse" name="multiplyResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="CalculatorSoapBinding" type="intf:Calculator">
<wsaw:UsingAddressing wsdl:required="false"
xmlns:wsaw="http://www.w3.org/2006/02/addressing/wsdl"/>
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="multiply">
<wsdlsoap:operation soapAction="multiply"/>
...
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="CalculatorService">
<wsdl:port binding="intf:CalculatorSoapBinding" name="Calculator">
<wsdlsoap:address
location="http://localhost:9080/WSUTSigEncRouterWeb/services/Calculator"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions> |
在此示例中,使用Xfire
stub生成器从WSDL创建CalculatorServiceClient。下面的代码使用这个stub来调用web服务的乘法方法,并将参数5和7传递给它:
package com.dev.ws.client.calculator.driver;
...
public class WSClientUTSigEnc {
// Non-SSL URL
public static String UT_ENDPOINT
= "http://localhost:9080/WSUTSigEncRouterWeb/services/Calculator";
public static void main(String[] args) throws MalformedURLException {
CalculatorServiceClient sc = new CalculatorServiceClient();
Calculator calc = sc.getCalculator(UT_ENDPOINT);
float a = 5f;
float b = 7f;
System.out.println(a + " * " + b + " = " + calc.multiply(a,b));
}
After starting the server and running the client, you can use a TCP/IP monitor at port
9080 to observe the messages that the client sends to the web service. You should observe
a SOAP message that looks like this:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<multiply xmlns="http://ejb.security.ws.dev.com">
<a>5.0</a>
<b>7.0</b>
</multiply>
</soap:Body>
</soap:Envelope>
The client outputs the expected result after getting the SOAP response:
5.0 * 7.0 = 35.0 |
上面的SOAP消息只是简单地将方法请求包含进来,并没有安全的报头。为了在SOAP消息的级别上支持加密,应用程序服务器和客户端必须加以配置来支持XML加密。客户端从一个X509的证书使用一个公钥来对SOAP消息加密,而服务器相对应地使用私钥对此消息解密。一对公/私钥必须由给客户端的公钥和给服务器的私钥来生成。(可参考如下的XFire文章来查看如何创建这对密钥的相关细节信息。应用程序服务器的配置与服务器息息相关,因此请参考你的服务器的相关文档资料,以得到XML加密的用法说明。你可以在这里找到本文的WebSphere配置的相关指导。)
(下一页)
网友评论