首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

XML_RPC用户印证实例(高级)

2012-12-20 
XML_RPC用户验证实例(高级)Server端:创建一个web工程,工程名XML_RPC,导入除client以外的4个jar包业务逻辑

XML_RPC用户验证实例(高级)
Server端:
创建一个web工程,工程名XML_RPC,导入除client以外的4个jar包

业务逻辑类:

package demo.xmlrpc.action;public class Calculator {    public int add(int i1, int i2) {            return i1 + i2;    }    public int subtract(int i1, int i2) {            return i1 - i2;    }}


用户验证逻辑类:
package demo.xmlrpc.authentication;import org.apache.xmlrpc.XmlRpcException;import org.apache.xmlrpc.XmlRpcRequest;import org.apache.xmlrpc.common.XmlRpcHttpRequestConfig;import org.apache.xmlrpc.server.AbstractReflectiveHandlerMapping;import org.apache.xmlrpc.server.PropertyHandlerMapping;import org.apache.xmlrpc.server.XmlRpcHandlerMapping;import org.apache.xmlrpc.webserver.XmlRpcServlet;public class MyServlet extends XmlRpcServlet {private static final long serialVersionUID = 1L;private boolean isAuthenticated(String pUserName, String pPassword) {return "foo".equals(pUserName) && "bar".equals(pPassword);}protected XmlRpcHandlerMapping newXmlRpcHandlerMapping() throws XmlRpcException {PropertyHandlerMapping mapping = (PropertyHandlerMapping) super.newXmlRpcHandlerMapping();AbstractReflectiveHandlerMapping.AuthenticationHandler handler = new AbstractReflectiveHandlerMapping.AuthenticationHandler() {public boolean isAuthorized(XmlRpcRequest pRequest) {XmlRpcHttpRequestConfig config = (XmlRpcHttpRequestConfig) pRequest.getConfig();return isAuthenticated(config.getBasicUserName(), config.getBasicPassword());};};mapping.setAuthenticationHandler(handler);return mapping;}}


服务控制Servlet:

package demo.xmlrpc.authentication;import java.io.IOException;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.xmlrpc.XmlRpcException;import org.apache.xmlrpc.server.PropertyHandlerMapping;import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;import org.apache.xmlrpc.webserver.XmlRpcServletServer;import demo.xmlrpc.action.Calculator;import demo.xmlrpc.action.HelloHandlerImpl;public class MyAuthenticationServlet extends MyServlet{private static final long serialVersionUID = 1L;private XmlRpcServletServer server;    public void init(ServletConfig pConfig) throws ServletException {          super.init(pConfig);          try {                // 创建一个XmlRpcServletServer对象                server = new XmlRpcServletServer();                                // 创建一个接收器并映射到XmlRpcServletServer对象                PropertyHandlerMapping phm =(PropertyHandlerMapping)newXmlRpcHandlerMapping();                phm.addHandler("HelloHandler", HelloHandlerImpl.class);                phm.addHandler("Calculator", Calculator.class);                              server.setHandlerMapping(phm);                // 更多XmlRpcServletServer对象设置                 XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl)server.getConfig();                serverConfig.setEnabledForExtensions(true);                serverConfig.setContentLengthOptional(false);          } catch (XmlRpcException e) {                try {                      log("Failed to create XmlRpcServer: " + e.getMessage(), e);                } catch (Throwable ignore) {                }                throw new ServletException(e);          }    }    public void doPost(HttpServletRequest pRequest, HttpServletResponse pResponse)          throws IOException, ServletException {          server.execute(pRequest, pResponse);    }}


在web.xml文件添加如下内容:

  <servlet><servlet-name>Authentication</servlet-name><servlet-class>demo.xmlrpc.authentication.MyAuthenticationServlet</servlet-class></servlet><servlet-mapping><servlet-name>Authentication</servlet-name><url-pattern>/Authentication</url-pattern></servlet-mapping>



创建一个包,里面添加一个配置文件,名字如下。两个名字一定不能变,这很雷人的,

框架规定的。

包名:org.apache.xmlrpc.webserver

文件名为:XmlRpcServlet.properties

文件内容为:Calculator=demo.xmlrpc.action.Calculator

Client端:
创建一个普通工程,导入除server以外的4个jar包

远程客户端调用类:
package demo.xmlrpc;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;import java.util.Vector;import org.apache.xmlrpc.XmlRpcException;import org.apache.xmlrpc.client.XmlRpcClient;import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;public class Client1 {public static void main(String[] args) {try {// 客户端配置对象,并且设置用户名密码XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();config.setBasicUserName("foo");config.setBasicPassword("bar");config.setServerURL(new URL("http://192.168.1.217:8080/XML_RPC/Authentication"));//创建一个XmlRpcClient对象,并给它绑定一个配置对象XmlRpcClient client = new XmlRpcClient();client.setConfig(config);// 创建参数集合Vector<Integer> params= new Vector<Integer>();params.addElement(3);params.addElement(8);Integer result=(Integer)client.execute("Calculator.add",params);System.out.println(result);} catch (MalformedURLException e) {System.out.println(e.toString());} catch (XmlRpcException e) {System.out.println(e.toString());} catch (IOException e) {e.printStackTrace();}}}


部署启动Server端的web工程,运行Client端的程序。返回结果为11。





热点排行