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

这篇具有很好参考价值的文章主要介绍了Java【算法 04】HTTP的认证方式之DIGEST认证详细流程说明及举例。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

1.是什么

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

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

2.认值流程

类似与Basic认证流程:

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

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"

完整算法,这里我们要非常注意带不带双引号:文章来源地址https://www.toymoban.com/news/detail-641590.html

// 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);

到了这里,关于Java【算法 04】HTTP的认证方式之DIGEST认证详细流程说明及举例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ES的两种认证登录方式: JAVA REST Client/HTTP Client

    通过RestHighLevelClient发送的es请求验证方式: 通过http直接发送的es请求认证方式:

    2024年02月16日
    浏览(25)
  • 深度学习之目标检测Fast-RCNN模型算法流程详解说明(超详细理论篇)

    1.Fast-RCNN论文背景 2. Fast-RCNN算法流程 3.Fast R-CNN 问题和缺点 这篇以对比RCNN来说明,如果你对RCNN网络没太熟悉,可访问这链接,快速了解,点下面链接 深度学习之目标检测R-CNN模型算法流程详解说明(超详细理论篇) 论文地址https://arxiv.org/abs/1504.08083   Fast R-CNN 是一篇由R

    2024年02月11日
    浏览(21)
  • 深度学习之目标检测R-CNN模型算法流程详解说明(超详细理论篇)

    1.R-CNN论文背景 2. R-CNN算法流程 3. R-CNN创新点 论文网址https://openaccess.thecvf.com/content_cvpr_2014/papers/Girshick_Rich_Feature_Hierarchies_2014_CVPR_paper.pdf   RCNN(Region-based Convolutional Neural Networks)是一篇由Ross Girshick等人于2014年发表的论文,题为《Rich feature hierarchies for accurate object detection

    2024年02月11日
    浏览(55)
  • HTTP 认证方式

    本文主要介绍HTTP 认证方式有哪些及在Python中的基本使用方式。 HTTP认证方式主要有以下几种: 基本认证(Basic Authentication):客户端将用户名和密码进行Base64编码后发送给服务器,服务器解码后进行验证。这种方式简单但不安全,因为Base64编码是可逆的。 摘要认证(Digest

    2024年01月21日
    浏览(21)
  • Qt http 的认证方式以及简单实现

    基本认证(Basic Authentication) : 基本认证是最简单的HTTP认证方式。客户端在请求头中使用Base64编码的用户名和密码进行身份验证 由于仅使用Base64编码,基本认证并不安全,因此建议与HTTPS一起使用,以加密传输 具体详细讲解:https://www.cnblogs.com/xy-ouyang/p/12600055.html https://datatr

    2024年02月11日
    浏览(23)
  • 【前车之鉴】: 2023最新教程-将java程序打包到maven私服的正确打开方式,详细流程介绍不怕你掌握不了

    一是当前网络上一些博客有遗漏部分,这里做补充,二是网上思路没错,但是有些关键配置maven官方做了变更,其三:更详细,更面向初次使用的developer 上图所示的步骤基本就是本文的框架,其实打包逻辑比较清晰,仅仅是过程复杂,但有过公司开发经历的同学应该能get到这

    2024年02月11日
    浏览(28)
  • http请求和响应格式说明,http的get和post请求方式说明,http的请求体body的几种数据格式

    一个HTTP请求报文由 请求行(request line)、请求头部(header)、空行和请求数据 4个部分组成, 请求报文的一般格式 1、第一行必须是一个请求行(request-line),用来说明请求类型,要访问的资源以及所使用的HTTP版本 2、紧接着是一个请求头(header),用来说明服务器要使用的附加信息

    2024年02月02日
    浏览(39)
  • SpringSecurity认证流程(超级详细)

    最近开发项目的时候遇到了和SpringSecurity相关的一些问题,但是之前并没有去了解过SpringSecurity,导致改系统安全权限验证的时候就比较吃力了,目前项目开发大多都直接用脚手架直接开发,系统安全权限验证已经形成了,所以并不是自己写的,自己理解起来会更慢一些,所以

    2024年02月07日
    浏览(39)
  • HTTPS的认证和加密流程,超详细!!

    1、服务器生成自己的 私钥A 和 公钥A 2、服务器将 公钥A 和服务器域名等相关信息,发送给认证机构 3、认证机构生成自己的 私钥B 和 公钥B ,并用自己的 私钥B 对服务器发送来的信息进行加密, 生成服务器A的证书 ,发送给服务器 4、认证机构将自己的 公钥B 发给的浏览器

    2024年02月13日
    浏览(26)
  • 腾讯云网站备案详细流程_审核时间说明

    腾讯云网站备案流程先填写基础信息、主体信息和网站信息,然后提交备案后等待腾讯云初审,初审通过后进行短信核验,最后等待各省管局审核 ,前面腾讯云初审时间1到2天左右,最长时间是等待管局审核时间,网站备案地区不同管局审核时间也不同,快的3天即可通过审核

    2024年02月10日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包