根据 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. xib与nib的区别

    xib和nib都是Interface Builder的图形界面设计文档,nib这个名字来自于NeXTSTEP系统,在NeXTSTEP被Apple收购之前,一直使用nib作为Interface Buil ...

  2. iscroll初体验

    引入 iscroll是什么?多的概念性让人看不懂的东西就不说了因为看了也可能看不明白,iscroll主要用于移动端设备,主要包括以下的应用场合: 缩放 拉动刷新 速度和性能提升 精确捕捉元素 自定义滚 ...

  3. Cocos2dx.3x入门三部曲-Hello Game项目创建(二)

    一.前提: 完成cocos2d-x-3.x开发环境的安装配置. 具体参考:Cocos2d-x.3x_软件配置篇 二.本篇目标: l  创建一个名为hello game的cocos2dx 3.3的项目 ...

  4. asp.net“服务器应用程序不可用” 解决方法

    服务器应用程序不可用 您试图在此 Web 服务器上访问的 Web 应用程序当前不可用.请点击 Web 浏览器中的“刷新”按钮重试您的请求. 管理员注意事项: 详述此特定请求失败原因的错误消息可在 We ...

  5. Android GPS 临近触发

    前面介绍过LocationManager有一个addProximityAlert(double latitude,double longitude,float radius,long expirati ...

  6. windows server 2008 R2 FTP登陆错误。

    建立了一个域用户ftp. 始终登陆不上winserver 2008 R2上的FTP. 错误如下: 530-User cannot log in. Win32 error:   Logon failur ...

  7. Convert IPv6 Address to IP numbers (C#)

    URL: http://lite.ip2location.com/ Use the code below to convert the IP address of your web visitors ...

  8. 【linux】vim编辑器vim+taglist+ctags的配置

    很多linux软件开发实际上并不实在X window的情况下进行的,这时我们不可能启动基于X window的图形化窗口,在这一情况下我们所能使用的主要的编辑器是vim和emacs.(ps:emacs和 ...

  9. [aaronyang]WPF4.5 - AyTabControlBase样式分享,绝对好看

    样式代码如下: 对于博客园将文章移除首页的做法:我就迁移了.文章已经迁移:http://www.ayjs.net/post/75.html 由于例子比较简单,你只要指定Style即可,难点,透明区域的 ...

  10. Android使用的设计模式2——策略模式

    今天讲解一下策略模式,策略模式也是很常用的设计模式,对多种算法或者数据结构选择使用的情况下,经常会使用策略模式来管理这些算法.下面会简单讲解一下策略模式的概念和基本实现.然后结合Android里面的实 ...