赞
踩
后端开发基本都遇到过使用签名校验的情况,签名的作用是为了防止请求数据被别人截取篡改重新请求。
为什么签名验证可以防止请求数据被篡改,因为一般签名的规则就是,你的所有请求参数,按照约定好的格式进行拼接,后面得到一个拼接后的字符串,这个字符串后面再加上一个双方私下确认后的签名秘钥(固定),最后组合成一个待签名字符串,用这个字符串进行sm3加密。sm3加密是个不可逆的加密(理论上),请求方把这个加密后面签名字段放到请求头中,服务提供方本地根据相同的方法进行组合签名字符串和加密,然后对面本地加密的签名和请求头中的签名是否相同,相同则认为这个数据是可靠的,未被篡改过的(加密前数据不同,加密后的签名肯定不同)。
PS:签名只能验证防止请求数据被篡改,并不能说你把数据加密让别人看不见,只要是互联网上传输,数据就可能被别人获取到
请求头加上时间戳,代码上验证请求的时间戳和当前时间戳时间差距,差距过大就拒绝请求(比如只接受一分钟内请求),同时把这个时间戳加到签名字符串去,这样签名的数据内容就包含时间戳(可以防止别人用同一份签名发起重复请求攻击,因为时间戳是一直在变的,签名中的时间戳和请求报文头的时间戳不同,验证签名就不会通过),这样就可以做到即使有人获取到了某个请求的参数和签名,以此发起多次重复请求攻击,这种攻击最多只有一分钟的有效期。
- package com.dw.task.utils;
-
- import cn.hutool.crypto.SmUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Component;
- import java.lang.reflect.Field;
- import java.util.Objects;
-
- @Slf4j
- @Component
- public class Sm3UtilHua {
-
- private static Integer httpCheckSignTimeOut = 1;//请求签名有效时间 默认1分钟
-
- /**
- * 获取实体类拼成的加密字段
- * @param checkSign 前端传入的签名(从请求报文头获取)
- * @param signModel 查询的DTO模型类
- * @param privateKey 签名加密私钥
- * @param timestamp 时间戳(从请求报文头获取)
- * @return 比对结果
- */
- public boolean checkSign(String checkSign , Object signModel , String privateKey,Long timestamp) throws Exception {
- Long thisTime = System.currentTimeMillis() - timestamp;
- Integer checkSignTimeOut = httpCheckSignTimeOut;
- if (!(Objects.isNull(checkSignTimeOut) || checkSignTimeOut.intValue() == 0)){
- //时间为0或者未配置签名超时时间,默认不验证时间戳
- if(thisTime >= 60*1000*checkSignTimeOut || thisTime<=0){
- //checkSignTimeOut分钟内的时间戳才处理
- log.error("时间戳异常,非"+checkSignTimeOut+"分钟内请求,当前时间戳:"+System.currentTimeMillis());
- return false;
- }
- }
- String signValue = getSignValue(signModel) + "×tamp=" + timestamp +"&privateKey=" +privateKey;
- String sign = getSign(signValue);
- log.info("【本地加密后 sm3 签名】" + sign);//生产上建议注释此行,防止泄露
- return sign.toUpperCase().equals(checkSign.toUpperCase())? true : false;
- }
- /**
- * 加密签名
- * @param signValue 待加密签名字符串
- * @return 加密后签名字符串
- */
- public String getSign(String signValue){
- return SmUtil.sm3(signValue);
- }
-
- /**
- * 获取实体类拼成的加密字段
- * @param classA 传入参数实体类
- * @return 待加密字符串
- */
- public String getSignValue(Object classA) {
- Field[] fs = classA.getClass().getDeclaredFields();//获取所有属性
- String[][] temp = new String[fs.length][2]; //用二维数组保存 参数名和参数值
- for (int i=0; i<fs.length; i++) {
- fs[i].setAccessible(true);
- temp[i][0] = fs[i].getName().toLowerCase(); //获取属性名
- try {
- temp[i][1] = String.valueOf(fs[i].get(classA)) ;//把属性值放进数组
- } catch (Exception e) {
- log.error("【签名字段:"+fs[i].getName()+"添加失败】");
- }
- }
- temp = doChooseSort(temp); //对参数实体类按照字母顺序排续
- String result = "";
- for (int i = 0; i < temp.length; i++) {//按照签名规则生成待加密字符串
- result = result + temp[i][0]+"="+temp[i][1]+"&";
- }
- result = result.substring(0, result.length()-1);//消除掉最后的“&”
- log.info("【签名信息】{}" ,result);
- return result;
- }
-
- /**
- * 对二维数组里面的数据进行选择排序,按字段名按abcd顺序排列
- * @param data 未按照字母顺序排序的二维数组
- * @return
- */
- private String[][] doChooseSort(String[][] data) {//排序方式为选择排序
- String[][] temp = new String[data.length][2];
- temp = data;
- int n = temp.length;
- for (int i = 0; i < n-1; i++) {
- int k = i;// 初始化最小值的小标
- for (int j = i+1; j<n; j++) {
- if (temp[k][0].compareTo(temp[j][0]) > 0) { //下标k字段名大于当前字段名
- k = j;// 修改最大值的小标
- }
- }
- // 将最小值放到排序序列末尾
- if (k > i) { //用相加相减法交换data[i] 和 data[k]
- String tempValue ;
- tempValue = temp[k][0];
- temp[k][0] = temp[i][0];
- temp[i][0] = tempValue;
- tempValue = temp[k][1];
- temp[k][1] = temp[i][1];
- temp[i][1] = tempValue;
- }
- }
- return temp;
- }
-
- }
- public static void main(String[] args) throws Exception {
- //模拟请求参数
- QueryDTO dto = new QueryDTO();
- dto.setName("张三");
- dto.setAge(18);
- dto.setIdCard("666666666666666");
- //模拟请求报文头时间戳
- Long nowTime = System.currentTimeMillis();
- //签名加密私钥(不要在互联网上传输,调用方和提供方私下物理确认和设置)
- String privateKey = "48d95af20fa1bc438db42e280085707b60841c";
-
- Sm3UtilHua sm3UtilHua = new Sm3UtilHua();
-
- System.out.println("1:请求签名错误的案例");
- //模拟请求错误的签名
- String errorSign = "2222222222222222222";
- System.out.println("验签校验结果:" + sm3UtilHua.checkSign(errorSign,dto, privateKey,nowTime));
- System.out.println();
-
- System.out.println("2:请求签名正确的案例");
- String signValue = sm3UtilHua.getSignValue(dto) + "×tamp=" + nowTime +"&privateKey=" +privateKey;//复制上面1请求的加密后签名
- String trueSign = sm3UtilHua.getSign(signValue);
- System.out.println("验签校验结果:" + sm3UtilHua.checkSign(trueSign,dto, privateKey,nowTime));
- System.out.println();
-
- System.out.println("3:请求签名正确,但是时间非一分钟内请求的案例");
- Long oldTime = 1688036145560L;//这个时间戳大概是2023年06月29号的时间戳,所以必不是当前一分钟内时间
- System.out.println("验签校验结果:" + sm3UtilHua.checkSign(trueSign,dto, privateKey,oldTime));
-
- }
下面的签名信息打印是不完整的,没有拼接时间戳和签名私钥,也是防止打印到日志然后泄露
- 1:请求签名错误的案例
- 10:42:11.506 [main] ERROR com.dw.task.utils.Sm3UtilHua - 时间戳异常,非1分钟内请求,当前时间戳:1688092931498
- 验签校验结果:false
-
- 2:请求签名正确的案例
- 10:42:11.522 [main] INFO com.dw.task.utils.Sm3UtilHua - 【签名信息】age=18&idcard=666666666666666&name=张三
- 10:42:12.358 [main] INFO com.dw.task.utils.Sm3UtilHua - 【签名信息】age=18&idcard=666666666666666&name=张三
- 10:42:12.359 [main] INFO com.dw.task.utils.Sm3UtilHua - 【本地加密后 sm3 签名】e60bf8ea2453f44a4a6d3b43f55399c2ce09a6f5b4be68378506d95d2d6f4491
- 验签校验结果:true
-
- 3:请求签名正确,但是时间非一分钟内请求的案例
- 10:42:12.359 [main] ERROR com.dw.task.utils.Sm3UtilHua - 时间戳异常,非1分钟内请求,当前时间戳:1688092932359
- 验签校验结果:false
上面的代码已经可以防止一定程度的重复请求攻击,但是一分钟内的重复请求攻击还是无法防止的,如果别人截取请求信息后一分钟内发起大量重复请求的话,还是会通过签名并且穿透到业务层,对此呢如果要再优化,就可以把每次验证成功的签名放到redis缓存中,数据有效期是1分钟。这样每次在验签之前先查询redis缓存中是否有相同的签名,有即代表这个请求是重复请求,直接拦截
思路就是这样,代码就不写了
感谢各位观众朋友阅读,如有不同意见,请不吝赐教
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。