SPI 安全令牌
更新时间:2025-03-31 06:58:42
服务商发布的SaaS类商品,接入阿里云市场时,服务商需要进行SPI对接,根据需要实现SPI相关的接口定义。用户在云市场购买对应商品支付后,云市场会回调服务商对应的SPI实现,开通应用实例,从而为用户提供服务。SPI的每次调用都必须进行安全校验,本文为您介绍 SPI 的安全令牌方案。
适用对象
适用主动推送 SPI 方式生产的 SaaS 类商品。
安全方案
服务商收到每次接口调用都必须进行安全校验,SPI的安全令牌就是接口URL的token参数,服务商根据URL的token参数值进行校验。
token值生成方式:取所有的 HTTP GET 请求参数(不包括 token 参数),对参数名进行字典排序,在字符串的最后加上 &key={服务商的安全密钥} ,然后对整个字符串进行 MD5 加密。
云市场在调用接口时,可能会新增其它参数,因此在校验签名时,请取URL的所有请求参数(不包括 token 参数),再按规则生成 token值。
服务商的安全密钥可登录云市场商家后台,在概览页中查看。
token值 = "p1=xxx&p2=xxx&p3=xxx&key=xxx".MD5()
服务商验证token示例
假设最终调用到服务商接口的请求为:
https://example.aliyundoc.com/api?p1=xxx&p2=xxx&p3=xxx&token=xxx
服务商验证token代码示例
/**
* 验证 token
* @param parameters 具体实例生命周期SPI参数
* @param secretKey 服务商密钥
* @param receivedToken 接收到的 token
* @return 验证结果,true 表示验证通过,false 表示验证失败
* @throws Exception
*/
private boolean verifyToken(Map<String, String> parameters, String secretKey, String receivedToken) throws Exception {
// 计算预期的 token
String expectedToken = computeToken(parameters, secretKey);
// 比较接收到的 token 和预期的 token
return expectedToken.equals(receivedToken);
}
/**
* 生成token
* @param parameters 具体实例生命周期SPI参数 {"p1":"xxx","p2":"xxx","p3":"xxx"}
* @param secretKey 服务商密钥
* @return
* @throws Exception
*/
private String computeToken(Map<String, String> parameters, String secretKey) throws Exception {
String[] sortedKeys = parameters.keySet().toArray(new String[]{});
Arrays.sort(sortedKeys);
StringBuilder queryString = new StringBuilder();
for (String key : sortedKeys) {
queryString.append(key).append("=").append(parameters.get(key)).append("&");
}
queryString.append("key").append("=").append(secretKey);
String token = DigestUtils.md5DigestAsHex(queryString.toString().getBytes(Charsets.UTF_8));
log.info("token: {}", token);
return token;
}
该文章对您有帮助吗?
- 本页导读 (0)
- 适用对象
- 安全方案
- 服务商验证token示例