HTTP Digest接入方式鉴权认证流程

article/2025/11/8 15:29:52

HTTP Digest接入方式鉴权认证流程

一、摘要认证原理

  摘要认证与基础认证的工作原理很相似,用户先发出一个没有认证证书的请求,Web服务器回复一个带有WWW-Authenticate头的响应,指明访问所请求的资源需要证书。但是和基础认证发送以Base 64编码的用户名和密码不同,在摘要认证中服务器让客户选一个随机数(称作”nonce“),然后浏览器使用一个单向的加密函数生成一个消息摘要(message digest),该摘要是关于用户名、密码、给定的nonce值、HTTP方法,以及所请求的URL。

二、摘要认证流程

  客户端首次向服务器发送HTTP请求,服务器返回401(未授权)响应进行挑战。401消息的头里带有WWW-Authenticate消息头,其中包含挑战摘要的随机参数nonce。客户端收到401后,将用户名密码和挑战信息用MD5加密形成认证鉴权头,重新发送给服务器,服务器对认证鉴权头进行验证,如果认证成功则返回200 OK,并在响应的消息中返回下次认证的随机数nextnonce,客户端下次请求时,根据nextnonce生成鉴权头进行HTTP请求。
HTTP摘要认证流程图
服务器的401未授权挑战WWW-Authenticate消息头语法:

challenge =“Digest” digest-challenge
digest-challenge=1#( realm | [ domain ] | nonce |
[ opaque ] |[ stale ] | [ algorithm ] |
[ qop-options ] | [auth-param] )
domain =“domain" “=” <“> URI ( 1*SP URI ) <”>
URI =absoluteURI | abs_path
nonce =“nonce" “=” nonce-value
nonce-value =quoted-string
opaque =“opaque" “=” quoted-string
stale =“stale" “=” ( “true” | “false” )
algorithm =“algorithm" “=” ( “MD5” | “MD5-sess” |
token )
qop-options =“qop" “=” <“> 1#qop-value <”>
qop-value =“auth" | “auth-int” | token

参数说明:

  • realm:用户域。由服务端告知
  • qop:保护质量。auth:鉴权,不对消息体做完整性验证。auth-int:鉴权并需要对消息体做摘要,保证消息完整性。
  • nonce:摘要质询参数。401响应中唯一生成的字符串数据。采用十六进制数据。
  • opaque:会话标识。由服务器指定,客户端须在请求二中返回该数据。采用十六进制数据。

客户端的挑战响应消息Authorization头参数一下为String类型:

  • username:用户名。必填。
  • realm:用户域。由服务方告知
  • nonce:摘要质询参数。返回请求一响应中的参数nonce
  • uri:访问路径。请求的URI
  • qop:保护质量。auth:鉴权,不对消息体做完整性验证。auth-int:鉴权并需要对消息体做摘要,保证消息完整性。注册过程使用auth
  • nc:nonce计数参数。客户端请求的十六进制计数,以00000001开始,每次请求加1,目的是防止重放攻击。
  • cnonce:客户端nonce值。客户端用来鉴定服务器的摘要质询参数
  • response:响应值。对请求一中401响应的参数采用MD5算法做摘要计算的结果
  • opaque:会话标识。返回服务器原值

response参数的算法:

response=<“> < KD ( H(A1), unq(nonce-value) “:” nc-value “:”
unq(cnonce-value) “:” unq(qop-value) “:” H(A2) ) <”>
A1=unq(username-value) “:” unq(realm-value) “:” passwd
如果qop等于auth,A2=Method “:” digest-uri-value 如果qop等于auth-int,A2=Method
“:” digest-uri-value “:” H(entity-body) 其中: H(data)=MD5(data)。
KD(secret, data)=H(concat(secret, “:”, data))。 unq(X)代表去掉X前后的引号。
Method=GET或者POST。 entity-body代表HTTP请求的消息体。 passwd=key。

三、代码实现HTTP Digest接入方式鉴权认证

以下是初次接触摘要认证时的简要请求方法。
摘要认证过程也可用Springorg.springframework.web.client.RestTemplate实现,修改配置类中的RestTemplate生成方法。具体方法自行百度。

