Java【算法 04】HTTP的认证方式之DIGEST认证详细流程说明及举例

news2025/4/24 17:36:28

HTTP的认证方式之DIGEST

  • 1.是什么
  • 2.认值流程
    • 2.1 客户端发送请求
    • 2.2 服务器返回质询信息
      • 2.2.1 质询参数
      • 2.2.2 质询举例
    • 2.3 客户端生成响应
    • 2.4 服务器验证响应
    • 2.5 服务器返回响应
  • 3.算法
    • 3.1 SHA-256
      • 3.1.1 Response
      • 3.1.2 A1
      • 3.1.3 A2
    • 3.2 MD5
      • 3.2.1 Request-Digest
      • 3.2.2 A1
      • 3.2.3 A2
  • 4.举例

详细的说明文档:WWW-Authenticate - HTTP | MDN (mozilla.org)

1.是什么

摘要认证(Digest Authentication)是一种用于在网络通信中验证用户身份的认证方法。它主要应用于HTTP和其他应用层协议中。

Digest认证相对于基本认证更加安全,因为它不直接传输明文密码。但它也不是完全的安全解决方案,因为在中间人攻击等情况下,仍然可能受到攻击。在现代网络中,更安全的认证方法通常是基于令牌(Token)的认证机制。

2.认值流程

类似与Basic认证流程:

在这里插入图片描述

Digest认证的整体流程如下:

  1. 客户端发送请求: 客户端向服务器发送请求,请求中包含需要访问的资源路径。

  2. 服务器返回挑战信息: 服务器接收到客户端请求后,返回一个“质询”信息(Challenge)给客户端。这个挑战信息是一个包含随机数、领域名(realm)以及其他一些参数的字符串。

  3. 客户端生成响应: 客户端使用用户名、密码和挑战信息来生成一个响应字符串。这个响应字符串的生成过程包括以下几个步骤:

    • 拼接:将用户名、领域名和密码用冒号分隔,并将它们拼接成一个字符串。
    • 对字符串进行哈希:对上述拼接后的字符串进行哈希运算,通常使用MD5或SHA-1等哈希算法。
  4. 客户端发送响应: 客户端将生成的响应字符串发送给服务器,放在请求的"Authorization"头部中。

  5. 服务器验证响应: 服务器收到客户端的响应后,使用相同的方式在服务器端重现生成响应字符串。然后将客户端发送的响应字符串和服务器端生成的响应字符串进行比较。如果两者相等,说明客户端拥有正确的用户名和密码。

  6. 服务器返回响应: 如果服务器验证成功,它会返回请求的资源内容给客户端,同时在响应的头部中包含认证成功的标识。

2.1 客户端发送请求

客户端的第一次请求。

2.2 服务器返回质询信息

2.2.1 质询参数

  • qop:带引号的字符串,表示服务器支持的保护程度。这必须提供,并且必须忽略无法识别的选项。

    • "auth":身份验证
    • "auth-int":有完整保护的身份验证
  • nonce:一个服务器指定的带引号的字符串,在每次的 401 响应期间,服务器可以使用它去验证指定的凭据。这必须是在每次 401 响应时唯一的生成,并且可以更频繁地重新生成(例如,允许一个摘要仅使用一次)。该规范包含有关生成此值算法的建议。nonce 值对客户端是不透明的。

  • opaque:一个服务器指定的带引号的字符串,应在 Authorization 中原封不动的返回。这对客户端是不透明的。建议服务器包含 Base64 或十六进制数据。

  • <realm>(可选)一个指示要使用的用户名/密码的字符串。至少应该包括主机名,但是可能指示具有访问权限的用户或组。

  • domain(可选)一个带引号,以空格分隔的 URI 前缀列表,定义了可以使用身份验证信息的所有位置。如果未指定此关键字,则可以在 web 根目录的任意位置使用身份验证信息。

  • stale(可选)一个不区分大小写的标志,指示客户端之前的请求因 nonce 太旧了(过期)而被拒绝。如果为 true,则可以使用新的 nonce 加密相同用户名/密码重试请求。如果它是任意其他的值,那么用户名/密码无效,并且必须向用户重新请求。

  • algorithm(可选)algorithm 被用于产生一个摘要。有效的非会话值是:"MD5"(如果未指定,则是默认)、"SHA-256""SHA-512"。有效的会话值是:"MD5-sess""SHA-256-sess""SHA-512-sess"

  • charset="UTF-8"(可选)当提交用户名和密码时,告诉客户端服务器的首选编码方案。仅允许的值是不区分大小写的“UTF-8”字符串。

  • userhash(可选)服务器可能指定为 "true",以指示它支持用户名哈希(默认是 "false")。

