thinkphp6.0 接入阿里云云盾人機驗證

在這裏插入圖片描述
下載阿里雲的官方sdk
解壓之後,把aliyun-php-sdk-afs這個文件夾放到extend裏面
在這裏插入圖片描述
再把aliyun-php-sdk-core這個文件夾放進,自己驗證邏輯的控制器目錄中!
完整的引入代碼

<?php
declare (strict_types=1);

namespace app\api\controller;

use app\Request;
use think\annotation\Route;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
include(app()->getRootPath() . '/app/api/aliyun-php-sdk-core/Config.php'); // 修改成你自己的路徑
use DefaultProfile;
use DefaultAcsClient;

use afs\Request\V20180112 as Afs;


如果有報錯提示 類名重複或者常量名重複,可以在重名的位置加上AS,如下,再將所有引用函數/常量的位置修改成你重命名的名字。
或者在config.php的第一行加上error_reporting(0);
例如:

<?php
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
error_reporting(0);
include_once 'Autoloader/Autoloader.php';
include_once 'Regions/EndpointConfig.php';
include_once 'Regions/LocationService.php';

//config sdk auto load path.
Autoloader::addAutoloadPath("aliyun-php-sdk-ecs");
Autoloader::addAutoloadPath("aliyun-php-sdk-batchcompute");
Autoloader::addAutoloadPath("aliyun-php-sdk-sts");
Autoloader::addAutoloadPath("aliyun-php-sdk-push");
Autoloader::addAutoloadPath("aliyun-php-sdk-ram");
Autoloader::addAutoloadPath("aliyun-php-sdk-ubsms");
Autoloader::addAutoloadPath("aliyun-php-sdk-ubsms-inner");
Autoloader::addAutoloadPath("aliyun-php-sdk-green");
Autoloader::addAutoloadPath("aliyun-php-sdk-dm");
Autoloader::addAutoloadPath("aliyun-php-sdk-iot");
Autoloader::addAutoloadPath("aliyun-php-sdk-jaq");
Autoloader::addAutoloadPath("aliyun-php-sdk-cs");
Autoloader::addAutoloadPath("aliyun-php-sdk-live");
Autoloader::addAutoloadPath("aliyun-php-sdk-vpc");
Autoloader::addAutoloadPath("aliyun-php-sdk-kms");
Autoloader::addAutoloadPath("aliyun-php-sdk-rds");
Autoloader::addAutoloadPath("aliyun-php-sdk-slb");
Autoloader::addAutoloadPath("aliyun-php-sdk-cms");
Autoloader::addAutoloadPath("aliyun-php-sdk-idst");
Autoloader::addAutoloadPath("aliyun-php-sdk-afs");

//config http proxy
define('ENABLE_HTTP_PROXY', false);
define('HTTP_PROXY_IP', '127.0.0.1');
define('HTTP_PROXY_PORT', '8888');

在這裏插入圖片描述

控制器驗證邏輯(這裏以滑動驗證爲例子)

$data = $request->post();

接收前端POST傳送的數據,有四個參數是必須的

$requests->setSessionId($data["sessionId"]);// 必填參數,從前端獲取,不可更改,android和ios只傳這個參數即可
$requests->setToken($data["token"]);// 必填參數,從前端獲取,不可更改
$requests->setSig($data["sig"]);// 必填參數,從前端獲取,不可更改
$requests->setScene($data["scene"]);// 必填參數,從前端獲取,不可更改

接口:

/**
     * @param Request $request
     * @Route("user/smsForRegister")
     */
    public function registerSms(Request $request)
    {
        $data = $request->post();
        return json($this->signCheck($data));
  	}     

驗證邏輯,返回100爲成功!
在這裏插入圖片描述
在這裏插入圖片描述

/**
     * @param $data
     * @return \think\response\Json
     */
    private function signCheck($data)
    {

        $iClientProfile = DefaultProfile::getProfile("cn-hangzhou", $this->ACCESSKEY, $this->SECRET);
        $client = new DefaultAcsClient($iClientProfile);
        DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", "afs", "afs.aliyuncs.com");
        $requests = new Afs\AuthenticateSigRequest();
        $requests->setSessionId($data["sessionId"]);// 必填參數,從前端獲取,不可更改,android和ios只傳這個參數即可
        $requests->setToken($data["token"]);// 必填參數,從前端獲取,不可更改
        $requests->setSig($data["sig"]);// 必填參數,從前端獲取,不可更改
        $requests->setScene($data["scene"]);// 必填參數,從前端獲取,不可更改
        $requests->setAppKey("FFFF0N000000000073D0");//必填參數,後端填寫
        $requests->setRemoteIp("49.233.200.55");//必填參數,後端填寫

        $response = $client->getAcsResponse($requests);//返回code 100表示驗籤通過,900表示驗籤失敗
        print_r($response);
        $result = [
            'code' => 401,
            'msg' => '密匙錯誤!'
        ];
        return json($result);
    }

這裏可以自己做判斷,我就不演示了。

關於前端接入:

header頭部引入

<script type="text/javascript" charset="utf-8" src="//g.alicdn.com/sd/ncpc/nc.js?t=2015052012"></script>

容器:

<div class="el-input item-input" id="check">

                </div>

JS

window.nc_token = ["FFFF0N000000000073D0", (new Date()).getTime(), Math.random()].join(':');
window.NC_Opt =
    {
        renderTo: "#check",
        appkey: "FFFF0N000000000073D0",
        scene: "nc_register",
        token: nc_token,
        customWidth: 300,
        trans: {"key1": "code0"},
        elementID: ["usernameID"],
        is_Opt: 0,
        language: "cn",
        isEnabled: true,
        timeout: 3000,
        times: 5,
        apimap: {
            // 'analyze': '//a.com/nocaptcha/analyze.jsonp',
            // 'get_captcha': '//b.com/get_captcha/ver3',
            // 'get_captcha': '//pin3.aliyun.com/get_captcha/ver3'
            // 'get_img': '//c.com/get_img',
            // 'checkcode': '//d.com/captcha/checkcode.jsonp',
            // 'umid_Url': '//e.com/security/umscript/3.2.1/um.js',
            // 'uab_Url': '//aeu.alicdn.com/js/uac/909.js',
            // 'umid_serUrl': 'https://g.com/service/um.json'
        },
        callback: function (data) {
            window.console && console.log(nc_token)
            window.console && console.log(data.csessionid)
            window.console && console.log(data.sig)
            that.regForm = {
                sessionId: data.csessionid,
                token: nc_token,
                sig: data.sig,
                scene: "nc_register",
                phone: that.regForm.phone,
                code: that.regForm.code
            }
            console.log(that.regForm)
        }
    }
window.nc = new noCaptcha(NC_Opt)
window.nc.upLang('cn', {
    _startTEXT: "請按住滑塊,拖動到最右邊",
    _yesTEXT: "驗證通過",
    _error300: "哎呀,出錯了,點擊<a href=\"javascript:__nc.reset()\">刷新</a>再來一次",
    _errorNetwork: "網絡不給力,請<a href=\"javascript:__nc.reset()\">點擊刷新</a>",
})

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章