`
fxly0401
  • 浏览: 146413 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

基于axis2开发授权web service

阅读更多
本文服务端使用axis2发布web service服务,客户端采用两种方式实现:
1. 由wsdl文件生成stub客户端代码
2. 不生产stub代码,纯手工编写客户端

服务端代码如下:
1. service.xml配置文件如下,
<service name="WS"><!-- web service名称-->
	<description>Axis Service Description</description><!-- 接口描述-->
	<parameter name="ServiceClass">com.*.webservice.WSone</parameter><!-- 接口实现类-->
	<operation name="getPrice"><!-- 接口名称-->
		<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
	</operation>
	<operation name="updatePrice"><!-- 接口名称-->
		<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
	</operation>
	<operation name="sum"><!-- 接口名称-->
		<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
	</operation>
	
</service>



2. 接口实现类WSnoe代码如下,
import java.util.HashMap;
import org.apache.axis2.AxisFault;

public class WSone {
	private HashMap map = new HashMap();
	public double getPrice(String symbol) throws AxisFault {
		LoginCheck.checkUserPwd(); // 当客户端调用getPrice方法是,在此处先进行用户名和密码校验,如果校验通过则继续后续逻辑处理,如果不通过则抛出异常。
		Double price = (Double) map.get(symbol);
		if (price != null) {
			return price.doubleValue();
		}
		return 42.00;
	}

	public void updatePrice(String symbol, double price) throws AxisFault {
		LoginCheck.checkUserPwd(); // 在此处先进行用户名和密码校验,如果校验通过则继续后续逻辑处理,如果不通过则抛出异常。
		map.put(symbol, new Double(price));
	}

	public int sum(int num1, int num2) throws AxisFault {
		LoginCheck.checkUserPwd(); // 在此处先进行用户名和密码校验,如果校验通过则继续后续逻辑处理,如果不通过则抛出异常。
		return num1 + num2;
	}
}

服务端授权认证模块类是LoginCheck,本例中用户名和密码是定死的,可根据实际情况修改,内容如下:
import java.util.Iterator;
import org.apache.axiom.om.OMElement;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;

public class LoginCheck {
	public static void checkUserPwd() throws AxisFault {
		MessageContext msgContext = MessageContext.getCurrentMessageContext();
		Iterator list = (Iterator) msgContext.getEnvelope().getHeader().getFirstElement().getChildren();
		String Username = "";
		String Password = "";
		while (list.hasNext()) {
			OMElement element = (OMElement) list.next();
			if (element.getLocalName().equals("Username")) {
				Username = element.getText();
			}
			if (element.getLocalName().equals("Password")) {
				Password = element.getText();
			}
		}
		if (!Username.equals("toone") || !Password.equals("111")) {
			throw new AxisFault(" Authentication Fail! Check username/password ");
		}else{
			System.out.println("use:"+Username+"验证通过");
		}
	}
}


客户端代码如下:
客户端需要将包含认证用到的用户名和密码的OMElement对象放置到soap header中。
1. 包含用户名和密码的OMElement对象HeaderOMElement代码如下,
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;

public class HeaderOMElement {
	public static OMElement createHeaderOMElement() {
		OMFactory factory = OMAbstractFactory.getOMFactory();
		OMNamespace SecurityElementNamespace = factory.createOMNamespace("http://handler.com", "wsse");
		OMElement authenticationOM = factory.createOMElement("Authentication",SecurityElementNamespace);
		OMElement usernameOM = factory.createOMElement("Username",SecurityElementNamespace);
		OMElement passwordOM = factory.createOMElement("Password",SecurityElementNamespace);
		usernameOM.setText("toone");
		passwordOM.setText("1111");
		authenticationOM.addChild(usernameOM);
		authenticationOM.addChild(passwordOM);
		return authenticationOM;
	}
}

2. 客户端代码实现方式1:不生成stub客户端代码
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import com.*.testws.HeaderOMElement;

public class PojoClient {
	public static void main(String[] args1)  {
		String serviceName = "http://localhost:8080/TestWS/services/WS";
		String namespace = "http://webservice.*.com";
		String methodName = "getPrice";
		Object[] methodArgs = new Object[] {};
		Class[] returnTypes = new Class[] { Double.class };
		try {
			RPCServiceClient serviceClient = new RPCServiceClient();
			// 将创建的OMElement对象放置到Header中
			serviceClient.addHeader(HeaderOMElement.createHeaderOMElement());
			Options options = serviceClient.getOptions();
			EndpointReference targetEPR = new EndpointReference(serviceName);
			options.setTo(targetEPR);
			QName op = new QName(namespace, methodName);
			Object[] response = serviceClient.invokeBlocking(op, methodArgs,returnTypes);
			Double result = (Double) response[0];
			if (result == null) {
				System.out.println("didn't initialize!");
				return;
			} else {
				System.out.println("Price ====== " + result.doubleValue());
			}
		} catch (AxisFault e) {
			System.out.println("验证失败");
			e.printStackTrace();
		}
	}

}


3. 客户端代码实现方式2:生成stub客户端代码
import java.rmi.RemoteException;
import org.apache.axis2.AxisFault;
import org.apache.axis2.client.ServiceClient;
import com.*.webservice.WSStub;

public class TestClient {
	public static void main(String[] args) {
		try {
			WSStub stub = new WSStub("http://localhost:8080/TestWS/services/WS");
			ServiceClient sc = stub._getServiceClient();
			sc.addHeader(HeaderOMElement.createHeaderOMElement());
			stub._setServiceClient(sc);
			
			WSStub.Sum sums = new WSStub.Sum();
			sums.setNum1(2);
			sums.setNum2(3);
			WSStub.SumResponse sr = stub.sum(sums);
			System.out.println(sr.get_return());
			
			WSStub.GetPrice gp = new WSStub.GetPrice();
			gp.setSymbol("sino");
			WSStub.GetPriceResponse gpr = stub.getPrice(gp);
			System.out.println(gp.getSymbol()+" : "+gpr.get_return());
			
			WSStub.UpdatePrice up = new WSStub.UpdatePrice();
			up.setSymbol("sino");
			up.setPrice(100.0);
			stub.updatePrice(up);
			
			WSStub.GetPrice gpnew = new WSStub.GetPrice();
			gpnew.setSymbol("sino");
			WSStub.GetPriceResponse gprnew = stub.getPrice(gpnew);
			System.out.println(gpnew.getSymbol()+" : "+gprnew.get_return());

		}  catch (AxisFault e) {
			System.out.println("验证失败:"+e.getMessage());
			e.printStackTrace();
		}catch (RemoteException e) {
			System.out.println("远程连接失败:"+e.getMessage());
			e.printStackTrace();
		}
	}
}


以上授权实现,仅是用作授权认证机制,用户名和密码在soap消息传递过程中为明文传递,不能实现链路或信息加密。
使用TcpTrace工具进行抓包验证,参见链接:http://fxly0401.iteye.com/blog/1940119
分享到:
评论

相关推荐

    web service axis 1.6

    Web服务Axis 1.6是Apache软件基金会开发的一个开源工具...尽管现在已经有了更现代的Web服务框架,如Axis 2和其他基于JAX-WS的实现,但了解和掌握Axis 1.6的基本知识仍然对于理解Web服务的原理和技术历史具有重要意义。

    Axis2-1.6.2

    5. **安全性**:Axis2支持多种安全机制,如WS-Security,可以保护Web服务免受未经授权的访问。 总之,Axis2-1.6.2是一个强大的Web服务框架,它为开发者提供了高效的工具来构建、部署和管理Web服务。通过理解其核心...

    springboot集成axis2-1.7.9实例

    Apache Axis2是Java平台上的一个Web服务框架,它提供了高性能、灵活的服务开发和部署机制。而Spring Boot是基于Spring框架的一个微服务启动器,简化了配置并提供了快速开发新应用的能力。通过将两者结合,我们可以...

    Web Service开发指南

    2. Web Service的工作流程 Web Service的工作流程通常包括以下步骤: - 服务提供者创建一个Web Service,并使用WSDL描述其接口和功能。 - 服务提供者将WSDL发布到一个可以通过UDDI查找的服务目录。 - 服务消费者...

    XML Web Service开发教程

    在开发XML Web Service时,我们将学习如何使用工具如Apache Axis、Microsoft .NET的ASMX或WCF(Windows Communication Foundation)等,它们简化了服务的创建、部署和调用。例如,ASMX允许开发者使用.NET Framework...

    Axis2的发布和生产stub的操作

    使用Axis2的wsdl2java工具,你可以根据WSDL(Web Service Description Language)文件自动生成客户端Stub。这使得客户端代码能够像调用本地方法一样调用远程服务。生成Stub的步骤大致如下: 1. 获取服务的WSDL URL。...

    springboot+axis1.4

    5. **暴露端点**:在Axis1.4中,你需要通过`wsdl2java`工具生成服务的WSDL(Web Service Description Language)文件,然后将这个WSDL文件的位置暴露为一个HTTP端点。Spring Boot可以通过`@Bean`注解创建一个`...

    axis web service

    ### Apache Axis2与Web服务开发 #### 一、引言:了解Apache Axis2及Web服务 随着互联网技术的发展,Web服务已经成为跨平台、跨语言进行数据交换和业务交互的重要手段之一。Apache Axis2作为一款开源的Java Web服务...

    web service reference

    - **Apache Axis**:一款流行的Java Web Service框架,提供了服务端和客户端的实现。 - **Eclipse WTP (Web Tools Platform)**:Eclipse的一个插件,提供了一整套工具用于Web应用和Web Service的开发。 - **Spring ...

    WebService axis2使用说明

    Axis2是Apache软件基金会开发的一个用于构建和部署Web服务的Java框架,它是Axis1的下一代产品。 2.1. **基本介绍** Axis2提供了一个强大的Web服务引擎,支持多种传输协议(如HTTP、HTTPS、SMTP等),并且可以与...

    WebService之axis2的使用(三)

    WebService之Axis2的使用是开发基于SOAP协议的Web服务的重要工具。Axis2是Apache软件基金会开发的一个开源项目,它提供了一种高效、灵活且可扩展的框架,用于构建和部署Web服务。本篇将深入探讨Axis2的核心概念、...

    axis2 webservice实现文件上传删除功能

    Axis2是Apache软件基金会开发的一个高效且灵活的Web服务框架,它基于Java语言,并且是 Axis1 的下一代产品。本篇文章将深入探讨如何使用Axis2来实现文件的上传和删除功能,这对于构建分布式系统和跨平台应用至关重要...

    Axis2_Codegen_Wizard_1.3.0

    【标题】"Axis2_Codegen_Wizard_1.3.0"是一款专为MyEclipse Web开发设计的Axis插件,其主要功能是自动化生成与Web服务相关的代码,极大地简化了开发过程,提高了开发效率。该插件是基于Apache Axis2框架的,Axis2是...

    axis入门中文文档

    在使用Axis开发Web服务时,还需要考虑安全性问题,如身份验证、授权和加密。此外,可以通过调整Axis配置、缓存策略等方式优化服务性能。 九、故障排查与调试 在开发过程中,可能会遇到各种错误和异常,如网络问题、...

    一个实现天气查询的WEB Service的源程序

    【标题】中的“一个实现天气查询的WEB Service的源程序”指的是使用Web Service技术开发的、用于查询天气信息的应用程序。Web Service是一种基于互联网的、跨平台的通信标准,允许不同系统之间通过XML(可扩展标记...

    web项目添加webservice 服务端(axis)

    Apache Axis是Apache软件基金会开发的开源项目,它提供了完整的Web服务堆栈,包括客户端和服务器端的工具,支持JAX-RPC(Java API for XML-based Remote Procedure Call)规范。Axis使开发人员能够轻松地创建和部署...

    Web Service详细解析及使用方法

    AXIS是Apache软件基金会开发的另一个Web Service框架,它提供了更广泛的协议支持和高级功能。 - **AXIS简介** AXIS提供了一个全面的解决方案,包括编码器/解码器、协议处理器、容器和服务发布工具。它支持SOAP ...

    WebService之axis2的使用(四)

    它基于Axis1,并且提供了更加强大和高效的特性,比如模块化架构、更好的性能以及对SOAP 1.2的支持。本篇文章将深入探讨如何使用Axis2进行Web服务的开发与调用,以及其核心概念和工作流程。 1. **Axis2基础知识** -...

Global site tag (gtag.js) - Google Analytics