2.2.2 质询举例

客户端试图访问http://www.example.org/dir/index.html处的文档,该文档受到 digest 身份验证的保护。这个文档的用户名是“Mufsas”,并且它的密码是“Circle of Life”。客户端第一次请求该文档时,不会发送 Authorization 标头字段。在这里,服务器使用 HTTP 401 消息响应,其中包括对它支持的每个摘要算法的质询,按照其优先顺序(SHA256,然后是 MD5)。

服务器将质询信息放在WWW-Authenticate响应头发送给客户端,如下例子:

HTTP/1.1 401 Unauthorized

WWW-Authenticate: Digest
    realm="http-auth@example.org",
    qop="auth, auth-int",
    algorithm=SHA-256,
    nonce="7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v",
    opaque="FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS"

WWW-Authenticate: Digest
    realm="http-auth@example.org",
    qop="auth, auth-int",
    algorithm=MD5,
    nonce="7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v",
    opaque="FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS"

2.3 客户端生成响应

客户端接收到401响应,表示需要进行认证,客户端提示用户输入他们的用户名和密码,然后响应一个新的请求,该请求在 Authorization 标头字段中对凭据进行加密。如果客户端选择 MD5 摘要,则 Authorization 标头字段看起来可能像如下这样:

Authorization: Digest username="Mufasa",
    realm="http-auth@example.org",
    uri="/dir/index.html",
    algorithm=MD5,
    nonce="7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v",
    nc=00000001,
    cnonce="f2/wE4q74E6zIJEtWaHKaf5wv/H5QzzpXusqGemxURZJ",
    qop=auth,
    response="8ca523f5e9506fed4657c9700eebdbec",
    opaque="FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS"

如果客户端选择 SHA-256 摘要,则 Authorization 标头看起来可能像以下这样:

Authorization: Digest username="Mufasa",
    realm="http-auth@example.org",
    uri="/dir/index.html",
    algorithm=SHA-256,
    nonce="7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v",
    nc=00000001,
    cnonce="f2/wE4q74E6zIJEtWaHKaf5wv/H5QzzpXusqGemxURZJ",
    qop=auth,
    response="753927fa0e85d155564e2e272a28d1802ca10daf4496794697cf8db5856cb6c1",
    opaque="FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS"

2.4 服务器验证响应

服务器收到客户端的响应后,使用相同的方式在服务器端重现生成响应字符串。然后将客户端发送的响应字符串和服务器端生成的响应字符串进行比较。如果两者相等,说明客户端拥有正确的用户名和密码。

2.5 服务器返回响应

如果服务器验证成功,它会返回请求的资源内容给客户端,同时在响应的头部中包含认证成功的标识。

3.算法

根据请求体里的algorithm的值:

HTTP/1.1 401 Unauthorized

WWW-Authenticate: Digest
    realm="http-auth@example.org",
    qop="auth, auth-int",
    algorithm=SHA-256,
    nonce="7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v",
    opaque="FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS"

一下是标准文档里的说明,来自RFC 7616: HTTP Digest Access Authentication (rfc-editor.org):

A string indicating an algorithm used to produce the digest and an unkeyed digest. If this is not present, it is assumed to be “MD5”. If the algorithm is not understood, the challenge SHOULD be ignored (and a different one used, if there is more than one). When used with the Digest mechanism, each one of the algorithms has two variants: Session variant and non-Session variant. The non-Session variant is denoted by “”, e.g., “SHA-256”, and the Session variant is denoted by “-sess”, e.g., “SHA-256-sess”.

In this document, the string obtained by applying the digest algorithm to the data “data” with secret “secret” will be denoted by KD(secret, data), and the string obtained by applying theunkeyed digest algorithm to the data “data” will be denoted H(data). KD stands for Keyed Digest, and the notation unq(X) means the value of the quoted-string X without the surrounding quotes and with quoting slashes removed.

For "<algorithm>" and "<algorithm>-sess"
	H(data) = <algorithm>(data)
and
	KD(secret, data) = H(concat(secret, ":", data))

For example:

For the "SHA-256" and "SHA-256-sess" algorithms
	H(data) = SHA-256(data)
	
