【已解决】关于http-client 摘要认证请求接口的问题(通过二次请求可以实现)

Gin

问题描述

curl这样设置是可以成功访问的

截图

guzzle 也可以

由于 http-client 无法从服务器获取 nonce 无法生成 digest http-client 无法完成 摘要认证

有没有大佬做过 http-client 摘要认证的 给说说

解决方法

实现方式
先通过第一次请求 获取 返回的头部信息 WWW-Authenticate 拿到 qop nonce realm 然后通过计算得出 response 然后拼接 请求头 进行二次请求

    $http->get('http://192.168.100.236:8686/ISAPI/Event/notification/httpHosts?format=json&devIndex=35BEDA2D-ED63-42CD-9A72-5B086AFDAE15',function($response)use($http,$url){
            $header = $response->getHeaders();

            $wwwAuth = $header['WWW-Authenticate'];
            $arr = explode(' ',$wwwAuth[0]);

            $username = "admin";

            $password = "hik123456";
            $realm   = trim(trim(explode('=',$arr[2])[1],','),'"'); //取出逗号及双引号
            $nonce   = trim(trim(explode('=',$arr[3])[1],','),'"');

            $cnonce = 'apipost';
            $nc = dechex(rand());
            $uri = '/ISAPI/Event/notification/httpHosts?format=json&devIndex=35BEDA2D-ED63-42CD-9A72-5B086AFDAE15';
            $method = 'GET';
            $qop = "auth";

            $A1 = md5($username.":".$realm.":".$password);

            $A2 = md5($method.":".$uri);

            $response3 = md5($A1.":".$nonce.":".$nc.":".$cnonce.':'.$qop.':'.$A2);

            $request = sprintf('Digest username="%s", realm="%s", nonce="%s", uri="%s", algorithm="MD5", qop=%s, nc=%s, cnonce="%s", response="%s", opaque=""', $username, $realm, $nonce, $uri, $qop, $nc,$cnonce, $response3);

            $http->request($url,[
                'method' => 'GET',
                'version' => '1.1',
                'headers' => ['Connection' => 'keep-alive', 'Authorization' => $request],
                //'data' => $data,
                'success' => function ($response2) {
                    echo $response2->getBody();
                },
                'error' => function ($exception) {
                    echo $exception;
                }
            ]);
        }, function($exception){
                echo $exception;
        });
809 0 1
0个回答

年代过于久远,无法发表回答
×
🔝