设备风险SDK 抖音小程序接入

本文档介绍了设备风险SDK (抖音小程序)的接入流程,在需要保护的应用中嵌入小程序 SDK。

背景信息

抖音小程序SDK是专为小程序环境设计的设备风险识别工具,通过嵌入SDK,开发者可以快速集成设备指纹能力,实时获取设备信息并联动风险识别服务。该SDK支持多种场景,如注册、登录、营销活动等,有效防范黑产攻击,提升业务安全性。

基础使用方法

  1. 引入小程序 SDK

    下载抖音小程序SDK,建议在小程序的首屏加载index页面引入SDK,并将AliyunFP对象全局挂载,方便别的页面调用。

    import * as AliyunFP from './feilin.tiktok.xxxx.js';
    const app = getApp();
    app.AliyunFP = AliyunFP;
  2. 添加配置信息

    在调用小程序SDK 接口前,需在对应的 ttml 中加入feilin-view节点:

    <view>
      <view> 业务的 UI 布局 </view>
      
      <view id = "feilin-view" style="position:fixed;top:99999rpx;"> 
        <canvas id="feilin-canvas" type="2d" style="width:150px;height:150px;"></canvas> 
        <canvas id="feilin-webgl" type="webgl" style="width:150px;height:150px;"></canvas>
      </view>
    </view>
  3. 初始化SDK

    引入小程序 SDK之后,在页面加载onReady时初始化SDK。

    App({
      onReady() {
        AliyunFP.init({
          appKey: '这里填写风险识别控制台申请的AppKey',
          appName: '这里填小程序应用名称,客户自定义',
          openId: '小程序用户ID,非必填,建议传入',
          endpoints: ['https://cloudauth-device.aliyuncs.com', 'https://cn-shanghai.device.saf.aliyuncs.com'],
        }, (initStatus, deviceToken) => {
          console.log("initStatus:" + initStatus + "  deviceToken:" + deviceToken);
        });
      }
    });
    重要

    如果不指定服务地址,则endpoints会读取默认配置。请务必将endpoints的域名添加至小程序后台白名单,以确保相关功能正常运行。

    endpoints默认服务地址:

    • 中国内地(默认)服务地址:https://cloudauth-device.aliyuncs.com,https://cn-shanghai.device.saf.aliyuncs.com

    • 国际/中国香港服务地址:https://cloudauth-device.ap-southeast-1.aliyuncs.com,https://ap-southeast-1.device.saf.aliyuncs.com

  4. 获取deviceToken

    获取 deviceToken,建议用 AliyunFP.init 接口回调返回的值,或者在发起业务行为(如注册、登录、订购等)时主动调用 AliyunFP.getToken 获取,AliyunFP.getToken 和 AliyunFP.init 之间至少预留1s左右的时间差。

    AliyunFP.getToken()

接入示例

<view>
  <view class="page-description"> {{ message }} </view>
  <button type="primary" onTap="onLogin">登录</button> 

  <view id = "feilin-view" style="position:fixed;top:99999rpx;"> 
    <canvas id="feilin-canvas" type="2d" style="width:150px;height:150px;"></canvas> 
    <canvas id="feilin-webgl" type="webgl" style="width:150px;height:150px;"></canvas>
  </view>
</view>
import * as AliyunFP from './feilin.tiktok.xxxx.js';
const app = getApp();
app.AliyunFP = AliyunFP;

Page({
  onReady() {
    AliyunFP.init({
      appKey: '这里填写风险识别控制台申请的AppKey',
      appName: '这里填小程序应用名称,客户自定义',
      openId: '小程序用户ID,非必填,建议传入',
      endpoints: ['https://cloudauth-device.aliyuncs.com', 'https://cn-shanghai.device.saf.aliyuncs.com'],
    }, (initStatus, deviceToken) => {
      console.log("initStatus:" + initStatus + "  deviceToken:" + deviceToken);
    });
  },
  onLogin() {
    //某行为点击,这里举例为登录。
    console.info("onLogin!");
    const deviceToken = AliyunFP.getToken();
    //将deviceToken传入客户的服务后台
    const data = {
      "deviceToken": deviceToken,
      "其他业务参数": ""
    };
    //发送后台请求...后台通过deviceToken查询风险信息
  }
});

调用风险识别API接口

deviceToken与其他参数,根据如下相应的风险识别服务事件参数文档说明,请求风险识别API接口进行识别: