因为RESTful的无状态特性,导致无法知道当前的请求方是否可靠,所以不得不对每次请求进行验证。但是如何更语义化的将需要验证的信息附加到HTTP里呢?现在比较常见的方式是把验证信息作为参数发送过去,但是这样会违反RESTful的原则。例如,GET /user/1/xx验证信息xx。幸好的是HTTP协议本身定义了两种认证方式,Basic和Digest。
一、HTTP 基本认证(Basic Athorization)
I.简介
HTTP基本认证比较简单,明文发送,没有签名,安全性低,没作用域,只能适用于一般场合。
整个交互过程如下:
- 用户访问需要认证的页面
- 服务端验证失败,响应401状态码,并响应WWW-Authenticate报头
- 客户端收到WWW-Authenticate报头,表示要提供认证信息
- 客户端将用户名和密码使用:号连接,并base64编码后方在报头里发送回服务端
例子:
1.客户端发起请求,无发送认证信息:
GET /private/index.html HTTP/1.0
Host: localhost
2.服务端响应
HTTP/1.0 401 Authorization Required
Server: HTTPd/1.0
Date: Sat, 27 Nov 2004 10:18:15 GMT
WWW-Authenticate: Basic realm="Secure Area"
Content-Type: text/html
Content-Length: 311
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML>
<HEAD>
<TITLE>Error</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
</HEAD>
<BODY><H1>401 Unauthorized.</H1></BODY>
</HTML>
3.客户端发起有认证信息的请求
GET /private/index.html HTTP/1.0
Host: localhost
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
4.服务端响应
HTTP/1.0 200 OK
Server: HTTPd/1.0
Date: Sat, 27 Nov 2004 10:19:07 GMT
Content-Type: text/html
Content-Length: 10476
II.服务端的实现
认证失败响应
header('WWW-Authenticate: Basic realm="Test"');
header('HTTP/1.1 401 Unauthorized');
客户端发送过来的用户名和密码通过 $_SERVER['PHP_AUTH_USER'] 和 $_SERVER['PHP_AUTH_PW'] 获得
III.客户端的使用
1.PHP/cURL
只需两个设置就可以实现HTTP 基本认证。
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
2.Javascript/AJAX
XMLHttpRequest对象的open方法提供了设置参数
xhr.open("GET", "/", true, "username", "password")
如果使用jQuery的话,用$.ajax方法
$.ajax({username : "username", password : "password"})
3.Linux/cURL
命令行下使用cURL发起请求
curl -u username:password http://localhost/
参考资料:
HTTP Authentication: Basic and Digest Access Authentication
Digest access authentication
Basic access authentication
Form形式的HTTP Basic Authentication
分享到:
相关推荐
使用Java创建RESTful Web Service - ImportNew1
提供restful风格的框架, 拥有客户端和服务端两部分, 具体实现功能代码需要自己添加。
Java-RESTful-Web-Service.rar
Building-RESTful-Python-Web-Services, 构建 RESTful python Web服务的代码库,由Packt发布 #Building RESTful python Web服务 这是构建 RESTful python Web服务 插件的代码库,由Packt发布。 它包含了从开始到结束...
使用RESTful Web Service实现转账业务,包括事务处理和并发控制
NULL 博文链接:https://liangyingying1117.iteye.com/blog/2155855
instrumented-restful-fast-request-pro-2023.1.7.1 可用版,亲测,可用。
laravel-restful-api-starter, 使用 Laravel 和MongoDB构建一个 RESTful API #Laravel Restful API启动程序现在查看一下 WIKI !欢迎捐赠对于代码和 doc: ) !是什么?这里启动器基于以下功能:Laravel 4.2: 使用轻...
RESTful Web APIs - Richardson, Leonard & Amundsen, Mike & Ruby, Sam.mobi
RESTful Web 服务 - 快速指南
Metamorphic-Testing-of-RESTful-Web-APIs.pdf
大神的Restful Web Service 详细介绍,Objectives By the end of this class, you should be able to: Give a working definition of RESTful Web Services actions, Enable a RESTful user in the SM operator ...
Building-RESTful-Python-Web-Services.pdf
Restful-Web-API-Design-with-Node.Js-10_3rd, 最新版Node JS10 开发Restful Web API技术,书籍是英文版的,不过上面demo比较多基本都能看懂。 本资源仅供学习参考,如有侵权,请联系上传者删除。
Java SpringBoot 课程 restful crud 实验资源 restful 风格的增删改查
RESTFUL的实现-springboot2.0版本 网址:https://blog.csdn.net/chenwewi520feng/article/details/130634490 使用一个示例来说明restful的使用,是以springboot的2.0版本作为基础的
它有一个漂亮的界面来完成请求、检查服务器响应、存储你的API请求和导出API请求,该插件能帮助你在IDEA内更快更高效地调试API! 找到插件的安装目录如:C:\Users\work_\AppData\Roaming\JetBrains\IntelliJIdea2023....
Developing-Restful-Web-Services-with-Jersey-2-0
Building a RESTful Web Service with Spring 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权...