更新时间:2024-05-27 GMT+08:00
分享

如何对开放的API接口进行鉴权

操作场景

AstroCanvas提供了开放API的能力,开放API时需要通过AK/SK进行鉴权。本章节主要向您介绍,如何新建访问密钥以及如何进行访问鉴权。

前提条件

只有空间管理员,才可以新建访问密钥,获取AK/SK。

图1 空间管理员

操作步骤

  1. 空间管理员参考如何登录AstroCanvas中操作,登录AstroCanvas界面。
  2. 单击页面右上角的用户名,选择“我的凭证”
  3. 在我的凭证页面,单击“新增访问密钥”

    图2 单击新增访问密钥

  4. 在新增访问密钥页面,输入描述,单击“确定”
  5. 在弹出的创建密钥成功页面,单击“立即下载”,下载密钥。

    图3 下载密钥

    访问密钥只能下载一次,请妥善保管。

  6. 通过AK/SK进行鉴权访问。

    调用AstroCanvas开放API时,需要在Header中添加图4中OpenApi-Authorization。

    图4 添加OpenApi-Authorization
    OpenApi-Authorization: ${method} Access=${Access}, SignedHeaders=${SignedHeaders}, Signature=${Signature}, Timestamp=${Timestamp}

    其中:

    • ${method}:签名的算法名,当前仅支持HmacSHA256。
    • ${Access}:配置为5中获取的Access Key的值。
    • ${SignedHeaders}:Headers中用于签名的字段名,多个字段名之间用“;”分隔。建议使用Headers中不同的KEY,这样每次请求生成的签名会有所不同,以提高保密性。
    • ${Signature}:使用Method算法生成的签名,具体算法如下。
      public class SignatureUtil {
          private static final String HMAC_SHA256_ALGORITHM = "HmacSHA256";
          private static final String REQUEST_TAG = "hws_request";
          private static final String DEFAULT_BUCKET_LOCATION_VALUE = "region";
      
          /**
           * 计算签名值
           *
           * @param string2Sign 待签名字段
           * @param shortDate 时间
           * @param sk security key
           * @param service 服务名称
           * @return 签名
           * @throws NoSuchAlgorithmException NoSuchAlgorithmException
           * @throws InvalidKeyException InvalidKeyException
           */
          public static String calculateSignature(String string2Sign, String shortDate, String sk, String service)
                  throws NoSuchAlgorithmException, InvalidKeyException {
              byte[] dateKey = hmacSha256Encode(("HWS" + sk).getBytes(StandardCharsets.UTF_8), shortDate);
              byte[] dateRegionKey = hmacSha256Encode(dateKey, DEFAULT_BUCKET_LOCATION_VALUE);
              byte[] dateRegionServiceKey = hmacSha256Encode(dateRegionKey, service);
              byte[] signKey = hmacSha256Encode(dateRegionServiceKey, REQUEST_TAG);
              return byte2Hex(hmacSha256Encode(signKey, string2Sign));
          }
      
          /**
           * hmac sha256加密
           * @param key key
           * @param data 数据
           * @return 密文
           * @throws NoSuchAlgorithmException NoSuchAlgorithmException
           * @throws InvalidKeyException InvalidKeyException
           */
          public static byte[] hmacSha256Encode(byte[] key, String data)
                  throws NoSuchAlgorithmException, InvalidKeyException {
              Mac sha256HMAC = Mac.getInstance(HMAC_SHA256_ALGORITHM);
              SecretKeySpec secretKey = new SecretKeySpec(key, HMAC_SHA256_ALGORITHM);
              sha256HMAC.init(secretKey);
              return sha256HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8));
          }
      
          private static String byte2Hex(byte[] bytes) {
              if (bytes == null) {
                  return null;
              }
              if (bytes.length <= 0) {
                  return "";
              }
              StringBuilder sb = new StringBuilder();
              for (byte h : bytes) {
                  String hv = Integer.toHexString(h);
                  if (hv.length() < 2) {
                      sb.append("0");
                  } else if (hv.length() == 8) {
                      hv = hv.substring(6);
                  }
                  sb.append(hv);
              }
              return sb.toString().toLowerCase(Locale.getDefault());
          }
      }
      • “string2Sign”:通过拼接参与计算的Header,得出的值。

        例如,参与签名的Header有如下两个参数:

        Accept-Encoding: gzip, deflate, br 
        Accept-Language: zh-CN,zh;q=0.9

        那么string2Sign的值为“`String.join(${Accept-Encoding},&{Accept-Language})`”,计算后得出的值为:

        gzip, deflate, brzh-CN,zh;q=0.9
      • shortDate:OpenApi-Authorization中传递的Timestamp值。
      • sk:配置为5中获取的Secret Access Key的值。
      • service:固定值,配置为“HUAWEI_ASTRO_CANVAS”

分享:

    相关文档

    相关产品