Web services 安全 - HTTP Basic Authentication
根据 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) 中获取认证。在正式开始之前,我们需要明白以下名词的含义:
- Authentication,即认证,验证。它是一种确认的过程,通过这样的认证过程,你可以确定某物体是不是它所声称的那种物体。这通常会涉及到用户名和密码,也可能是身份证明,或生物特征,如视网膜等。
- Realm,即域。一个 Realm 就是一系列用户名和密码的“数据库”,它通常用来保存、识别某个或某些 Web 应用中有效的用户和密码。它还定义了每个有效用户所对应的角色。
本文将介绍如何使用 HTTP BASIC Authentication 来保护 Web services endpoint 服务资源,当 Web services 的 Endpoints 被设置为 BASIC HTTP 认证才能访问的受限资源时,用户必须提供用户名密码才能使用它们,基本的流程如图 2 所示。
图 2. Web services 客户端访问受限 Web services 服务流程

为 Web Application 配置 Basic Authentication:
- 打开 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"/> - 打开 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的更多相关文章
- 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 ...
- Web API 基于ASP.NET Identity的Basic Authentication
今天给大家分享在Web API下,如何利用ASP.NET Identity实现基本认证(Basic Authentication),在博客园子搜索了一圈Web API的基本认证,基本都是做的Forms ...
- HTTP Basic Authentication认证(Web API)
当下最流行的Web Api 接口认证方式 HTTP Basic Authentication: http://smalltalllong.iteye.com/blog/912046 什么是HTTP B ...
- Web验证方式(1)--Http Basic Authentication
Http Basic Authentication是HTTP协议中定义的Web系统中的验证方式.参考wiki 主要的实现机制如下: 1. 用户通过浏览器匿名访问web资源. 2. web服务器检测到w ...
- Basic Authentication in ASP.NET Web API
Basic authentication is defined in RFC 2617, HTTP Authentication: Basic and Digest Access Authentica ...
- ASP.NET Web API 实现客户端Basic(基本)认证 之简单实现
优点是逻辑简单明了.设置简单. 缺点显而易见,即使是BASE64后也是可见的明文,很容易被破解.非法利用,使用HTTPS是一个解决方案. 还有就是HTTP是无状态的,同一客户端每次都需要验证. 实现: ...
- 就是这么简单!使用Rest-assured 测试Restful Web Services
使用 Rest-assured 测试 Restful Web Services 转载注明出处: http://www.cnblogs.com/wade-xu/p/4298819.html 这里向大家介 ...
- Using PL/SQL APIs as Web Services
Overview Oracle E-Business Suite Integrated SOA Gateway allows you to use PL/SQL application program ...
- Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结
Atitit HTTP认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结 1.1. 最广泛使用的是基本验证 ( ...
随机推荐
- duilib进阶教程 -- 响应windows原生消息和自定义消息(13)
一.windows原生消息 同样,入门教程只是给出了响应windows原生消息的方法,并没给出例子,这里以自适应屏幕分辨率为例.迅雷播放器虽然可以在启动的时候自动调整窗口大小,但是当屏幕分辨率实时改变 ...
- chrome浏览器扩展的事件处理
关于chrome扩展开发的栗子已经有很多了,问问度娘基本能满足你的欲望, 我想说的是扩展和页面间的数据传递问题. 我们知道写扩展有个必须的文件就是“manifest.json”, 这个里面定义了一个和 ...
- ZPL打印中文信息
博客来源:http://www.cnblogs.com/Geton/p/3595312.html 相信各位在实际的项目中,需要开发打条码模块的也会有不少,很多同行肯定也一直觉得斑马打印机很不错,但是Z ...
- Windows Server 2008 R2 备份和恢复 (转)
Windows Server Backup : 1.安装Windows Server Backup的方法: 通过"服务器管理器"中的"添加功能"向导进行安装. ...
- DNS劫持
忽然发现我最喜欢的chrome 一直有广告,好些论坛都有大量的广告,原以为是重了什么插件了,找了也没发现,有可能是DNS劫持. 打开路由器,找到 dns 设置里面把里面的全部去掉.然后把 192.16 ...
- Ceph monitor故障恢复探讨
1 问题 一般来说,在实际运行中,ceph monitor的个数是2n+1(n>=0)个,在线上至少3个,只要正常的节点数>=n+1,ceph的paxos算法能保证系统的正常运行.所以,对 ...
- Linux: Set OR Change The Library Path
Linux: Set OR Change The Library Path by NIX CRAFT on APRIL 9, 2010 · 3 COMMENTS· LAST UPDATED AUGUS ...
- ngnix编译遇到的问题.
总结:先后遇到libz库文件没有正确的链接和pcre库文件没有正确的链接 1./configure后提示需要zlib 2.locate zlib,系统中没有zlib的共享库so文件,但是有一些头文件, ...
- 神舟K650c i7(W350STQ)上成功装好Mac OS X 10.9,兼谈如何安装WinXP、7、8.1、OSX、Ubuntu五系统(Chameleon、MBR)
作者:zyl910 参考教程——http://bbs.pcbeta.com/viewthread-1432534-1-4.html笔记本SNB和IVY平台Win7/Win8/Win8.1安装OS X ...
- nodejs+express+jade安装备忘
安装步骤 1.安装nodejs,比如安装在E:\nodejs. 确保有两个环境变量 用户环境变量:C:\Users\Administrator\AppData\Roaming\npm 系统环境变量:e ...