美文网首页
校验时间戳和签名(转)

校验时间戳和签名(转)

作者: 是我拉叔 | 来源:发表于2019-08-14 14:45 被阅读0次

    https://blog.csdn.net/riju4713/article/details/81457479

    校验时间戳和签名

    2018年08月06日 18:52:25 慕容潇湘 阅读数 2778

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。

    本文链接:https://blog.csdn.net/riju4713/article/details/81457479

    1.获取url链接里面的时间戳和签名 

    // 时间戳

    Stringtimestamp = qpMap.get("timestamp");

    // 签名

    Stringsign = qpMap.get("sign");

    2.进行校验

    // 校验签名前后过期时间

                checkSignTime(timestamp);

    // 校验签名

                checkSign(streamStr, timestamp, sign);

    privatevoidcheckSignTime(String timestamp){

    if(StringUtils.isBlank(timestamp)) {

    //logger.error("{}-鉴权时间戳缺失", MsgCode.SIGN_PARAM_ERROR_CODE);

    thrownewBusinessException(MsgCode.SIGN_PARAM_ERROR_MSG, MsgCode.SIGN_PARAM_ERROR_CODE);

            }

    longcurrentTime = System.currentTimeMillis() /1000;// 统一都传毫秒

    longrequestTime =0;

    try{

                requestTime = Long.parseLong(timestamp);

    }catch(NumberFormatException e) {

    //logger.error("{}-鉴权时间戳错误 timestamp:{}", MsgCode.SIGN_PARAM_ERROR_CODE, timestamp);

    thrownewBusinessException(MsgCode.SIGN_PARAM_ERROR_MSG, MsgCode.SIGN_PARAM_ERROR_CODE);

            }

    if(Math.abs(currentTime - requestTime) > APPIConstants.Filter.OUT_OF_DATE_TIME_LONG) {

    logger.error("{}-签名已过期,服务器当前时间:{}", MsgCode.OUT_OF_DATE_SIGN_CODE, currentTime);

    thrownewBusinessException(String.format(MsgCode.OUT_OF_DATE_SIGN_MSG, currentTime), MsgCode.OUT_OF_DATE_SIGN_CODE);

            }

        }

    if(StringUtils.isBlank(sign)) {

    //logger.error("{}-签名为空", MsgCode.SIGN_PARAM_ERROR_CODE);

    thrownewBusinessException(MsgCode.SIGN_PARAM_ERROR_MSG, MsgCode.SIGN_PARAM_ERROR_CODE);

            }

    // 验证签名

              streamStr = Base64.encode(streamStr);

    if(!SignUtil.checkSign(streamStr, APPIConstants.Filter.key, timestamp, sign)) {

    //logger.error("{}-签名错误", MsgCode.SIGN_ERR_CODE);

    thrownewBusinessException(MsgCode.SIGN_ERR_MSG, MsgCode.SIGN_ERR_CODE);

            }

        }

    验证工具类: 

    publicclassSignUtil{

    privatefinalstaticLogger logger = LoggerFactory.getLogger(SignUtil.class);

    /**

    *@Description验证签名

    *@paramcontent 验签内容

    *@paramkey 签名key值

    *@paramadditionContent 附加的签名信息

    *@paramsign 待验证的签名

    */

    publicstaticbooleancheckSign(String content, String key, String additionContent, String sign){

    if(StringUtils.isBlank(sign)) {

    returnfalse;

    }

    String _sign = createSign(content, key, additionContent);

    if(sign.equalsIgnoreCase(_sign)) {

    returntrue;

    }

    logger.error("参数sign:{},验签sign:{}", sign, _sign);

    returnfalse;

    }

    /**

    *@Description生成签名

    *@paramcontent 签名内容

    *@paramkey 签名key值

    */

    publicstaticStringcreateSign(String content, String key, String additionContent){

    if(StringUtils.isEmpty(additionContent)){

    returnMD5Util.getMd5(content + key);

    }else{

    returnMD5Util.getMd5(content + key + additionContent);

    }

    }

    }

    相关文章

      网友评论

          本文标题:校验时间戳和签名(转)

          本文链接:https://www.haomeiwen.com/subject/zmvgjctx.html