本文介绍: 加签验签发送消息方,对消息加签名;接受消息方,验证签名是否正确

网络发展快速过程中,总是会忽略接口数据安全问题,进行验签能够在一定程度上能够防刷数据篡改。

加签验签

发送消息方,对消息加签名

接受消息方,验证签名是否正确

发送消息方:

1、根据消息内容形成摘要

  1. Accept: application/json, text/plain, */*
  2. Accept-Encoding: gzip, deflate
  3. Accept-Language: en_US
  4. Authorization: eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiIxNTk5NjM2NTUyOEAxNjMuY29tIiwic2NvcGUiOlsiYWxsIl0sImlkIjozNTU5LCJleHAiOjE2Njg1ODkxMjUsImF1dGhvcml0aWVzIjpbIuWJjeWPsOS8muWRmCJdLCJqdGkiOiIyZDAxMWVhOS02N2RjLTRmZmQtOTAwMS1jYTMwNTU1MzM0NzkiLCJjbGllbnRfaWQiOiJmcm9udC1hcHAifQ.AkKh_rTiIn1tckM-hvbM5o9lKEESd9TFz19W8xKDVcVisgwZ6tUTYcROB3JUdijGq-_7VnEnFjmjY3qXYeOHngo_ctDgw6KQqkDghSHGUDohI2SLXiTuHDZtp6H_f1wnTXYnHWqox7hwiNIWYbckkh4sRw6jkNDQAf6KfxccJ0Y
  5. Connection: keepalive
  6. Cookie: _ga=GA1.1.500562097.1650948294; _ga_TZED0ZY3LF=GS1.1.1667983869.442.1.1667984325.0.0.0
  7. Expires: 1667984325369
  8. Host: h5.newsitunemall.com
  9. Referer: http://h5.newsitunemall.com/mine
  10. Signature: 1c356d047bcb10de8c1700ab3f3f25ec
  11. Source: h5
  12. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36
  13. version: 1.0.82
  14. X-Request-Id: acc9d18b85159844ea89e758747ecfa3
public void checkSign(String sign, Map<String, String> requestParam,String timestamp, String requestId, URI uri){
        if (StrUtil.isBlank(sign) || StrUtil.isBlank(timestamp) || StrUtil.isBlank(requestId)) {
            LOGGER.error("illegal request  sign:{} timestamp:{}  requestId:{}",sign,timestamp,requestId);
            throw new ApiException("illegal request,param is null!");
        }

        long time;
        try {
            time = Long.parseLong(timestamp);
        } catch (Exception e) {
            LOGGER.error("illegal request  timestamp不合法,timestamp:" + timestamp);
            throw new ApiException("timestamp illegal");
        }
        //验证时间戳是否过期
        long currentTime = System.currentTimeMillis();
        if (time > currentTime + timeOver || time < currentTime - timeOver) {
            LOGGER.error("illegal request  timestamp expires now:{} timestamp:{}",currentTime,time);
            throw new ApiException("please refresh");
        }

        if(redisTemplate.hasKey(requestId)){
            LOGGER.error("illegal request  requestId exist:{}",requestId);
            throw new ApiException("illegal request,requestId exist:"+requestId);
        }else{
            redisTemplate.opsForValue().set(requestId, requestId,timeOver, TimeUnit.MILLISECONDS);
        }
        String paramAscII = MapUtil.sortJoin(requestParam, "&amp;", "=", true, requestId, timestamp);

        String md5 = new Digester(DigestAlgorithm.MD5).digestHex(paramAscII);
        if (!md5.equals(sign)) {
            LOGGER.error("illegal request,check sign fail! paramAscII:{} sourceSign:{}  sysSign:{}",paramAscII,sign,md5);
            throw new ApiException("illegal request,check sign fail");
        }
    }

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注