添加注册失败时回复403避免陷入401循环

This commit is contained in:
64850858
2021-06-08 14:43:03 +08:00
parent 57b562d622
commit abd569d79a
6 changed files with 124 additions and 62 deletions

View File

@@ -241,4 +241,59 @@ public class DigestServerAuthenticationHelper {
return mdString.equals(response);
}
public static void main(String[] args) throws NoSuchAlgorithmException {
String realm = "4401000000";
String username = "44010000001110008008";
String nonce = "0074b397f86fc263b1b7f9eb72553267";
String uri = "sip:44010000002000000001@4401000000";
// qop 保护质量 包含auth默认的和auth-int增加了报文完整性检测两种策略
String qop = null;
// 客户端随机数,这是一个不透明的字符串值,由客户端提供,并且客户端和服务器都会使用,以避免用明文文本。
// 这使得双方都可以查验对方的身份,并对消息的完整性提供一些保护
//String cNonce = authHeader.getCNonce();
// nonce计数器是一个16进制的数值表示同一nonce下客户端发送出请求的数量
int nc = -1;
String ncStr = new DecimalFormat("00000000").format(nc);
// String ncStr = new DecimalFormat("00000000").format(Integer.parseInt(nc + "", 16));
MessageDigest messageDigest = MessageDigest.getInstance(DEFAULT_ALGORITHM);
String A1 = username + ":" + realm + ":" + "crservice@123";
String A2 = "REGISTER" + ":" + uri.toString();
byte mdbytes[] = messageDigest.digest(A1.getBytes());
String HA1 = toHexString(mdbytes);
System.out.println("A1: " + A1);
System.out.println("A2: " + A2);
mdbytes = messageDigest.digest(A2.getBytes());
String HA2 = toHexString(mdbytes);
System.out.println("HA1: " + HA1);
System.out.println("HA2: " + HA2);
String cnonce = null;
System.out.println("nonce: " + nonce);
System.out.println("nc: " + ncStr);
System.out.println("cnonce: " + cnonce);
System.out.println("qop: " + qop);
String KD = HA1 + ":" + nonce;
if (qop != null && qop.equals("auth") ) {
if (nc != -1) {
KD += ":" + ncStr;
}
if (cnonce != null) {
KD += ":" + cnonce;
}
KD += ":" + qop;
}
KD += ":" + HA2;
System.out.println("KD: " + KD);
mdbytes = messageDigest.digest(KD.getBytes());
String mdString = toHexString(mdbytes);
System.out.println("mdString: " + mdString);
String response = "fdb1608a7a3b96f0598f40b8ba78d6a9";
System.out.println("response: " + response);
}
}