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

本文涉及的产品
密钥管理服务KMS,1000个密钥,100个凭据,1个月
简介: Java【算法 04】HTTP的认证方式之DIGEST认证详细流程说明及举例

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

1.是什么

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

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

2.认值流程

类似与Basic认证流程:

Digest认证的整体流程如下:

  1. 客户端发送请求: 客户端向服务器发送请求,请求中包含需要访问的资源路径。
  2. 服务器返回挑战信息: 服务器接收到客户端请求后,返回一个“质询”信息(Challenge)给客户端。这个挑战信息是一个包含随机数、领域名(realm)以及其他一些参数的字符串。
  3. 客户端生成响应: 客户端使用用户名、密码和挑战信息来生成一个响应字符串。这个响应字符串的生成过程包括以下几个步骤:
  • 拼接:将用户名、领域名和密码用冒号分隔,并将它们拼接成一个字符串。
  • 对字符串进行哈希:对上述拼接后的字符串进行哈希运算,通常使用MD5或SHA-1等哈希算法。
  1. 客户端发送响应: 客户端将生成的响应字符串发送给服务器,放在请求的"Authorization"头部中。
  2. 服务器验证响应: 服务器收到客户端的响应后,使用相同的方式在服务器端重现生成响应字符串。然后将客户端发送的响应字符串和服务器端生成的响应字符串进行比较。如果两者相等,说明客户端拥有正确的用户名和密码。
  3. 服务器返回响应: 如果服务器验证成功,它会返回请求的资源内容给客户端,同时在响应的头部中包含认证成功的标识。

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);
目录
相关文章
|
1月前
|
存储 算法 Java
解锁“分享文件”高效密码:探秘 Java 二叉搜索树算法
在信息爆炸的时代,文件分享至关重要。二叉搜索树(BST)以其高效的查找性能,为文件分享优化提供了新路径。本文聚焦Java环境下BST的应用,介绍其基础结构、实现示例及进阶优化。BST通过有序节点快速定位文件,结合自平衡树、多线程和权限管理,大幅提升文件分享效率与安全性。代码示例展示了文件插入与查找的基本操作,适用于大规模并发场景,确保分享过程流畅高效。掌握BST算法,助力文件分享创新发展。
|
2月前
|
存储 人工智能 算法
解锁分布式文件分享的 Java 一致性哈希算法密码
在数字化时代,文件分享成为信息传播与协同办公的关键环节。本文深入探讨基于Java的一致性哈希算法,该算法通过引入虚拟节点和环形哈希空间,解决了传统哈希算法在分布式存储中的“哈希雪崩”问题,确保文件分配稳定高效。文章还展示了Java实现代码,并展望了其在未来文件分享技术中的应用前景,如结合AI优化节点布局和区块链增强数据安全。
|
2月前
|
算法 安全 Java
Java线程调度揭秘:从算法到策略,让你面试稳赢!
在社招面试中,关于线程调度和同步的相关问题常常让人感到棘手。今天,我们将深入解析Java中的线程调度算法、调度策略,探讨线程调度器、时间分片的工作原理,并带你了解常见的线程同步方法。让我们一起破解这些面试难题,提升你的Java并发编程技能!
98 16
|
2月前
|
算法 Java C++
【潜意识Java】蓝桥杯算法有关的动态规划求解背包问题
本文介绍了经典的0/1背包问题及其动态规划解法。
60 5
|
2月前
|
算法 搜索推荐 Java
【潜意识Java】深度解析黑马项目《苍穹外卖》与蓝桥杯算法的结合问题
本文探讨了如何将算法学习与实际项目相结合,以提升编程竞赛中的解题能力。通过《苍穹外卖》项目,介绍了订单配送路径规划(基于动态规划解决旅行商问题)和商品推荐系统(基于贪心算法)。这些实例不仅展示了算法在实际业务中的应用,还帮助读者更好地准备蓝桥杯等编程竞赛。结合具体代码实现和解析,文章详细说明了如何运用算法优化项目功能,提高解决问题的能力。
83 6
|
2月前
|
人工智能 算法 搜索推荐
算法备案全流程攻略:保姆级教程
在AI热潮下,算法成为互联网服务的核心驱动力,但也带来了大数据杀熟、算法歧视等问题。为规范行业发展,算法备案制度应运而生。该制度涵盖网站、APP等多种产品形式,要求企业在2个月内完成备案,依据《互联网信息服务算法推荐管理规定》等法规。未备案企业可能面临无法上线、罚款甚至刑罚的后果。备案流程包括注册、主体备案、信息填报及审核,确保算法合规运营。通过悬挂备案号、标识AI生成内容和定期自查,企业需持续维护算法安全与合规。
|
2月前
|
存储 监控 算法
剖析基于Java算法驱动的智能局域网管控之道
本文探讨了基于Java语言的局域网控制方案,结合链表数据结构与令牌桶算法,解决设备管理和流量调度难题。通过链表灵活存储网络设备信息,实现高效设备管理;令牌桶算法则精准控制流量,确保网络平稳运行。二者相辅相成,为校园、企业等局域网提供稳固高效的控制体系,保障业务连续性和数据安全。
|
2月前
|
存储 监控 算法
探秘局域网桌面监控:深入剖析 Java 语言核心算法
在数字化办公时代,局域网桌面监控如同企业的“智慧鹰眼”,确保工作效率与数据安全。本文以Java为载体,揭示哈希表在监控中的关键应用。通过高效的数据结构和算法,哈希表能快速索引设备连接信息,大幅提升监控的时效性和响应速度。代码示例展示了如何用Java实现设备网络连接监控,结合未来技术如AI、大数据,展望更智能的监控体系,助力企业在数字化浪潮中稳健前行。
|
2月前
|
运维 监控 算法
企业局域网监控软件中 Java 优先队列算法的核心优势
企业局域网监控软件是数字化时代企业网络安全与高效运营的基石,犹如一位洞察秋毫的卫士。通过Java实现的优先队列算法,它能依据事件优先级排序,确保关键网络事件如异常流量、数据泄露等被优先处理,保障系统稳定与安全。代码示例展示了如何定义网络事件类并使用PriorityQueue处理高优先级事件,尤其在面对疑似风险时迅速启动应急措施。这一核心技术助力企业在复杂网络环境中稳健前行,护航业务腾飞。
69 32
|
3月前
|
监控 算法 网络协议
Java 实现局域网电脑屏幕监控算法揭秘
在数字化办公环境中,局域网电脑屏幕监控至关重要。本文介绍用Java实现这一功能的算法,涵盖图像采集、数据传输和监控端显示三个关键环节。通过Java的AWT/Swing库和Robot类抓取屏幕图像,使用Socket进行TCP/IP通信传输图像数据,并利用ImageIO类在监控端展示图像。整个过程确保高效、实时和准确,为提升数字化管理提供了技术基础。
95 15

热门文章

最新文章