根据 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. Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php

    Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php 1. Jdk zip 跟apache ant zip 1 2. Apache Ant包进行ZIP文件压缩,upzip ...

  2. paip.sqlite 管理最好的工具 SQLite Expert 最佳实践总结

    paip.sqlite 管理最好的工具 SQLite Expert 最佳实践总结 一般的管理工具斗可以...就是要是sqlite没正常地关闭哈,有shm跟wal文件..例如ff的place.sqlit ...

  3. XML入门级的简单学习

    xml案例<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>Ge ...

  4. 使用gulp进行React任务的构建

    如果你不熟悉gulp的操作,可以看下下面的教程: gulp学习笔记1 gulp学习笔记2 gulp学习笔记3 gulp学习笔记4 对于gulp在react中的构建,找了很多资料,看了很多文章,也根据文 ...

  5. Android MultiDex兼容包怎么使用?

    在Android系统中安装应用的时候,需要对Dex进行优化,但由于其处理工具DexOpt的限制,导致其id的数目不能够超过65536个.而MultiDex兼容包的出现,就很好的解决了这个问题,它可以配 ...

  6. shell命令从目录中循环匹配关键词

    #!/bin/bash while read line do for file in /home/local/test/* do if test -f $file then sed -n " ...

  7. Android BitmapShader 实战 实现圆形、圆角图片

    转载自:http://blog.csdn.net/lmj623565791/article/details/41967509 1.概述 记得初学那会写过一篇博客Android 完美实现图片圆角和圆形( ...

  8. JavaScript中如何中断forEach循环

    先来看下forEach的实现 // Production steps of ECMA-262, Edition 5, 15.4.4.18// Reference: http://es5.github. ...

  9. InteliJ IDEA15 安装jrebel破解文件

    使用InteliJ IDEA这个工具感觉比eclipse好用,例如它在没有源码的情况下自动反编译源码等,但是在使用的时,有个很不爽的地方就是不能实时编译,导致java代码更改了一点代码就需要重启项目, ...

  10. mysql慢日志设置

    mysql的慢日志查询对于sql的优化还是很有意义的,具体说下如何开启这个mysql慢查询日志(默认是开启的). 关于设置在mysql的官方手册或网上都有很多,但是要注意的是,mysql5.6与之前的 ...