public static String sendPostRequest(String username,String password,String uri, String jsonParams){CloseableHttpClient httpClient = null;String result =null;String requestUrl=url+uri;try {CredentialsProvider credsProvider = new BasicCredentialsProvider();httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();//第一次请求HttpPost post = new HttpPost(requestUrl);// 构造消息头post.addHeader("Content-type","application/json; charset=utf-8");post.setHeader("Accept", "application/json");HttpResponse firstResponse = httpClient.execute(post);org.apache.http.HttpEntity firstEntity = null;firstEntity = firstResponse.getEntity();result = EntityUtils.toString(firstEntity, "UTF-8");Map<String,String> map =new HashMap<String,String>();StringEntity stringEntity = new StringEntity(jsonParams);//解析头部Header[] h=firstResponse.getHeaders("WWW-Authenticate");StringBuffer auth=new StringBuffer(h[0].toString());String realm=auth.substring(auth.indexOf("realm=")+6,auth.indexOf("qop=")-1);String qop=auth.substring(auth.indexOf("qop=")+4,auth.indexOf("nonce=")-1);String nonce=auth.substring(auth.indexOf("nonce=")+6,auth.indexOf("opaque=")-1);String opaque=auth.substring(auth.indexOf("opaque=")+7);String cnonce="00000001";String nc="00000001";String algorithm="MD5";String responseString;String params1=username+":"+realm+":"+password;String params2="POST:"+uri;String params3= SecureUtil.md5(params1)+":"+nonce+":"+nc+":"+cnonce+":"+qop+":"+SecureUtil.md5(params2);responseString=SecureUtil.md5(params3);StringBuilder sb=new StringBuilder();String Authorization ="Digest username=" + username +",realm="+ realm +",nonce=" + nonce +",uri=" + uri +",response=" + responseString + ",cnonce=" +cnonce + ",qop=" +qop+ ",nc="+nc;//第二次请求// 构造消息头post.setHeader("Authorization",Authorization);post.setEntity(stringEntity);HttpResponse secondResponse = httpClient.execute(post);org.apache.http.HttpEntity secondEntity = null;secondEntity = secondResponse.getEntity();result = EntityUtils.toString(secondEntity, "UTF-8");httpClient.close();} catch (Exception e) {log.error(e.getMessage());} finally {if (httpClient != null) {try {httpClient.close();} catch (IOException e) {log.error(e.getMessage());}}}return result;}

http://chatgpt.dhexx.cn/article/YmZnnuUv.shtml

相关文章

C++ libcurl Digest Auth

C libcurl Digest Auth postman操作如下&#xff1a; 附认证原理如下&#xff1a; MD5 md5; string HA1 md5("username:realm:password"); string HA2 md5("method:uri"); string response md5("HA1:nouce:nc:cnonce:qop:HA2"); 这里…

mysql sql语句digest收集与展示

1.前言 mysql慢查询&#xff0c;已经有现成的成熟的方案收集展示了&#xff1a;pt-query-digest结合box公司的anemometer&#xff0c;没用过的移步&#xff1a;《mysql慢查询可视化》&#xff08;本章内容需要提前了解anemometer&#xff09;。 但DBA们一定还遇到过这样的问题…

HTTP Digest authentication

什么是摘要认证 摘要认证&#xff08; Digest authentication&#xff09;是一个简单的认证机制&#xff0c;最初是为HTTP协议开发的&#xff0c;因而也常叫做HTTP摘要&#xff0c;在RFC2617中描述。其身份验证机制很简单&#xff0c;它采用杂凑式&#xff08;hash&#xff09…

配置Apache Digest认证

Apache常见的用户认证可以分为下面三种&#xff1a; 基于IP&#xff0c;子网的访问控制(ACL)基本用户验证(Basic Authentication)消息摘要式身份验证(Digest Authentication) 消息摘要式身份验证(Digest Authentication) Digest Authentication在基本身份验证上面扩展了安全…

http Digest认证计算方法整理

摘要认证及实现HTTP digest authentication - 简书 HTTP Basic和Digest认证介绍与计算 - 诸子流 - 博客园 不要不知道上面说的URI是什么意思啊 图解HTTP 第 8 章 确认访问用户身份的认证 - 简书8.1 何为认证 为了弄清究竟是谁在访问服务器&#xff0c;就得让对方的客户端自报家…

业务维度digest日志的记录与监控方案

需求 ​   为了满足从业务整体的维度 实现监控和链路复原&#xff0c;我们希望对于一个业务接口&#xff0c;记录一行请求日志&#xff0c;并通过某个 Unique Id&#xff08;如UserId、OrderId&#xff09;将多行日志关联起来&#xff0c;最终产出一批和业务强相关的数据&am…

java发起Digest Auth请求

常规认证方式 上代码&#xff1a; 需要的Maven <dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5</version></dependency><dependency><groupId>org.…

认证学习3 - Digest摘要认证讲解、代码实现、演示

文章目录 Digest摘要认证 - 密文讲解&#xff08;Digest摘要认证&#xff09;实现&#xff08;Digest认证&#xff09;代码&#xff08;Digest认证&#xff09;代码&#xff08;Digest认证-客户端&#xff09;演示&#xff08;Digest认证-postman&#xff09; 认证大全&#xf…

HTTP的认证方式之DIGEST 认证(摘要认证)

核心步骤&#xff1a; 步骤 1&#xff1a; 请求需认证的资源时&#xff0c;服务器会随着状态码 401Authorization Required&#xff0c;返回带WWW-Authenticate 首部字段的响应。该字段内包含质问响应方式认证所需的临时质询码&#xff08;随机数&#xff0c;nonce&#xff09;…

Digest Auth 摘要认证

Digest Auth 摘要认证 1.非常规方式 转载&#xff1a;https://blog.csdn.net/qq_25391785/article/details/86595529 public static void postMethod(String url, String query) {try {CredentialsProvider credsProvider new BasicCredentialsProvider();credsProvider.setC…

digest鉴权

“摘要”式认证&#xff08; Digest authentication&#xff09;是一个简单的认证机制&#xff0c;最初是为HTTP协议开发的&#xff0c;因而也常叫做HTTP摘要&#xff0c;在RFC2671中描述。其身份验证机制很简单&#xff0c;它采用杂凑式&#xff08;hash&#xff09;加密方法&…

消息摘要(Digest),数字签名(Signature),数字证书(Certificate)是什么?

1. 消息摘要&#xff08;Digest&#xff09; 1. 什么是消息摘要&#xff1f; 对一份数据&#xff0c;进行一个单向的 Hash 函数&#xff0c;生成一个固定长度的 Hash 值&#xff0c;这个值就是这份数据的摘要&#xff0c;也称为指纹。 2. 摘要算法 常见的摘要算法有 MD5、SHA…

HTTP通讯安全中的Digest摘要认证释义与实现

摘要 出于安全考虑&#xff0c;HTTP规范定义了几种认证方式以对访问者身份进行鉴权&#xff0c;最常见的认证方式之一是Digest认证 Digest认证简介 HTTP通讯采用人类可阅读的文本格式进行数据通讯&#xff0c;其内容非常容易被解读。出于安全考虑&#xff0c;HTTP规范定义了几…

http协议之digest(摘要)认证,详细讲解并附Java SpringBoot源码

目录 1.digest认证是什么&#xff1f; 2.digest认证过程 3.digest认证参数详解 4.基于SpringBoot实现digest认证 5.digest认证演示 6.digest认证完整项目 7.参考博客 1.digest认证是什么&#xff1f; HTTP通讯采用人类可阅读的文本格式进行数据通讯&#xff0c;其内容非…

【WinRAR】WinRAR 6.01 官方最新简体中文版

WinRAR 6.01 官方简体中文商业版下载地址&#xff08;需要注册&#xff09;&#xff1a; 64位&#xff1a; https://www.win-rar.com/fileadmin/winrar-versions/sc/sc20210414/wrr/winrar-x64-601sc.exe https://www.win-rar.com/fileadmin/winrar-versions/sc/sc20210414/…

WinRAR命令行

基本使用 实践 将文件夹压缩到zip包 输入&#xff1a;文件夹如下&#xff0c;文件夹为class。 输出&#xff1a;classes.zip 指令如下&#xff1a; rar a classes.zip .\classes或者 WinRAR a classes.zip .\classes结果如下&#xff1a; PS C:\Users\liyd\Desktop\kuai…

WinRAR安装教程

文章目录 WinRAR安装教程无广告1. 下载2. 安装3. 注册4. 去广告 WinRAR安装教程无广告 1. 下载 国内官网&#xff1a;https://www.winrar.com.cn/ 2. 安装 双击&#xff0c;使用默认路径&#xff1a; 点击“安装”。 点击“确定”。 点击“完成”。 3. 注册 链接&#x…

WinRAR注册+去广告教程

1、注册 在WinRAR安装目录创建rarreg.key文件&#xff0c; 拷贝如下内容并保存&#xff1a; RAR registration data Federal Agency for Education 1000000 PC usage license UIDb621cca9a84bc5deffbf 6412612250ffbf533df6db2dfe8ccc3aae5362c06d54762105357d 5e3b1489e751c…

WinRAR4.20注册文件rarreg.key

2019独角兽企业重金招聘Python工程师标准>>> 在WinRAR的安装目录下&#xff0c;新建rarreg.key文件&#xff08;注意不要创建成rarreg.key.txt文件了^_^&#xff09;&#xff0c;内容为如下&#xff1a; RAR registration data Team EAT Single PC usage license UI…

Android按钮样式

//创建一个新的XML文件&#xff0c;可命名为styles<style name"button1"><item name"android:layout_height">wrap_content</item><item name"android:textColor">#FFFFFF</item><item name"android:text…