For the "MD5" and "MD5-sess" algorithms 
	H(data) = MD5(data)

i.e., the digest is the “” of the secret concatenated with a colon concatenated with the data. The “-sess” is intended to allow efficient third-party authentication servers; for the difference in usage, see the description in Section 3.4.2.

简单进行一下解释:

  • KD(secret,data)是将secretdata用冒号:拼接之后secret:data进行算法加密
  • H(data)是直接对数据进行算法加密
  • unq(username)是不带引号的字符串

3.1 SHA-256

我们可以参考RFC 7616: HTTP Digest Access Authentication (rfc-editor.org)

3.1.1 Response

If the qop value is “auth” or “auth-int”:

response = KD(H(A1),unq(nonce):nc:unq(cnonce):unq(qop):H(A2))

See below for the definitions for A1 and A2.

3.1.2 A1

If the algorithm parameter’s value is “”, e.g., “SHA-256”,then A1 is:

A1 = unq(username):unq(realm):passwd
where passwd = < user's password >

If the algorithm parameter’s value is “-sess”, e.g., “SHA-256-sess”, then A1 is calculated using the nonce value provided in the challenge from the server, and cnonce value from the request by the client following receipt of a WWW-Authenticate challenge from the server. It uses the server nonce from that challenge, herein called nonce-prime, and the client nonce value from the response, herein called cnonce-prime, to construct A1 as follows:

A1 = H(unq(username):unq(realm):passwd):unq(nonce-prime):unq(cnonce-prime)

This creates a “session key” for the authentication of subsequent requests and responses that is different for each “authentication session”, thus limiting the amount of material hashed with any one key. (Note: see further discussion of the authentication session in Section 3.6.) Because the server needs only use the hash of the user credentials in order to create the A1 value, this construction could be used in conjunction with a third-party authentication service so that the web server would not need the actual password value. The specification of such a protocol is beyond the scope of this specification.

3.1.3 A2

If the qop parameter’s value is “auth” or is unspecified, then A2 is:

A2 = Method:request-uri

If the qop value is “auth-int”, then A2 is:

A2 = Method:request-uri:H(entity-body)

3.2 MD5

我们可以参考RFC 2617 - HTTP Authentication: Basic and Digest Access Authentication (ietf.org)

3.2.1 Request-Digest

If the “qop” value is “auth” or “auth-int”:

request-digest = KD(H(A1), unq(nonce-value):nc-value:unq(cnonce-value):unq(qop-value):H(A2))

If the “qop” directive is not present (this construction is for compatibility with RFC 2069):

request-digest = KD(H(A1), unq(nonce-value):H(A2))

See below for the definitions for A1 and A2.

3.2.2 A1

If the “algorithm” directive’s value is “MD5” or is unspecified, then A1 is:

A1 = unq(username-value):unq(realm-value):passwd
where passwd = < user's password >

If the “algorithm” directive’s value is “MD5-sess”, then A1 is calculated only once - on the first request by the client following receipt of a WWW-Authenticate challenge from the server. It uses the server nonce from that challenge, and the first client nonce value to construct A1 as follows:

A1 = H(unq(username-value):unq(realm-value):passwd):unq(nonce-value):unq(cnonce-value)

This creates a ‘session key’ for the authentication of subsequent requests and responses which is different for each “authentication session”, thus limiting the amount of material hashed with any one key. (Note: see further discussion of the authentication session in section 3.3.) Because the server need only use the hash of the user credentials in order to create the A1 value, this construction could be used in conjunction with a third party authentication service so that the web server would not need the actual password value. The specification of such a protocol is beyond the scope of this specification.

3.2.3 A2

If the “qop” directive’s value is “auth” or is unspecified, then A2 is:

A2 = Method:digest-uri-value  

If the “qop” value is “auth-int”, then A2 is:

A2 = Method:digest-uri-value:H(entity-body)

4.举例

我们还拿上边的例子进行一下算法处理,algorithm没有赋值就是默认MD5,用户名还是使用Mufasa,密码使用123456

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Digest
    realm="http-auth@example.org",
    qop="auth",
    nonce="7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v",
    opaque="FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS"

响应:

Authorization: Digest username="Mufasa", 
	realm="http-auth@example.org", 
	uri="/dir/index.html", 
	algorithm=MD5, 
	nonce="7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v", 
	nc=00000001, 
	cnonce="nvlfh1ra", 
	qop=auth, 
	response="7bddc3c7fceb317dc002c524187fa170", 
	opaque="FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS"

