3.1开发一个基本的webservice
(1)服务端代码:
HelloServiceImpl.java
package server.service; public class HelloServiceImpl { public String hello(String s) { System.out.println("我是服务端......"); System.out.println("方法的入参为:"+s); return "hello," + s; } }(2)服务端wsdd文件的配置
<?xml version="1.0" encoding="UTF-8"?> <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <globalConfiguration> <parameter name="sendMultiRefs" value="true" /> <parameter name="disablePrettyXML" value="true" /> <parameter name="adminPassword" value="admin" /> <parameter name="attachments.Directory" value="D:\tomcat5\webapps\WebService\WEB-INF\attachments" /> <parameter name="dotNetSoapEncFix" value="true" /> <parameter name="enableNamespacePrefixOptimization" value="false" /> <parameter name="sendXMLDeclaration" value="true" /> <parameter name="sendXsiTypes" value="true" /> <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl" /> <requestFlow> <handler type="java:org.apache.axis.handlers.JWSHandler"> <parameter name="scope" value="session" /> </handler> <handler type="java:org.apache.axis.handlers.JWSHandler"> <parameter name="scope" value="request" /> <parameter name="extension" value=".jwr" /> </handler> </requestFlow> </globalConfiguration> <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder" /> <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper" /> <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" /> <service name="AdminService" provider="java:MSG"> <parameter name="allowedMethods" value="AdminService" /> <parameter name="enableRemoteAdmin" value="false" /> <parameter name="className" value="org.apache.axis.utils.Admin" /> <namespace>http://xml.apache.org/axis/wsdd/</namespace> </service> <service name="Version" provider="java:RPC"> <parameter name="allowedMethods" value="getVersion" /> <parameter name="className" value="org.apache.axis.Version" /> </service> <transport name="http"> <requestFlow> <handler type="URLMapper" /> <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler" /> </requestFlow> <parameter name="qs:list" value="org.apache.axis.transport.http.QSListHandler" /> <parameter name="qs:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler" /> <parameter name="qs.list" value="org.apache.axis.transport.http.QSListHandler" /> <parameter name="qs.method" value="org.apache.axis.transport.http.QSMethodHandler" /> <parameter name="qs:method" value="org.apache.axis.transport.http.QSMethodHandler" /> <parameter name="qs.wsdl" value="org.apache.axis.transport.http.QSWSDLHandler" /> </transport> <transport name="local"> <responseFlow> <handler type="LocalResponder" /> </responseFlow> </transport> <!-- 配置自己的服务 --> <service name="HelloService" provider="java:RPC"> <parameter name="allowedMethods" value="*" /> <parameter name="className" value="server.service.HelloServiceImpl" /> </service> </deployment>(3)客户端代码
package client; import java.net.URL; import javax.xml.rpc.ParameterMode; import org.apache.axis.client.Call; import org.apache.axis.encoding.XMLType; public class Test { public static void main(String args[]) throws Exception { webservice_user(); } public static void webservice_user() throws Exception { // 1.创建service对象,通过axis自带的类创建 org.apache.axis.client.Service service = new org.apache.axis.client.Service(); // 2.创建url对象 String wsdlUrl = "http://localhost:8080/WebService06_Security/services/HelloService?wsdl";// 请求服务的URL URL url = new URL(wsdlUrl);// 通过URL类的构造方法传入wsdlUrl地址创建URL对象 // 2.创建服务方法的调用者对象call,设置call对象的属性 Call call = (Call) service.createCall(); call.setTargetEndpointAddress(url);// 给call对象设置请求的URL属性 String serviceName = "hello";// webservice的方法名 call.setOperationName(serviceName);// 给call对象设置调用方法名属性 call.addParameter("s", XMLType.XSD_STRING, ParameterMode.IN);// 给call对象设置方法的参数名、参数类型、参数模式 call.setReturnType(XMLType.SOAP_STRING);// 设置调用方法的返回值类型 // call.setTimeout(new Integer(200));//设置超时限制 // 4.通过invoke方法调用webservice String str = new String("pantp"); String dept = (String) call.invoke(new Object[] { str });// 调用服务方法 // 5.打印返回结果 System.out.println("我是客户端......."); System.out.println(dept); } }
运行Test.java类:
(1)客户端日志
(2)服务端日志
(1)增加users.lst文件
在工程的WEB-INF目录下新建文件users.lst,文件内容如下:
(2)在 server-config.wsdd文件中增加验证的配置
修改后完整的文件如下:
<?xml version="1.0" encoding="UTF-8"?> <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <globalConfiguration> <parameter name="sendMultiRefs" value="true" /> <parameter name="disablePrettyXML" value="true" /> <parameter name="adminPassword" value="admin" /> <parameter name="attachments.Directory" value="D:\tomcat5\webapps\WebService\WEB-INF\attachments" /> <parameter name="dotNetSoapEncFix" value="true" /> <parameter name="enableNamespacePrefixOptimization" value="false" /> <parameter name="sendXMLDeclaration" value="true" /> <parameter name="sendXsiTypes" value="true" /> <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl" /> <requestFlow> <handler type="java:org.apache.axis.handlers.JWSHandler"> <parameter name="scope" value="session" /> </handler> <handler type="java:org.apache.axis.handlers.JWSHandler"> <parameter name="scope" value="request" /> <parameter name="extension" value=".jwr" /> </handler> </requestFlow> </globalConfiguration> <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder" /> <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper" /> <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" /> <service name="AdminService" provider="java:MSG"> <parameter name="allowedMethods" value="AdminService" /> <parameter name="enableRemoteAdmin" value="false" /> <parameter name="className" value="org.apache.axis.utils.Admin" /> <namespace>http://xml.apache.org/axis/wsdd/</namespace> </service> <service name="Version" provider="java:RPC"> <parameter name="allowedMethods" value="getVersion" /> <parameter name="className" value="org.apache.axis.Version" /> </service> <transport name="http"> <requestFlow> <handler type="URLMapper" /> <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler" /> </requestFlow> <parameter name="qs:list" value="org.apache.axis.transport.http.QSListHandler" /> <parameter name="qs:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler" /> <parameter name="qs.list" value="org.apache.axis.transport.http.QSListHandler" /> <parameter name="qs.method" value="org.apache.axis.transport.http.QSMethodHandler" /> <parameter name="qs:method" value="org.apache.axis.transport.http.QSMethodHandler" /> <parameter name="qs.wsdl" value="org.apache.axis.transport.http.QSWSDLHandler" /> </transport> <transport name="local"> <responseFlow> <handler type="LocalResponder" /> </responseFlow> </transport> <!-- 配置自己的服务 --> <service name="HelloService" provider="java:RPC"> <parameter name="allowedMethods" value="*" /> <parameter name="className" value="server.service.HelloServiceImpl" /> <!-- ===================采用webservice的users.lst的安全机制增加的配置部分 --> <!-- 采用axis默认的handler来进行访问权限的控制 --> <requestFlow> <handler name="SimpleAuthenticationHandler" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" /> </requestFlow> <!-- ===================采用webservice的users.lst的安全机制增加的配置部分 --> </service> </deployment>
Test.java
package client; import java.net.URL; import javax.xml.rpc.ParameterMode; import org.apache.axis.client.Call; import org.apache.axis.encoding.XMLType; public class Test { public static void main(String args[]) throws Exception { webservice_user(); } public static void webservice_user() throws Exception { // 1.创建service对象,通过axis自带的类创建 org.apache.axis.client.Service service = new org.apache.axis.client.Service(); // 2.创建url对象 String wsdlUrl = "http://localhost:8080/WebService06_Security/services/HelloService?wsdl";// 请求服务的URL URL url = new URL(wsdlUrl);// 通过URL类的构造方法传入wsdlUrl地址创建URL对象 // 2.创建服务方法的调用者对象call,设置call对象的属性 Call call = (Call) service.createCall(); call.setTargetEndpointAddress(url);// 给call对象设置请求的URL属性 String serviceName = "hello";// webservice的方法名 call.setOperationName(serviceName);// 给call对象设置调用方法名属性 call.addParameter("s", XMLType.XSD_STRING, ParameterMode.IN);// 给call对象设置方法的参数名、参数类型、参数模式 call.setReturnType(XMLType.SOAP_STRING);// 设置调用方法的返回值类型 // call.setTimeout(new Integer(200));//设置超时限制 //-----------------------------采用webservice的users.lst的安全机制增加的------------------------------------- //此处的用户名和密码对应WEB-INF目录下users.lst文件中的用户名和密码 call.getMessageContext().setUsername("pantp"); call.getMessageContext().setPassword("123456"); //-----------------------------采用webservice的users.lst的安全机制增加的------------------------------------- // 4.通过invoke方法调用webservice String str = new String("pantp"); String dept = (String) call.invoke(new Object[] { str });// 调用服务方法 // 5.打印返回结果 System.out.println("我是客户端......."); System.out.println(dept); } }
(1)输入正确的用户名和密码
客户端日志:
服务端日志:
(2)输入错误的用户名或密码
客户端日志:
服务端日志:
后台没有任何日志输出;
总结:
开发通过webservice自带的验证机制实现步骤如下:
1.先不考虑安全机制的问题,开发一个需要的webservice服务和客户端,并测试通过;
2.在WEB-INF目录下新建一个文件users.lst,文件名必须一模一样,然后在文件中加入允许的用户名和密码;
3.在wsdd配置文件中对应的服务中加入SimpleAuthenticationHandler的配置;
4.在客户端访问时增加用户名和密码的设置;