webService添加basic验证.docx_第1页
webService添加basic验证.docx_第2页
webService添加basic验证.docx_第3页
webService添加basic验证.docx_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Web service提供数据供别人使用,如果因为数据的保密性需添加认证,可以使用axis2提供的basic验证。具体做法如下。如果项目是基于axis2开发的,需修改web.xml文件第一步:Apache-Axis2 AxisServlet Apache-Axis Servlet org.apache.axis2.transport.http.AxisServlet !- !-axis2.xml.path- !-/WEB-INF/conf/axis2.xml- !-axis2.xml.url- !-http:/localhost/myrepo/axis2.xml- !-axis2.repository.path- !-/WEB-INF- !-axis2.repository.url- !-http:/localhost/myrepo- !- 1 . . AxisServlet /services/* bank_member BASIC Axis Basic Authentication Area bank_member AxisServlet 是你的名,就是需要给那个servlet添加验证,这块的名字就要和那个名字对应/services/* 是那些请求地址需要验证,例子中是services下的所有请求都需要验证Bank_member表示角色名只有在服务端属于bank_member角色(组)中的人员才被允许访问该web service且,该该访问采用“BASIC”模式,即需要用户名和密码来进行访问。然后修改tomact 配置文件 cof文件夹下的tomcat-users.xml文件添加用户名密码: 好了,这样就可以了,修改完后重启tomcat。输入web service地址,上面是对/service/* 下请求拦截,此时就会发现输入正确的用户名和密码就会进入web service的wsdl描述页面,错误的话就会就如一个401错误页面。OK,这样就可以了,下来就是如何通过验证访问web service电脑客户端访问代码如下:public class AuthTest public static void main(String args) try ServiceClient sc = new ServiceClient(); Options opts = sc.getOptions(); opts.setTo(new EndpointReference(http:/localhost:8080/axis2/services/PrizeInfoService/getAllCommentByPage); opts.setAction(urn:getAllCommentByPage); HttpTransportProperties.Authenticator basicAuth = new HttpTransportProperties.Authenticator(); basicAuth.setUsername(用户名); basicAuth.setPassword(密码); / basicAuthentication.setHost(xx.xx.x.xx); / basicAuthentication.setDomain(xx.xx.xx.xx); opts.setProperty(HTTPConstants.AUTHENTICATE, basicAuth); System.out.println(HTTPConstants.AUTHENTICATE); OMElement res = sc.sendReceive(createPayLoad(); System.out.println(res); catch (AxisFault e) e.printStackTrace(); public static OMElement createPayLoad() OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace(,m); OMElement method = fac.createOMElement(getAllCommentByPage, omNs); /如果请求的方法需要参数,如下添加 OMElement value = fac.createOMElement(programNumber, null); value.setText(1); method.addChild(value); System.out.println(method); return method; Android端是通过Ksoap访问web service的代码如下:首先自定义一个类继承HttpTransportSE,我这块定义类名为MyHttpTransportSE。在自定义类中复写getServiceConnection( )方法,代码如下public class MyHttpTransportSE extends HttpTransportSE public MyHttpTransportSE(String url) super(url);protected ServiceConnection getServiceConnection() throws IOException /设置验证用户名和 密码ServiceConnection connection = super.getServiceConnection();String Login = Base64.encode(账号:密码.getBytes();connection.setRequestProperty(Authorization, Basic + Login);return connection;源码中直接返回一个new ServiceConnection(),我们现在要访问有验证的web service,需要在创建连接时设置请求信息,代码就是上面的啦。然后就是ksoap访问web service步骤啦,代码如下:public class Util /命名空间final static String serviceNameSpace = ;public final static String ERROR = netError;/定义一个常量,表示异常返回信息public static String getInfoFromWeb(String method, String address) String resultString = ;/ 该变量用于保存接收到的字符串/ 实例化SoapObject对象SoapObject request = new SoapObject(serviceNameSpace, method); /如果调用的方法有参数,则给request中添加参数名,和参数,有多少加多少 Request.addProperty(“参数名”,参数值);/ 获得序列化的EnvelopeSoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);/这块的SoapEnvelope.VER11要看你web service那端的版本了envelope.bodyOut = request;envelope.dotNet = true;envelope.setOutputSoapObject(request);/ 构建传输对象,并指明WSDL文档URLString serviceURL = http:1:8080/axis2/services/PrizeInfoService/+method);/ Android传输对象MyHttpTransportSE ht = new MyHttpTransportSE(serviceURL);ht.debug = true;StringBuffer SOAP_ACTION = new StringBuffer();SOAP_ACTION.append(serviceNameSpace);SOAP_ACTION.append(/);SOAP_ACTION.append(method);try ht.call(SOAP_ACTION.toString(), envelope);/ 如果获得信息if (envelope.bodyIn != null) / 获取接收的数据/ 将传输结果包装成字符串返回Log.e(receive msg, envelope.bodyIn.toString();SoapObject soapObject = (SoapObject) envelope.bodyIn;for (int i = 0; i soapObject.getPropertyCount(); i+) resultString = soapObject.getProperty(i).toString();Log.e(receive

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论