zoukankan      html  css  js  c++  java
  • Web services 安全

    根据 RFC2617 的规定,HTTP 有两种标准的认证方式,即,BASIC 和 DIGEST。HTTP Basic Authentication 是指客户端必须使用用户名和密码在一个指定的域 (Realm) 中获取认证。

    正如"HTTP Basic Authentication"这个名字,它是 Authentication( 认证 ) 中最简单的方法。长期以来,这种认证方法被广泛的使用。当你通过 HTTP 协议去访问一个使用 Basic Authentication 保护的资源时,服务器通常会在 HTTP 请求的 Response 中加入一个"401 需要身份验证"的 Header,来通知客户提供用户凭证,以使用资源。如果你正在使用 Internet Explorer 或者 Mozilla Firefox 这样的可视化浏览器来访问需要认证的资源,浏览器会弹出一个窗口,让你输入用户名和密码,如果所输入的用户名在资源使用者的验证列表,并且密码完全正确,此时,用户才可以访问受限的资源。

    HTTP Basic Authentication 介绍

    HTTP BASIC 认证的基本流程如图 1 所示,

    图 1. BASIC HTTP认证基本流程

    图 1. BASIC HTTP认证基本流程

    HTTP Basic Authentication 是指客户端在使用 HTTP 协议访问受限资源时,必须使用用户名和密码在一个指定的域 (Realm) 中获取认证。在正式开始之前,我们需要明白以下名词的含义:

    1. Authentication,即认证,验证。它是一种确认的过程,通过这样的认证过程,你可以确定某物体是不是它所声称的那种物体。这通常会涉及到用户名和密码,也可能是身份证明,或生物特征,如视网膜等。
    2. Realm,即域。一个 Realm 就是一系列用户名和密码的“数据库”,它通常用来保存、识别某个或某些 Web 应用中有效的用户和密码。它还定义了每个有效用户所对应的角色。

    本文将介绍如何使用 HTTP BASIC Authentication 来保护 Web services endpoint 服务资源,当 Web services 的 Endpoints 被设置为 BASIC HTTP 认证才能访问的受限资源时,用户必须提供用户名密码才能使用它们,基本的流程如图 2 所示。

    图 2. Web services 客户端访问受限 Web services 服务流程

    图 2. Web services 客户端访问受限 Web services 服务流程

    为 Web Application 配置 Basic Authentication:

    1. 打开 Tomcat 安装目录下的“conf”文件夹,修改文件“tomcat-users.xml”,该文件是用来存储 Tomcat 预加载的用户和角色定义的,此文件即是上文提到的 Realm。在“<tomcat-users>”中加入如下用户和角色:
      清单 2. 定义用户及其角色
        <!-- Web services invoker role --> 
        <role rolename="WsInvokerRole"/> 
        
        <!-- Web services invokers/users --> 
        <user username="wsaxis" password="wsaxis" roles="WsInvokerRole"/>
    2. 打开 Web 应用“TomcatAxis”的部署描述符:web.xml 文件,并在“<web-app>”中加入如下片段:
      清单 3. 配置安全资源
        <!-- configurations for BASIC Authentication --> 
        <security-constraint> 
          <web-resource-collection> 
            <web-resource-name>All Web services Endpoints</web-resource-name> 
            <url-pattern>/services/*</url-pattern> 
          </web-resource-collection> 
          
          <auth-constraint> 
            <description>Web services invokers are allowed doing invocation</description> 
            <role-name>WsInvokerRole</role-name> 
          </auth-constraint> 
        </security-constraint> 
        
        <!-- authentication method --> 
        <login-config> 
          <auth-method>BASIC</auth-method> 
          <realm-name>Realm of Web services Invokers</realm-name> 
        </login-config>

      在“<security-constraint>”片段内,定义了改 Web Application 需要保护的资源,可以通过“<url-pattern>”来定义符合一定 URL 样式的资源,上述片段的定义,保护了所有 Web services 的 endpoints。

      “<login-config>”片段定义了采取 BASIC 认证方式,其中“<realm-name>”只在 BASIC 认证方式下有效,它分配安全领域名,此名称供浏览器用于对话框标题,且为 Authorization 头部的一部分。

    静态调用类
     package sample.test.client.runable; 
    
     import java.net.URL; 
     import sample.test.ServiceImplServiceLocator; 
     import sample.test.ServiceImplSoapBindingStub; 
    
     public class StaticClientTest { 
      public static void main(String[] args) { 
        try { 
        // String userName = "wsaxis", password = "wsaxis"; 
          int addend = 64, augend = 128; 
          ServiceImplSoapBindingStub sisbs = new ServiceImplSoapBindingStub( 
              new URL(  "http://localhost:8080/TomcatAxis/services/ServiceImpl"), 
              new ServiceImplServiceLocator()); 
        // sisbs.setUsername(userName);   // sisbs.setPassword(password); 
          System.out 
              .println("Static Client Invocation:
    	The summation is: "
                  + sisbs.sum(addend, augend)); 
        } catch (Exception e) { 
          e.printStackTrace(); 
        } 
      } 
     }
    

      

    动态调用测试代码
     package sample.test.client.runable; 
     import javax.xml.namespace.QName; 
     import javax.xml.rpc.Call; 
     import javax.xml.rpc.ParameterMode; 
     import javax.xml.rpc.Service; 
     import javax.xml.rpc.ServiceFactory; 
     public class DynamicClientTest { 
      public static void main(String[] args) { 
        try { 
          String address = "http://localhost:8080/TomcatAxis/services/ServiceImpl"; 
          String namespaceURI = "http://test.sample"; 
          String serviceName = "ServiceImplService"; 
          String portName = "ServiceImpl"; 
          String operationName = "sum"; 
          String userName = "wsaxis", password = "wsaxis"; 
          int addend = 64, augend = 128; 
          ServiceFactory factory = ServiceFactory.newInstance(); 
          Service service = factory.createService(new QName(serviceName)); 
          Call call = service.createCall(new QName(portName)); 
          call.setTargetEndpointAddress(address); 
          QName intQName = new QName("http://www.w3.org/2001/XMLSchema","int"); 
          call.setOperationName(new QName(namespaceURI, operationName)); 
          call.addParameter("addend", intQName, ParameterMode.IN); 
          call.addParameter("augend", intQName, ParameterMode.IN); 
          call.setReturnType(intQName); 
          call.setProperty(Call.USERNAME_PROPERTY, userName); 
          call.setProperty(Call.PASSWORD_PROPERTY, password); 
          Object[] inParams = new Object[2]; 
          inParams[0] = new Integer(addend); 
          inParams[1] = new Integer(augend); 
          int value = ((Integer) call.invoke(inParams)).intValue(); 
          System.out.println("Dynamic Client Invocation:
    	The summation is: " + value); 
        } catch (Exception e) { 
          e.printStackTrace(); 
        } 
      } 
     }
    

      

  • 相关阅读:
    HDU 2955 Robberies(01背包)
    HDU 2602 Bone Collector(01背包)
    HUST 1352 Repetitions of Substrings(字符串)
    HUST 1358 Uiwurerirexb jeqvad(模拟解密)
    HUST 1404 Hamming Distance(字符串)
    HDU 4520 小Q系列故事――最佳裁判(STL)
    HDU 2058 The sum problem(枚举)
    【破解】修改程序版权、添加弹窗
    HDU 1407 测试你是否和LTC水平一样高(枚举)
    HDU 1050 Moving Tables(贪心)
  • 原文地址:https://www.cnblogs.com/silva/p/6010634.html
Copyright © 2011-2022 走看看