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;}}
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); }}
<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>
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();}}}