Java接口签名如何实现

2024-03-14

这篇文章给大家分享的是有关Java接口签名如何实现的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

一、要求

下图为具体要求

二、流程

1、线下分配appid和appsecret,针对不同的调用方分配不同的appid和appsecret

  2、加入timestamp(时间戳),10分钟内数据有效

  3、加入流水号noncestr(防止重复提交),至少为10位。针对查询接口,流水号只用于日志落地,便于后期日志核查。 针对办理类接口需校验流水号在有效期内的唯一性,以避免重复请求。

  4、加入signature,所有数据的签名信息。

三、实现

简单来说,调用者调用接口业务参数在body中传递,header中额外增加四个参数signature、appkey、timestamp、noncestr。

我们在后台取到四个参数,其后三个参数加上调用者分配的appSecret,使用字典排序并使用MD5加密后与第一个参数signature进行比对,一致既表示调用者有权限调用。

以下代码为接口验证签名的demo实现:

 //引用jackson依赖
    @Autowired
    private ObjectMapper objectMapper;
    @Value("${appsecret}")
    private String appSecret;
  
  /**
     * 验证签名
     * @param preInfoItem
     * @return
     */
    boolean checkSignature(PreInfoItem preInfoItem) throws JsonProcessingException, IllegalAccessException {
        String signature="signature";
        String appkey="appkey";
        String timestamp="timestamp";
        String noncestr="noncestr";
        HttpServletRequest request = ServletUtils.getRequest();
        String headerSignature = request.getHeader(signature);
        String headerAppkey = request.getHeader(appkey);
        String headerTimestamp = request.getHeader(timestamp);
        String headerNoncestr = request.getHeader(noncestr);
		//因为需要排序,直接使用TreeMap
        Map<String,Object> parms=new TreeMap<>();
        parms.put(appkey,headerAppkey);
        parms.put(timestamp,headerTimestamp);
        parms.put(noncestr,headerNoncestr);
        Map<String, Object> stringObjectMap = objectToMap(parms, preInfoItem);
        String s = buildSignature(stringObjectMap);
        //签名比对
        if (s.equals(headerSignature)){
            return true;
        }
        return false;
    }
    Map<String,Object> objectToMap(Map<String,Object> map,Object o){
        Field[] declaredFields = o.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            try {
                if (field.getName() instanceof String){
                    map.put(field.getName(),field.get(o));
                }
            }catch (IllegalAccessException e){
                throw new CustomException("对象转map异常");
            }
        }
        return map;
    }
  private String buildSignature(Map<String,Object> maps){
      String s2;
      try {
            StringBuffer s = null;
            String s1 = objectMapper.writeValueAsString(maps);
            //添加appSecret
            s.append(s1).append(appSecret);
             s2 = DigestUtils.md5DigestAsHex(s.toString().getBytes());
        }catch (JsonProcessingException e){
            throw new CustomException("map转json异常");
        }
        return s2;
    }

感谢各位的阅读!关于“Java接口签名如何实现”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

《Java接口签名如何实现.doc》

下载本文的Word格式文档,以方便收藏与打印。