完整算法,这里我们要非常注意带不带双引号:

// A1 = unq(username-value):unq(realm-value):passwd
String A1 = StrUtil.format("{}:{}:\"{}\"", "Mufasa", "http-auth@example.org", "123456");
// A2 = Method:digest-uri-value
String A2 = StrUtil.format("\"{}\":\"{}\"", "POST", "/dir/index.html");
// request-digest = KD(H(A1), unq(nonce-value):nc-value:unq(cnonce-value):unq(qop-value):H(A2))
String HA1 = SecureUtil.md5(A1);
String HA2 = SecureUtil.md5(A2);
String responseStr = StrUtil.format("\"{}\":{}:\"{}\":{}:{}:\"{}\"", HA1, "7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v","00000001", "nvlfh1ra", "auth", HA2);
String response = SecureUtil.md5(responseStr);

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/863678.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SQL | 过滤数据

4-过滤数据 4.1-使用WHERE子句 数据根据 WHERE 子句中指定的搜索条件进行过滤。WHERE 子句在表名&#xff08; FROM 子句&#xff09;之后给出。 select prod_name,prod_price from products where prod_price 3.49; 上述语句查询价格为3.49的行&#xff0c;然后输出名字和…

多路复用select实现

select函数 int select(int nfds, fd_set *readfds,fd_set *writefds,fd_set *exceptfds, struct timeval *timeout);nfds:是三个集合中编号最高的文件描述符&#xff0c;加上1readfds/writefds/exceptfds: 可读集合/可写集合/异常集合timeout NULL&#xff1a;永久阻塞0&…

JS 解决鼠标悬浮显示弹窗 迅速离开时弹窗显示到其他位置的延迟问题

解决该问题的思路就是&#xff0c;判断当前鼠标的位置是否在某个div上&#xff0c;如果在这个div上则取消显示悬浮弹窗消息。 首先监听鼠标的移动事件 鼠标移动时判断是否在div里面进行移动了 clientX表示鼠标X的位置 client Y表示鼠标Y的位置 拿到要判断的div元素 获取off…

【Uni-App】uview 开发多端应用,密码显示隐藏功能不生效问题

出现的问题&#xff1a; 使用uview组件u-input框密码绑定时会出现右侧密码显隐图标不显示的问题 思路&#xff1a; 1.看了下uview源码&#xff0c;发现这有一段注释&#xff0c;我们需要把源码修改一下&#xff0c;问题出在这里 这行代码修改为 :password"password || …

spring按条件注入@Condition及springboot对其的扩展

概述 spring的ioc极大的方便了日常开发&#xff0c;但随着业务的迭代。配置的一些参数在某些情况下需要按条件注入。 比如原先定义的db公共模块下&#xff0c;相关的配置和工具类只是基于mysql的。但是后续有模块需要使用mongo/es等其他数据库&#xff0c;又想继续使用db公共…

七. 定时器

定时器 1. EPIT定时器(1) 特征&#xff1a;(2) 结构&#xff1a;(3) 工作模式&#xff1a;(4) 寄存器(5) 配置步骤(6) 定时器消抖 2. GPT 定时器(1) 特征(2) 结构(3) 工作模式(4) 寄存器<1> GPTx_CR 配置<2> GPTx_PR 分频<3> GPTx_SR 状态<4> GPTx_CNT…

Vue+SpringBoot后台管理系统:Vue3+TypeScript项目搭建(一)

写在开始:一个搬砖程序员的随缘记录文章目录 一、Node安装二、Vue CLI安装三、相关的版本四、创建Vue3TypeScript项目五、Vue项目初始化六、项目启动 一、Node安装 查看Note版本 node -v查看npm版本 npm -v然后将npm升级至最新版本 npm -g install npm将npm下载源换至http:…

分布式异步任务处理组件(八)

分布式异步任务组件网络通信线程模型设计-- 大概说一下功能场景&#xff1a; 从节点和主节点建立连接&#xff0c;负责和主节点的网络IO通信&#xff0c;通信动作包括投票&#xff0c;心跳&#xff0c;举证等&#xff0c;步骤为读取主节点的信息&#xff0c;写入IO队列中&…

yum 安装本地包 rpm

