目录:
1、wsdd配置说明。
2、读取任意目录下的wsdd配置文件。
3、添加service服务。
4、添加权限认证。
5、完整代码样例。
内容:
1、wsdd配置说明
<?xml version="1.0" encoding="UTF-8"?> <deployment name="defaultClientConfig" xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java" xmlns:handler="http://xml.apache.org/axis/wsdd/providers/handler"> <globalConfiguration> <!--禁止纯粹的XML请求 --> <parameter name="disablePrettyXML" value="true" /> <!--接受dotNet soap请求 --> <parameter name="dotNetSoapEncFix" value="true" /> <!--禁止名称空间前缀优化 --> <parameter name="enableNamespacePrefixOptimization" value="false" /> <!-- 请求处理配置 ,每一个service请求前都会调用里面的handler--> <requestFlow> <!-- Java Web Services 处理类 --> <handler type="java:org.apache.axis.handlers.JWSHandler"> <!-- 会话范围 --> <parameter name="scope" value="request" /> <parameter name="extension" value=".jwr" /> </handler> <!-- soap监控 --> <handler type="java:org.apache.axis.handlers.SOAPMonitorHandler"/> </requestFlow> </globalConfiguration> <!-- 一个特定的HTTP处理程序,通常作为HTTP的传输链,接受这样的请求如:http://localhost:8080/was/was/TicketService?wsdl --> <handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper" /> <!-- 序列化本地响应消息 --> <handler type="java:org.apache.axis.transport.local.LocalResponder" name="LocalResponder" /> <!-- 定义服务器端的传输,当有请求送达时启用 --> <transport name="http"> <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:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler" /> <requestFlow> <handler type="URLMapper" /> <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler" /> </requestFlow> </transport> </deployment>
2、读取任意目录下的wsdd配置文件
增加servlet配置,web.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>WasAxisServlet</servlet-name> <display-name>Apache-Axis Servlet</display-name> <servlet-class> org.was.servlet.WasAxisServlet </servlet-class> <load-on-startup>10</load-on-startup> </servlet> <servlet-mapping> <servlet-name>WasAxisServlet</servlet-name> <url-pattern>/was/*</url-pattern> </servlet-mapping> </web-app>
读取位于WEB-INF/conf/server-config.wsdd文件Java代码如下:
package org.was.servlet; import java.io.File; import org.apache.axis.configuration.FileProvider; import org.apache.axis.server.AxisServer; import org.apache.axis.transport.http.AxisServlet; /** * 加载自定义配置文件Servlet * * @author orientalpigeon * */ public class WasAxisServlet extends AxisServlet { private static final long serialVersionUID = -7163843775902482518L; /** * 覆盖AxisServlet中的初始化AxisServer的方法,用于读取自定义webservices配置文件 */ public AxisServer getEngine() { if(axisServer != null) return axisServer; String fullPath = getLocalePath() + getAxisServerConfigPath(); FileProvider fileProvider = new FileProvider(fullPath); axisServer = new AxisServer(fileProvider); return axisServer; } /** * 设置axis配置文件相对路径 * @return */ private String getAxisServerConfigPath(){ StringBuffer relativePath = new StringBuffer(); relativePath.append("WEB-INF"); relativePath.append(File.separator); relativePath.append("conf"); relativePath.append(File.separator); relativePath.append("server-config.wsdd"); return relativePath.toString(); } /** * 读取Web应用根路径 * @return */ private String getLocalePath() { String contextPath = this.getServletContext().getRealPath("/"); if (contextPath == null) contextPath = ""; if (contextPath.endsWith("/") || contextPath.endsWith("\\")) { return contextPath; } contextPath += File.separator; return contextPath; } }
3、添加service服务
在wsdd配置文件中增加代码如下:
<!-- 自定义 Web Services 服务 --> <service name="TicketService" provider="java:RPC"> <!-- 允许访问全部方法 --> <parameter name="allowedMethods" value="*" /> <parameter name="scope" value="Session" /> <parameter name="className" value="org.was.service.TicketService" /> </service>
对应service服务Java代码如下:
package org.was.service; /** * 公开服务 * * @author orientalpigeon * */ public class TicketService { public int getTicketNumber(String trainNo){ int ticketNumber = 0; if("T261".equals(trainNo)){ ticketNumber = 10;// } return ticketNumber; } }
4、添加权限认证
在wsdd文件中增加权限配置代码如下:
<!-- 自定义认证配置 --> <handler name="Authenticate" type="java:org.was.security.AuthenticationHandler"/>
修改wsdd配置中service标签内容为:
<!-- 自定义 Web Services 服务 --> <service name="TicketService" provider="java:RPC"> <!-- 允许访问全部方法 --> <parameter name="allowedMethods" value="*" /> <parameter name="scope" value="Session" /> <parameter name="className" value="org.was.service.TicketService" /> <requestFlow> <!-- 增加认证handler --> <handler type="Authenticate"/> </requestFlow> </service>
认证Java代码如下:
package org.was.security; import javax.servlet.http.HttpServletRequest; import javax.xml.rpc.Call; import org.apache.axis.AxisFault; import org.apache.axis.MessageContext; import org.apache.axis.handlers.BasicHandler; import org.apache.axis.transport.http.HTTPConstants; /** * 用户权限认证处理 * * @author orientalpigeon * */ public class AuthenticationHandler extends BasicHandler { private static final long serialVersionUID = -7685663586762871169L; private static final String PUBLIC_USER = "public"; private static final String PUBLIC_PASSWORD = PUBLIC_USER; private static final String AUTHENTICATION_ERROR_MESSAGE = "用户名或密码不正确。"; /** * 请求认证回到方法 * * @param messageContext * -用户上下文消息对象 */ public void invoke(MessageContext messageContext) throws AxisFault { HttpServletRequest request = (HttpServletRequest) messageContext .getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST); //从请求header中获取可认证信息,很多人通常用Base64进行转码后认证 String user = request.getHeader(Call.USERNAME_PROPERTY); String password = request.getHeader(Call.PASSWORD_PROPERTY); if (!PUBLIC_USER.equals(user) || !PUBLIC_PASSWORD.equals(password)) { throw new AxisFault("Server.Unauthenticated", AUTHENTICATION_ERROR_MESSAGE, null, null); } } }
5、完整代码样例
见附件。