Java【算法 04】HTTP的認證方式之DIGEST認證詳細流程說明及舉例

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 UnauthorizedWWW-Authenticate: Digestrealm="http-auth@example.org",qop="auth, auth-int",algorithm=SHA-256,nonce="7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v",opaque="FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS"WWW-Authenticate: Digestrealm="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 UnauthorizedWWW-Authenticate: Digestrealm="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)
andKD(secret, data) = H(concat(secret, ":", data))For example:For the "SHA-256" and "SHA-256-sess" algorithmsH(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: Digestrealm="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.pswp.cn/news/38282.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/38282.shtml
英文地址,請注明出處:http://en.pswp.cn/news/38282.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

CSS3 中新增了哪些常見的特性?

聚沙成塔每天進步一點點 ? 專欄簡介? 圓角&#xff08;Border Radius&#xff09;? 漸變&#xff08;Gradients&#xff09;? 陰影&#xff08;Box Shadow&#xff09;? 文本陰影&#xff08;Text Shadow&#xff09;? 透明度&#xff08;Opacity&#xff09;? 過渡&…

Spring boot與Spring cloud 之間的關系

Spring boot與Spring cloud 之間的關系 Spring boot 是 Spring 的一套快速配置腳手架&#xff0c;可以基于spring boot 快速開發單個微服務&#xff0c;Spring Boot&#xff0c;看名字就知道是Spring的引導&#xff0c;就是用于啟動Spring的&#xff0c;使得Spring的學習和使用…

MATLAB中xlsread函數用法

目錄 語法 說明 示例 將工作表讀取到數值矩陣 讀取元胞的范圍 讀取列 請求數值、文本和原始數據 對工作表執行函數 請求自定義輸出 局限性 xlsread函數的功能是讀取Microsoft Excel 電子表格文件 語法 num xlsread(filename) num xlsread(filename,sheet) num x…

Nacos和GateWay路由轉發NotFoundException: 503 SERVICE_UNAVAILABLE “Unable to find

問題再現&#xff1a; 2023-08-15 16:51:16,151 DEBUG [reactor-http-nio-2][CompositeLog.java:147] - [dc73b32c-1] Encoding [{timestampTue Aug 15 16:51:16 CST 2023, path/content/course/list, status503, errorService Unavai (truncated)...] 2023-08-15 16:51:16,17…

leetcode27—移除元素

思路&#xff1a; 參考26題目雙指針的思想&#xff0c;只不過這道題不是快慢指針。 看到示例里面數組是無序的&#xff0c;也就是說后面的元素也是可能跟給定 val值相等的&#xff0c;那么怎么處理呢。就想到了從前往后遍歷&#xff0c;如果left對應的元素 val時&#xff0c…

汽車制造業上下游協作時 外發數據如何防泄露?

數據文件是制造業企業的核心競爭力&#xff0c;一旦發生數據外泄&#xff0c;就會給企業造成經濟損失&#xff0c;嚴重的&#xff0c;可能會帶來知識產權剽竊損害、名譽傷害等。汽車制造業&#xff0c;會涉及到重要的汽車設計圖紙&#xff0c;像小米發送汽車設計圖紙外泄事件并…

[足式機器人]Part5 機械設計 Ch00/01 緒論+機器結構組成與連接 ——【課程筆記】

本文僅供學習使用 本文參考&#xff1a; 《機械設計》 王德倫 馬雅麗課件與日常作業可登錄網址 http://edu.bell-lab.com/manage/#/login&#xff0c;選擇觀摩登錄&#xff0c;查看2023機械設計2。 機械設計-Ch00Ch01——緒論機器結構組成與連接 Ch00-緒論0.1 何為機械設計——…

12.Eclipse導入Javaweb項目

同事復制一份他的項目給我ekp.rar (懶得從SVN上拉取代碼了)放在workspace1目錄下 新建一個文件夾 workspace2&#xff0c;Eclipse切換到workspace2工作空間 選擇Import導入 選擇導入的項目(這里是放到workspace1里面) 拷貝一份到workspace2里面 例子 所有不是在自己電腦上開發…

可白嫖的4家免費CDN,并測試其網絡加速情況(2023版)

網站加載速度優化過程中&#xff0c;不可避免的會用上CDN來加速資源的請求速度。但是市面上的CDN資源幾乎都是要收費的&#xff0c;而且價格還不便宜&#xff0c;對于小公司站長來講&#xff0c;這將是一筆不小的開銷。不過還是有一些良心公司給我們提供了免費的資源&#xff0…

ZooKeeper的基本概念

集群角色 通常在分布式系統中&#xff0c;構成一個集群的每一臺機器都有自己的角色&#xff0c;最典型的集群模式就是Master/Slave模式(主備模式)。在這種模式中&#xff0c;我們把能夠處理所有寫操作的機器稱為Master機器&#xff0c;把所有通過異步復制方式獲取最新數據&…

Redis_億級訪問量數據處理

11. 億級訪問量數據處理 11.1 場景表述 手機APP用戶登錄信息&#xff0c;一天用戶登錄ID或設備ID電商或者美團平臺&#xff0c;一個商品對應的評論文章對應的評論APP上有打卡信息網站上訪問量統計統計新增用戶第二天還留存商品評論的排序月活統計統計獨立訪客(Unique Vistito…

【BEV】3D視覺 PRELIMINARY

這里的知識來自于論文 Delving into the Devils of Bird’s-eye-view Perception: A Review, Evaluation and Recipe 的 Appendix B.1 部分來自 這篇文章 從透視圖轉向鳥瞰圖。&#xff08;Xw、Yw、Zw&#xff09;、&#xff08;Xc、Yc、Zc&#xff09;表示世界World坐標和相…

Android學習之路(4) UI控件之Button (按鈕)與 ImageButton (圖像按鈕)

本節引言&#xff1a; 今天給大家介紹的Android基本控件中的兩個按鈕控件&#xff0c;Button普通按鈕和ImageButton圖像按鈕&#xff1b; 其實ImageButton和Button的用法基本類似&#xff0c;至于與圖片相關的則和后面ImageView相同&#xff0c;所以本節 只對Button進行講解&am…

vue自定義穿梭框支持遠程滾動加載

分享-2023年資深前端進階&#xff1a;前端登頂之巔-最全面的前端知識點梳理總結&#xff0c;前端之巔 *分享一個使用比較久的&#x1fa9c; 技術框架公司的選型(老項目)&#xff1a;vue2 iview-ui 方案的實現思路是共性的&#xff0c;展現UI樣式需要你們自定義進行更改&#…

【注解使用】使用@Autowired后提示:Field injection is not recommended(Spring團隊不推薦使用Field注入)

問題發生場景&#xff1a; 在使用 IDEA 開發 SpringBoot 項目時&#xff0c;在 Controller 類中使用注解 Autowired 注入一個依賴出現了警告提示&#xff0c;查看其他使用該注解的地方同樣出現了警告提示。這是怎么回事&#xff1f;由于先去使用了SpringBoot并沒有對Spring進行…

分布式 - 消息隊列Kafka:Kafka消費者和消費者組

文章目錄 1. Kafka 消費者是什么&#xff1f;2. Kafka 消費者組的概念&#xff1f;3. Kafka 消費者和消費者組有什么關系&#xff1f;4. Kafka 多個消費者如何同時消費一個分區&#xff1f; 1. Kafka 消費者是什么&#xff1f; 消費者負責訂閱Kafka中的主題&#xff0c;并且從…

【數據結構OJ題】反轉鏈表

原題鏈接&#xff1a;https://leetcode.cn/problems/reverse-linked-list/description/ 目錄 1. 題目描述 2. 思路分析 3. 代碼實現 1. 題目描述 2. 思路分析 方法一&#xff1a;三指針翻轉法 使用三個結構體指針n1&#xff0c;n2&#xff0c;n3&#xff0c;原地修改結點…

VSCode如何設置高亮

一、概述 本文主要介紹在 VSCode 看代碼時&#xff0c;怎樣使某個單詞高亮顯示&#xff0c;主要通過以下三步實現&#xff1a; 安裝 highlight-words 插件 配置 highlight-words 插件 設置高亮快捷鍵F8 工作是嵌入式開發的&#xff0c;代碼主要是C/C的&#xff0c;之前一直用…

【Linux】高級IO

目錄 IO的基本概念 釣魚五人組 五種IO模型 高級IO重要概念 同步通信 VS 異步通信 阻塞 VS 非阻塞 其他高級IO 阻塞IO 非阻塞IO IO的基本概念 什么是IO&#xff1f; I/O&#xff08;input/output&#xff09;也就是輸入和輸出&#xff0c;在著名的馮諾依曼體系結構當中…

ROS學習筆記(三)---好用的終端Terminator

ROS學習筆記文章目錄 01. ROS學習筆記(一)—Linux安裝VScode 02. ROS學習筆記(二)—使用 VScode 開發 ROS 的Python程序&#xff08;簡例&#xff09; 一、Terminator是什么&#xff1f; 在前面的學習中&#xff0c;為了運行hello.py我是在vscode頻繁的點擊運行窗口的“”號…