有时直接yum install 有几个包死活下不下来 根据网址&#xff0c;手动下载&#xff0c;下载后上传至 centos 然后运行 sudo yum localinstall xxx.rpm 即可安装 参考 https://blog.csdn.net/weiguang1017/article/details/52293244

【Nginx】Nginx网站服务

国外主流还是使用apache&#xff1b;国内现在主流是nginx&#xff08;并发能力强&#xff0c;相对稳定&#xff09; nginx&#xff1a;高新能、轻量级的web服务软件 特点&#xff1a; 1.稳定性高&#xff08;没apache稳&#xff09;&#xff1b; 2.系统资源消耗比较低&#xf…

40G光模块在高速网络中的关键角色

40G光模块在高速网络中扮演着关键的角色。它是用于传输数据的光纤通信设备&#xff0c;具备高速数据传输能力&#xff0c;广泛应用于数据中心、电信运营商、企业网络等领域。本文我们来看看40G光模块在高速网络中是如何发挥它的作用的&#xff01; 一、认识40G光模块 为实现4…

vue手写多对多关联图,连线用leader-line

效果如图 鼠标滑动效果 关联性效果 <template ><div class="main" ref="predecessor"><div class="search"><div class="search-item"><div class="search-item-label">部门</div><…

多用户一体化建设跨境电商小程序、app开发

跨境电商是指通过互联网技术&#xff0c;进行国际贸易的电子商务活动。随着跨境电商的快速发展&#xff0c;许多企业开始关注开发跨境电商小程序和app&#xff0c;以扩大其国际业务。下面是多用户一体化建设跨境电商小程序和app的开发步骤。 第一步&#xff1a;需求分析和规划…

tomcat的多实例,动静分离(web服务基础结束)

多实例 多实例就是在一台服务器上有多个tomcat的服务&#xff08;核心是改端口&#xff09; 实验&#xff1a;多实例 安装步骤 1.安装好 jdk 2.安装 tomcat cd /opt tar zxvf apache-tomcat-9.0.16.tar.gz mkdir /usr/local/tomcat mv apache-tomcat-9.0.16 /usr/local/tomca…

IDEA项目实践——Spring集成mybatis、spring当中的事务

系列文章目录 IDEA项目实践——创建Java项目以及创建Maven项目案例、使用数据库连接池创建项目简介 IDEWA项目实践——mybatis的一些基本原理以及案例 IDEA项目实践——动态SQL、关系映射、注解开发 IDEA项目实践——Spring框架简介&#xff0c;以及IOC注解 IDEA项目实践—…

python爬虫实战(1)——网站小说

整本小说的爬取保存 目标大致思路页面的爬取解析—XPath请求网页内容解析网页内容正文爬取与解析单个页面数据获取爬取所有页面 数据清洗 经过学习基础&#xff0c;我们学以致用一下子&#xff0c;爬取小说&#xff0c;注意这个小说本身是免费的哦&#xff0c;以后再进阶。 本次…

工作中的方法论总结

1、SMART SMART原则是目标管理的一种方法&#xff0c;通过有效地进行成员的组织与目标的制定和控制以达到更好的工作绩效。大到业务规划&#xff0c;小到个人项目开发计划都比较适用。 SMART的具体含义如下&#xff1a; S&#xff08;Specific&#xff09;&#xff1a;目标是确…

阿里发布财报,电商业务重回增长

KlipC报道&#xff1a;8月10日阿里巴巴集团发布了最新财报&#xff0c;据数据显示该公司一季度营收同比上涨14%&#xff0c;达2341.6亿元人民币&#xff0c;超市场预期。净利润为330亿元较去年同期增长63%达202.98亿元&#xff0c;财报发布后&#xff0c;阿里美股一度涨超5%&am…

动力节点|Spring6框架学习教程,从基础到手撕源码一套打通

Spring框架已广泛应用于诸多Java应用程序的开发中&#xff0c;它提供了很多解决方案及最佳实践&#xff0c;简化了Java应用程序的开发过程并加速了开发。 Spring6.0版本是下一个十年的新开端&#xff0c;动力节点老杜精心打造全新升级版Spring6教程&#xff0c;手把手教学&…

vue插槽slots

一、默认插槽&#xff1a; vue组件能够接收任意类型的 JavaScript 值作为 props&#xff0c;也可以为子组件传递一些模板片段&#xff0c;让子组件在它们的组件中渲染这些片段。 例如&#xff1a;有一个<FancyButton>组件 在父组件中引用 最终渲染出来的dom 插槽内容可…