目录
1.digest认证是什么?
2.digest认证过程
3.digest认证参数详解
4.基于SpringBoot实现digest认证
5.digest认证演示
6.digest认证完整项目
7.参考博客
1.digest认证是什么?
HTTP通讯采用人类可阅读的文本格式进行数据通讯,其内容非常容易被解读。出于安全考虑,HTTP规范定义了几种认证方式以对访问者身份进行鉴权,最常见的认证方式之一是Digest认证。Digest是一种加密认证方式,通讯中不会传输密码信息,而仅采用校验方式对接入的请求进行验证。
DIGEST 认证是使用质询 / 响应的方式(challenge/response),但不会像 BASIC 认证那样直接发送明文密码。质询响应方式是指,一开始一方会先发送认证要求给另一方,接着使用从另一方那接收到的质询码计算生成响应码。最后将响应码返回给对方进行认证的方式。
Digest认证支持的加密算法有:SHA256,SHA512/256,MD5。上述这几种算法都是由哈希函数来生成散列值,其加密过程为单向计算,请求方无法反算出密码明文。
2.digest认证过程

步骤 1: 请求需认证的资源时,服务器会随着状态码 401Authorization Required,返回带WWW-Authenticate 首部字段的响应。该字段内包含质问响应方式认证所需的临时质询码(随机数,nonce)。首部字段 WWW-Authenticate 内必须包含realm 和nonce 这两个字段的信息。客户端就是依靠向服务器回送这两个值进行认证的。nonce 是一种每次随返回的 401 响应生成的任意随机字符串。该字符串通常推荐由Base64 编码的十六进制数的组成形式,但实际内容依赖服务器的具体实现。
步骤 2:接收到401状态码的客户端,返回的响应中包含 DIGEST 认证必须的首部字段 Authorization 信息。首部字段 Authorization 内必须包含 username、realm、nonce、uri 和response的字段信息。其中,realm 和 nonce 就是之前从服务器接收到的响应中的字段。
username是realm 限定范围内可进行认证的用户名。uri(digest-uri)即Request-URI的值,但考虑到经代理转发后Request-URI的值可能被修改因此事先会复制一份副本保存在 uri内。
response 也可叫做 Request-Digest,存放经过 MD5 运算后的密码字符串,形成响应码。
步骤 3:接收到包含首部字段 Authorization 请求的服务器,会确认认证信息的正确性。认证通过后则返回包含 Request-URI 资源的响应。并且这时会在首部字段 Authentication-Info 写入一些认证成功的相关信息。(不过我下面的例子没有去写这个Authentication-Info,而是直接返回的数据。因为我实在session里缓存的认证结果)。
3.digest认证参数详解
WWW-Authentication:用来定义使用何种方式(Basic、Digest、Bearer等)去进行认证以获取受保护的资源
realm:表示Web服务器中受保护文档的安全域(比如公司财务信息域和公司员工信息域),用来指示需要哪个域的用户名和密码
qop:保护质量,包含auth(默认的)和auth-int(增加了报文完整性检测)两种策略,(可以为空,但是)不推荐为空值
nonce:服务端向客户端发送质询时附带的一个随机数,这个数会经常发生变化。客户端计算密码摘要时将其附加上去,使得多次生成同一用户的密码摘要各不相同,用来防止重放攻击
nc:nonce计数器,是一个16进制的数值,表示同一nonce下客户端发送出请求的数量。例如,在响应的第一个请求中,客户端将发送“nc=00000001”。这个指示值的目的是让服务器保持这个计数器的一个副本,以便检测重复的请求
cnonce:客户端随机数,这是一个不透明的字符串值,由客户端提供,并且客户端和服务器都会使用,以避免用明文文本。这使得双方都可以查验对方的身份,并对消息的完整性提供一些保护
response:这是由用户代理软件计算出的一个字符串,以证明用户知道口令
Authorization-Info:用于返回一些与授权会话相关的附加信息
nextnonce:下一个服务端随机数,使客户端可以预先发送正确的摘要
rspauth:响应摘要,用于客户端对服务端进行认证
stale:当密码摘要使用的随机数过期时,服务器可以返回一个附带有新随机数的401响应,并指定stale=true,表示服务器在告知客户端用新的随机数来重试,而不再要求用户重新输入用户名和密码了
4.基于SpringBoot实现digest认证
DemoApplication.java
package com.digest.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication(scanBasePackages="com.digest")
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}
WebConfig.java
import com.digest.interceptor.DigestAuthInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
public class WebConfig implements WebMvcConfigurer {@Overridepublic void addInterceptors(InterceptorRegistry registry) {DigestAuthInterceptor requireAuthInterceptor = new DigestAuthInterceptor();registry.addInterceptor(requireAuthInterceptor);}}
IndexController.java
package com.digest.controller;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.digest.interceptor.DigestAuth;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;@Controller
public class IndexController {@DigestAuth@RequestMapping("/login")@ResponseBodypublic String login(HttpServletRequest req, HttpServletResponse res) {return "{code: 0, data: {username:\"test\"}}";}@DigestAuth@RequestMapping("/index")@ResponseBodypublic String index(HttpServletRequest req, HttpServletResponse res) {return "{code: 0, data: {xxx:\"xxx\"}}";}}
DigestAuth.java
package com.digest.interceptor;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;// can be used to method
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DigestAuth {}
DigestAuthInterceptor.java
package com.digest.interceptor;import java.text.MessageFormat;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.digest.model.DigestAuthInfo;
import com.digest.util.DigestUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;public class DigestAuthInterceptor extends HandlerInterceptorAdapter {// 为了 测试Digest nc 值每次请求增加private int nc = 0;@Overridepublic boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {// 请求目标为 method of controller,需要进行验证if (handler instanceof HandlerMethod) {HandlerMethod handlerMethod = (HandlerMethod) handler;Object object = handlerMethod.getMethodAnnotation(DigestAuth.class);/* 方法没有 @RequireAuth 注解, 放行 */if (object == null) {return true; // 放行}/* 方法有 @RequireAuth 注解,需要拦截校验 */// 没有 Authorization 请求头,或者 Authorization 认证信息验证不通过,拦截if (!isAuth(req, res)) {// 验证不通过,拦截return false;}// 验证通过,放行return true;}// 请求目标不是 mehod of controller, 放行return true;}private boolean isAuth(HttpServletRequest req, HttpServletResponse res) {String authStr = req.getHeader("Authorization");System.out.println("请求 Authorization 的内容:" + authStr);if (authStr == null || authStr.length() <= 7) {// 没有 Authorization 请求头,开启质询return challenge(res);}DigestAuthInfo authObject = DigestUtils.getAuthInfoObject(authStr);// System.out.println(authObject);/** 生成 response 的算法:* response = MD5(MD5(username:realm:password):nonce:nc:cnonce:qop:MD5(<request-method>:url))*/// 这里密码固定为 123456, 实际应用需要根据用户名查询数据库或缓存获得String HA1 = DigestUtils.MD5(authObject.getUsername() + ":" + authObject.getRealm() + ":"+authObject.getUsername());String HD = String.format(authObject.getNonce() + ":" + authObject.getNc() + ":" + authObject.getCnonce() + ":"+ authObject.getQop());String HA2 = DigestUtils.MD5(req.getMethod() + ":" + authObject.getUri());String responseValid = DigestUtils.MD5(HA1 + ":" + HD + ":" + HA2);// 如果 Authorization 中的 response(浏览器生成的) 与期望的 response(服务器计算的) 相同,则验证通过System.out.println("Authorization 中的 response: " + authObject.getResponse());System.out.println("期望的 response: " + responseValid);if (responseValid.equals(authObject.getResponse())) {/* 判断 nc 的值,用来防重放攻击 */// 判断此次请求的 Authorization 请求头里面的 nc 值是否大于之前保存的 nc 值// 大于,替换旧值,然后 return true// 否则,return false// 测试代码 startint newNc = Integer.parseInt(authObject.getNc(), 16);System.out.println("old nc: " + this.nc + ", new nc: " + newNc);if (newNc > this.nc) {this.nc = newNc;return true;}return false;// 测试代码 end}// 验证不通过,重复质询return challenge(res);}/*** 质询:返回状态码 401 和 WWW-Authenticate 响应头** @param res 返回false,则表示拦截器拦截请求*/private boolean challenge(HttpServletResponse res) {// 质询前,重置或删除保存的与该用户关联的 nc 值(nc:nonce计数器,是一个16进制的数值,表示同一nonce下客户端发送出请求的数量)// 将 nc 置为初始值 0, 这里代码省略// 测试代码 startthis.nc = 0;// 测试代码 endres.setStatus(401);String str = MessageFormat.format("Digest realm={0},nonce={1},qop={2}", "\"no auth\"","\"" + DigestUtils.generateToken() + "\"", "\"auth\"");res.addHeader("WWW-Authenticate", str);return false;}}
DigestAuthInfo.java
package com.digest.model;import lombok.Data;@Data
public class DigestAuthInfo {private String username;//认证的用户名private String realm;//Web服务器中受保护文档的安全域(比如公司财务信息域和公司员工信息域),用来指示需要哪个域的用户名和密码private String nonce;//服务端向客户端发送质询时附带的一个随机数private String uri;//请求的资源位置,访问地址,例如/indexprivate String response;//由用户代理软件计算出的一个字符串,以证明用户知道口令private String qop;//保护质量,包含auth(默认的)和auth-int(增加了报文完整性检测)两种策略,(可以为空,但是)不推荐为空值private String nc;//nonce计数器,是一个16进制的数值,表示同一nonce下客户端发送出请求的数量。public String cnonce;//客户端随机数,这是一个不透明的字符串值,由客户端提供,并且客户端和服务器都会使用,以避免用明文文本。}
DigestUtils.java
package com.digest.util;import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Random;import com.digest.model.DigestAuthInfo;public class DigestUtils {/*** 根据当前时间戳生成一个随机字符串* @return*/public static String generateToken() {String s = String.valueOf(System.currentTimeMillis() + new Random().nextInt());try {MessageDigest messageDigest = MessageDigest.getInstance("md5");byte[] digest = messageDigest.digest(s.getBytes());return Base64.getEncoder().encodeToString(digest);} catch (NoSuchAlgorithmException e) {throw new RuntimeException();}}public static String MD5(String inStr) {MessageDigest md5 = null;try {md5 = MessageDigest.getInstance("MD5");} catch (Exception e) {System.out.println(e.toString());e.printStackTrace();return "";}char[] charArray = inStr.toCharArray();byte[] byteArray = new byte[charArray.length];for (int i = 0; i < charArray.length; i++) {byteArray[i] = (byte) charArray[i];}byte[] md5Bytes = md5.digest(byteArray);StringBuffer hexValue = new StringBuffer();for (int i = 0; i < md5Bytes.length; i++) {int val = ((int) md5Bytes[i]) & 0xff;if (val < 16)hexValue.append("0");hexValue.append(Integer.toHexString(val));}return hexValue.toString();}/*** 该方法用于将 Authorization 请求头的内容封装成一个对象。** Authorization 请求头的内容为:* Digest username="aaa", realm="no auth", nonce="b2b74be03ff44e1884ba0645bb961b53",* uri="/BootDemo/login", response="90aff948e6f2207d69ecedc5d39f6192", qop=auth,* nc=00000002, cnonce="eb73c2c68543faaa"*/public static DigestAuthInfo getAuthInfoObject(String authStr) {if (authStr == null || authStr.length() <= 7)return null;if (authStr.toLowerCase().indexOf("digest") >= 0) {// 截掉前缀 DigestauthStr = authStr.substring(6);}// 将双引号去掉authStr = authStr.replaceAll("\"", "");DigestAuthInfo digestAuthObject = new DigestAuthInfo();String[] authArray = new String[8];authArray = authStr.split(",");// System.out.println(java.util.Arrays.toString(authArray));for (int i = 0, len = authArray.length; i < len; i++) {String auth = authArray[i];String key = auth.substring(0, auth.indexOf("=")).trim();String value = auth.substring(auth.indexOf("=") + 1).trim();switch (key) {case "username":digestAuthObject.setUsername(value);break;case "realm":digestAuthObject.setRealm(value);break;case "nonce":digestAuthObject.setNonce(value);break;case "uri":digestAuthObject.setUri(value);break;case "response":digestAuthObject.setResponse(value);break;case "qop":digestAuthObject.setQop(value);break;case "nc":digestAuthObject.setNc(value);break;case "cnonce":digestAuthObject.setCnonce(value);break;}}return digestAuthObject;}}
application.properties
server.port=8080
server.servlet.context-path=/
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.6</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>SpringBootDigest</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
5.digest认证演示
启动项目后,访问http://localhost:8080/login,出现如下界面

输入账号 user 密码 user,登录,出现如下界面

项目后端打印信息如下所示:
请求 Authorization 的内容:Digest username="user", realm="no auth", nonce="Fy9j+28xS8co4LWob6LOAg==", uri="/login", response="516234f0509c3c81f9130ddfbcd95f50", qop=auth, nc=00000006, cnonce="b064dbc52ff14c0a"
Authorization 中的 response: 516234f0509c3c81f9130ddfbcd95f50
期望的 response: 516234f0509c3c81f9130ddfbcd95f50
old nc: 0, new nc: 6
请求 Authorization 的内容:Digest username="user", realm="no auth", nonce="Fy9j+28xS8co4LWob6LOAg==", uri="/login", response="3977b5c6e46b2941b61c835659dd904a", qop=auth, nc=00000007, cnonce="637df6c7b8be6edb"
Authorization 中的 response: 3977b5c6e46b2941b61c835659dd904a
期望的 response: 3977b5c6e46b2941b61c835659dd904a
old nc: 6, new nc: 7
请求 Authorization 的内容:null
请求 Authorization 的内容:Digest username="user", realm="no auth", nonce="T/46pI2qR2K0Cy6D4hb4jg==", uri="/login", response="ae9a759d6ac8af2d4a9e73a17cdd0859", qop=auth, nc=00000002, cnonce="3efb440863ffc450"
Authorization 中的 response: ae9a759d6ac8af2d4a9e73a17cdd0859
期望的 response: ae9a759d6ac8af2d4a9e73a17cdd0859
old nc: 0, new nc: 2
6.digest认证完整项目
基于SpringBoot完整项目 http://www.zrscsoft.com/sitepic/12152.html
7.参考博客
http协议之digest(摘要)认证_red-fly的博客-CSDN博客_digest认证
HTTP的几种认证方式之DIGEST 认证(摘要认证) - wenbin_ouyang - 博客园
















