根据 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认证基本流程

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 服务流程

为 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:\n\tThe 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:\n\tThe summation is: " + value);
} catch (Exception e) {
e.printStackTrace();
}
}
}

  

Web services 安全 - HTTP Basic Authentication的更多相关文章

  1. UTL_DBWS - Consuming Web Services in Oracle 10g Onward

    from:http://oracle-base.com/articles/10g/utl_dbws-10g.php In a previous article I presented a method ...

  2. Web API 基于ASP.NET Identity的Basic Authentication

    今天给大家分享在Web API下,如何利用ASP.NET Identity实现基本认证(Basic Authentication),在博客园子搜索了一圈Web API的基本认证,基本都是做的Forms ...

  3. HTTP Basic Authentication认证(Web API)

    当下最流行的Web Api 接口认证方式 HTTP Basic Authentication: http://smalltalllong.iteye.com/blog/912046 什么是HTTP B ...

  4. Web验证方式(1)--Http Basic Authentication

    Http Basic Authentication是HTTP协议中定义的Web系统中的验证方式.参考wiki 主要的实现机制如下: 1. 用户通过浏览器匿名访问web资源. 2. web服务器检测到w ...

  5. Basic Authentication in ASP.NET Web API

    Basic authentication is defined in RFC 2617, HTTP Authentication: Basic and Digest Access Authentica ...

  6. ASP.NET Web API 实现客户端Basic(基本)认证 之简单实现

    优点是逻辑简单明了.设置简单. 缺点显而易见,即使是BASE64后也是可见的明文,很容易被破解.非法利用,使用HTTPS是一个解决方案. 还有就是HTTP是无状态的,同一客户端每次都需要验证. 实现: ...

  7. 就是这么简单!使用Rest-assured 测试Restful Web Services

    使用 Rest-assured 测试 Restful Web Services 转载注明出处: http://www.cnblogs.com/wade-xu/p/4298819.html 这里向大家介 ...

  8. Using PL/SQL APIs as Web Services

    Overview Oracle E-Business Suite Integrated SOA Gateway allows you to use PL/SQL application program ...

  9. Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结

    Atitit HTTP认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结 1.1. 最广泛使用的是基本验证 ( ...

随机推荐

  1. PHP中VC6、VC9、TS、NTS版本的区别与用法详解

    Thread safe(线程安全)是运行在Apache上以模块的PHP上,如果你以CGI的模式运行PHP,请选择非线程安全模式(non-thread safe). 1. VC6与VC9的区别: VC6 ...

  2. atitit.获取北京时间CST 功能api总结 O7

    atitit.获取北京时间CST 功能api总结 O7 1. 获取cst时间(北京时间)两布:1.抓取url timtstamp >>format 到cst 1 2. 设置本机时间  se ...

  3. Maven学习总结(四)——Maven核心概念——转载

    一.Maven坐标 1.1.什么是坐标? 在平面几何中坐标(x,y)可以标识平面中唯一的一点. 1.2.Maven坐标主要组成 groupId:组织标识(包名) artifactId:项目名称 ver ...

  4. 一个映射到mac风格按键的AHK脚本(替换虚拟机键盘映射)

    Mac键位映射(部分) win+q 退出程序 win+w 关闭当前页面 win+h 隐藏当前窗口 win+shift+h 隐藏其他窗口 win+s 保存 win+o 打开 win+z 撤销 win+s ...

  5. jquery实现简单的Tab切换菜单

    实现tab切换的主要html代码: <div class="container"> <ul class="tabs"> <li c ...

  6. HTML常用命名和CSS reset代码【收集总结】

    CSS命名规则 头:header 内容:content/containe 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wrapper 左右中:l ...

  7. 一致性hash和solr千万级数据分布式搜索引擎中的应用

    互联网创业中大部分人都是草根创业,这个时候没有强劲的服务器,也没有钱去买很昂贵的海量数据库.在这样严峻的条件下,一批又一批的创业者从创业中 获得成功,这个和当前的开源技术.海量数据架构有着必不可分的关 ...

  8. 【高德地图API】如何设置Marker的offset?

    一些朋友在往地图上添加标注的时候,往往会发现,图片的尖尖角对不上具体的点.比如,我要在上海东方明珠上扎一个点. 首先,我使用取点工具http://lbs.amap.com/console/show/p ...

  9. #入魔这些年#零度智控&模型控社区大型征文活动,万元大奖等你拿

        玩航模是个好爱好,它能培养严谨的科学态度.超强的DIY动手能力.规范的项目管理习惯.良好的沟通合作技巧.过人的三维空间思维和感知: 你需要的知识储备有力学.空气动力学.电子学.通讯工程.材料学 ...

  10. SSD性能对比

    SSD性能测试顺序写 16k iops 85061.08顺序写 8k iops 146250.93顺序写 4k iops 239816.69顺序写 2k iops 294540.87顺序写 1k io ...