AXIS2 访问控制的 BASIC认证说明

来源:互联网 发布:nba2k16三井寿捏脸数据 编辑:程序博客网 时间:2024/05/24 00:37

在进行数据同步的时候,有时候要调用一些系统自己开发的webservice,但是有些开发商为了项目进度并没有考虑安全问题。其实采取采取基于用户名和密码的访问控制,从实现上也是很方便的。参考网上资料,基本思路是

1 首先启用容器的访问控制功能;

2 客户端在调用web 服务的时候,增加basic认证的参数。

 

具体操作步骤如下:

实验环境参考 http://www.blogjava.net/nokiaguy/archive/2010/05/15/249556.html?opt=admin

首先按照这篇文章测试好客户端代码和web服务。

 

为了给wsdl的访问增加用户认证,需要在container,比如tomcat 里面增加如下内容。


   <security-constraint>
      <display-name>Example Security Constraint</display-name>
      <web-resource-collection>
         <web-resource-name>Protected Area</web-resource-name>
  <!-- Define the context-relative URL(s) to be protected此处说明要保护的url是SimpleService下面的内容 -->
         <url-pattern>/services/SimpleService/*</url-pattern> 
   
  <!-- If you list http methods, only those methods are protected -->
  <http-method>DELETE</http-method>
         <http-method>GET</http-method>
         <http-method>POST</http-method>
  <http-method>PUT</http-method>
      </web-resource-collection>
      <auth-constraint>
         <!-- Anyone with one of the listed roles may access this area,此处是tomcat-users里面定义的角色 -->
         <role-name>tomcat</role-name>
  <role-name>role1</role-name>
      </auth-constraint>
    </security-constraint>

    <!-- Default login configuration uses form-based authentication ,认证方法,目前测试form 认证无法通过客户端调用-->
    <login-config>
      <auth-method>BASIC</auth-method>
      <realm-name>Example  Authentication Area</realm-name>
     <!-- <form-login-config>
        <form-login-page>/axis2-web/loginservice.jsp</form-login-page>
        <form-error-page>/axis2-web/errorservice.jsp</form-error-page>
      </form-login-config> -->
    </login-config>
        
    <!-- Security roles referenced by this web application -->
    <security-role>
      <role-name>role1</role-name>
    </security-role>
    <security-role>
      <role-name>tomcat</role-name>
    </security-role>

 

三 在tomcat-users.xml里面设置 角色和账户

<role rolename="role1"/>
 <user username="role1" password="role1" roles="role1"/>

 

四 在server.xml里面检查Resource 和Realm的resourceName设置,此处不用修改,默认就是读取tomcat-users.xml 的数据。

此时直接访问http://localhost:8080/axis2/services/SimpleService?wsdl 就会要求输入用户名和密码才能访问。

 

五 参考步骤一的实验环境里面rpcclientservice的代码,增加如下内容

HttpTransportProperties.Authenticator basicAuthentication = new HttpTransportProperties.Authenticator(); 
        basicAuthentication.setPassword("role1");         
        basicAuthentication.setUsername("role1");
        //basicAuthentication.setHost("localhost");
        //basicAuthentication.setDomain("localhost");
        options.setProperty(HTTPConstants.AUTHENTICATE ,basicAuthentication);

并对qname 等属性按照实际情况进行调整,就可以通过basic 的认证方式直接调用wsdl的功能。

 

总结:在企业内部使用web服务的方式进行数据同步时,不应该不加认证的去访问服务。如果采用ws-security 之类的方式比较困难的话,那么利用容器提供的访问控制功能,也能在要求不高的前提下很快实现一定的安全性。相关资料还可以参考

http://u.zhubajie.com/space-2418666-do-blog-id-1013090.html 比较全的客户端开发说明

http://wujianjun.javaeye.com/blog/523727  一个sap的调用代码

P.S. 在ibm 的tdi 的一个webservice 函数组件里面,也提供了对basic 认证的设置。

原创粉丝点击