java对外接口开发实例

时间:2025-03-19 13:07:50
  1. 使用授权码_secret,从服务代理端获取令牌。
  2. 使用令牌,对服务请求者身份标识(_orgid)、服务标识(_servicecode)和请求时间(_rtime)进行令牌密钥计算,得到令牌密钥信息_token。
  3. 将服务请求者身份标识(_orgid)、服务标识(_servicecode)、请求时间(_rtime)和令牌密钥(_token)放入HTTP请求的Header,发送请求到服务代理平台。

服务代理平台:

  1. 获取请求Header的服务请求者身份标识(_orgid)、服务标识(_servicecode)、请求时间(_rtime)和令牌密钥(_token)等信息,进行权限验证,进行调用频率、调用次数、流量检验。
  2. 验证通过后,从系统中获取被授予的输出参数,将被授予输出参数组成Json数组作为_fieldNames放入发送给提供方服务的HTTP请求Head区。

提供方服务平台:

  1. 获取请求数据进行相关业务处理。
  2. 根据HTTP请求Header区输出参数(_fieldNames)信息过滤输出参数。
  3. 返回调用结果给服务代理平台,再由服务代理平台返回给服务使用方。

 

1)设置签名规则

  public String sign(String orgId, Long timestamp, String serviceCode) {

     
  
        //自定义令牌信息
        String platToken = "034ad87df37fff6154849852d60150717";
        String result = null;
        //时间戳相差超过5分钟,认定为非法操作
        Long timestamp2 = ();
        if ((timestamp2 - timestamp) > 5 * 60 * 1000) {
            return "";
        }
        try {
            Mac hmacSha256 = ("HmacSHA256");
            byte[] keyBytes = ("UTF-8");
            (new SecretKeySpec(keyBytes, 0, , "HmacSHA256"));
            String inputString = serviceCode + orgId + timestamp;
            ("INPUT: " + inputString);
            byte[] hmacSha256Bytes = (("UTF-8"));
            result = new String(Base64.encodeBase64(hmacSha256Bytes), "UTF-8");
            ("OUTPUT: " + result);
            return result;
        } catch (NoSuchAlgorithmException e) {
            ();
            return "";
        } catch (UnsupportedEncodingException e) {
            ();
            return "";
        } catch (InvalidKeyException e) {
            ();
            return "";
        }
    }

 

2)获取令牌信息

 @RequestMapping(value = "/getPlatToken", method = , consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public TokenResult getPlatToken(String orgId, Long timestamp, String serviceCode, String secret) {
 //校验参数信息是否为空
        (!(orgId) && !(timestamp) && !(secret), "参数错误");
        TokenResult result = new TokenResult();
//校验签名信息是否一致
        if ((sign(orgId, timestamp, serviceCode))) {
            //校验用户标识,在本系统用户信息
            SysUserPublic sysUserPublic = (orgId);
            if (sysUserPublic == null) {
                ("0");
                ("用户标识错误");
                return result;
            }
//返回令牌信息
            result = saveToken(orgId);
        } else {
            ("0");
            ("签名错误");
        }
        return result;
    }

 private TokenResult saveToken(String orgId) {
        String token = ().toString();
        ("orgId:" + orgId);
        ("token1>>>:" + token);
        // token有效期为2小时
        Calendar calendar = ();
        (new Date());
        (, 7200);
        Date expireTime = ();
        // 4. 保存token
        ((orgId+token), token, RedisConstant.EXIST_HOUSE_2);
        TokenResult accessToken = new TokenResult();
        ("1");
        (token);
        (expireTime);
        ("返回成功");
        ("token2>>>>:" + 
        ((orgId)));
        return accessToken;
    }

 

3)接口调用示例

   /**
     * add
     */
    @RequestMapping(value = "/add", method = )
    public String orderFlowGeneralSelect(HttpServletRequest request) {
        String token= ("token");
        String orgId= ("orgId");
        //通过orgid获取令牌信息校验
        String token1 = ((orgId+orgId));
        if (token == null || !token1 .equals(token + "")) {
     
            return "token错误,请重新获取";
        }
        return "返回成功";
    }