抱石云开放API接入文档

1. 概念介绍&签名验签方法:

参数项 示例 描述
x-app-id bsy123456789 AppKey
sk xxxxxxxxxxx 32位密钥
timestamp 16045601360000 时间戳 (毫秒)
x-nonce-str xxxxxxxx 随机字符串 8位
x-sign-str AAAAAAAA..... 签名,32位字符,算法:md5(x-app-id+timestamp+x-nonce-str+sk),
"+"为字符串连接符;md5之前字符串编码统一使用utf-8编码

2. HTTP签名&请求方法:

验签参数采用在http请求header头信息中增加签名信息的方式验证AKSK,由于http头的键不区分大小写,所以统一使用小写字母,详细参数如下:

http请求协议头(http_request_header) 示例 描述
x-app-id bsy123456789 AK
x-sign-str xxxxxxxxxx 签名,生成规则见上表 (32位字符)
x-timestamp 1604560136000 时间戳(毫秒)
x-nonce-str xxxxxxxx 8位随机字符

3. 示例代码:

  • PHP代码示例:
<?php
/**
 * 签名生成类
 * Class Sign
 */
class Sign
{
    /**
     * 生成 AKSK 签名
     * @param $appId: AK
     * @param $secretKsy: SK
     * @return string
     */
    public function buildSignature($appId, $secretKsy)
    {
        $timestamp = $this->getTimestamp();
        $nonce = $this->getNonce();

        return md5($appId . $timestamp . $nonce . $secretKsy);
    }

    /**
     * 获取当前毫秒时间戳
     */
    protected function getTimestamp()
    {
        list($msec, $sec) = explode(' ', microtime());
        return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    }

    /**
     * 获取随机字符串
     */
    protected function getNonce()
    {
        return uniqid();
    }
}

$obj = new Sign();
$sign = $obj->buildSignature(
    'bsyxxxxxxxxxxxxx',
    'xxxxxxxxxxxxxxxxxxxxxxxxx'
);
  • Java代码示例:

/**
 * api 文档代码示例
 */
public class ClientAuthRequest {
    /**
     * 应用Id
     */
    private String appId;

    /**
     * 时间戳
     */
    private String timestamp;


    /**
     * 随机生成字符串
     */
    private String nonceStr;

    /**
     * 秘钥
     */
    private String secret;

    /**
     * 字符集
     */
    private String charset;


    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }
}

package com.hky.demo;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.UnsupportedEncodingException;

/**
 * api 文档代码示例
 */
public class AkskSignUtil {

    private AkskSignUtil() {
        throw new IllegalStateException("Utility class");
    }

    /**
     * 生成 AKSK 签名
     *
     * @param authRequest 参与签名生成参数
     * @return
     */
    public static String generateSign(ClientAuthRequest authRequest) throws Exception {

        String nonceStr = authRequest.getNonceStr();

        StringBuilder preSignStr = new StringBuilder(authRequest.getAppId())
                .append(authRequest.getTimestamp())
                .append(nonceStr)
                .append(authRequest.getSecret());

        return buildSign(preSignStr.toString(), authRequest.getCharset());
    }


    /**
     * 加密签名
     *
     * @param content 加密前签名字符串
     * @return
     */
    public static String md5Hex(String content) {
        return DigestUtils.md5Hex(content);
    }


    /**
     * 加密签名
     *
     * @param content 加密前签名字符串
     * @param charset 编码
     * @return
     */
    public static String md5Hex(String content, String charset) {
        return DigestUtils.md5Hex(getContentBytes(content, charset));
    }


    /**
     * 构建签名
     *
     * @param preSignStr 构建签名前字符串
     * @param charset    字符集
     * @return String 签名
     */
    public static String buildSign(String preSignStr, String charset) {
        String sign = "";
        if (StrUtil.isBlank(charset)) {
            sign = md5Hex(preSignStr);
        }
        if (StrUtil.isNotBlank(charset)) {
            sign = md5Hex(preSignStr, charset);
        }
        return sign;
    }

    /**
     * 将字符串按照指定字符集转成 byte 数组
     *
     * @param content 生成签名前字符串
     * @param charset 编码
     * @return
     */
    private static byte[] getContentBytes(String content, String charset) {
        if (charset == null || "".equals(charset)) {
            return content.getBytes();
        }
        try {
            return content.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:");
        }
    }

    // 调用示例
    public static void main(String[] args) throws Exception {
        ClientAuthRequest request = new ClientAuthRequest();
        request.setAppId("bsy12345678");
        request.setNonceStr("12345678");
        request.setTimestamp(String.valueOf(System.currentTimeMillis()));
        request.setSecret("e5cc8fc4c8acd2c9ee58d6365f298dc4");
        request.setCharset(CharsetUtil.CHARSET_UTF_8.toString());
        String sign = AkskSignUtil.generateSign(request);
    }
}

Java代码需要依赖如下:

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.4.0</version>
</dependency>
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.11</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.70</version>
</dependency>

results matching ""

    No results matching ""