c++ - 无法获得协商身份验证以在 libcurl 程序中工作

标签 c++ http curl kerberos spnego

我目前正在使用 libcurl(版本 7.19.6,内置 SPNEGO 和 GSS-Negotiate 支持) 编写连接到 protected 网页(Kerberos protected) 在 Tomcat 服务器后面。使用命令行:-

curl --negotiate -u: http://prtotectedpage.jsp --verbose

这有效(服务器返回 HTTP 401 未经授权,然后它允许传递和处理 SPNEGO token ,我可以访问 protected 页面)。

但是,当我编写以下代码并尝试时:-

using namespace std;
#include <stdio.h>
#include <curl.h>
#define YOUR_URL "http://protectedpage.jsp"
#define ANYUSER ""

int main(int argc, char* argv[])
{
    __asm int 3;               //a debugging thing

//initialize a curl object
CURLcode result;
int x;
CURL* curl = curl_easy_init();                   
if(curl){
    curl_easy_setopt(curl,CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);         
    curl_easy_setopt(curl,CURLOPT_USERNAME, ANYUSER);                       
    curl_easy_setopt(curl,CURLOPT_VERBOSE, 1);
    curl_easy_setopt(curl, CURLOPT_URL,YOUR_URL);
    curl_easy_perform(curl);
    curl_easy_cleanup(curl);                            
}
return 0;
}

在与页面对应的初始连接(错误 302)暂时移动后,我从服务器收到响应。

有谁知道这是怎么发生的。

其他一些配置供引用 (KDC = Windows server 2008 中的 Windows Active Directory),

curl 版本(7.19.6)

IDE = (Microsoft visual studio)

好的,我对 wireshark 做了更多调查,发现它们的初始请求之间存在以下差异:-

对于命令行(即成功的):-

GET /protected.jsp HTTP 1.1 \r\n
Host : somecomputername
User Agent: curl(7.19.6) (ipc-386-win32) libcurl/7.19.16 OPENSSL/0.9.8K \r\n
Accept */*\r\n
Full request: [http://somecomputername/protected.jsp]

而对于客户端代码(我编写但失败的代码):-

GET /protected.jsp HTTP 1.1 \r\n
Host : somecomputername   
Accept */*\r\n
Full request: [http://somecomputername/protected.jsp]

这意味着用户代理未在程序中传递。我仍在研究它,一些意见将不胜感激

第二次编辑:- 我对两者的详细输出进行了观察:-

对于命令行版本(工作版本)-

> GET /examples/ HTTP/1.1
> User-Agent: curl/7.19.6 (i386-pc-win32) libcurl/7.19.6 OpenSSL/0.9.8k
> Host: somecomputer
> Accept: */*

对于非工作代码(我写的客户端代码):-

> GET /examples HTTP/1.1
Authorization: Basic RGt1bUByMTIzOg==
User-Agent: curl/7.19.6 (i386-pc-win32) libcurl/7.19.6 OpenSSL/0.9.8k
Host: somecomputer
Accept: */*

这两个都是各自 .exe 文件输出的前几行。现在我注意到两件事。默认情况下,失败的一个转到 Basic。两个(这个更令人不安),失败的用户代理和主机行中没有箭头 (>)。这是否意味着永远不会发送用户代理?

最佳答案

好吧,我终于让它工作了,在 n.m 的一些指点和一些在这里和那里的挣扎之后,我终于得到了一个工作代码:-

using namespace std;

#include "stdafx.h"
#include <stdio.h>
#include <curl.h>

#define YOUR_URL "http://invr28ppqa24:8080/examples"
#define ANYUSER ""
#define ANYPWD ""

int main(int argc, char* argv[])
{
//__asm int 3;

//initialize a curl object
CURLcode result;
int x;
CURL* curl = curl_easy_init();                  //initialize a easy curl handle
if(curl){
    curl_easy_setopt(curl,CURLOPT_USERNAME, ANYUSER);                       //set second option to enable anyuser, a trick necessary for program to work
    curl_easy_setopt(curl,CURLOPT_USERNAME, ANYPWD);
    curl_easy_setopt(curl,CURLOPT_VERBOSE, 1);
    curl_easy_setopt(curl, CURLOPT_URL,YOUR_URL);
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, "false");
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.19.6 (i386-pc-win32) libcurl/7.19.6 OpenSSL/0.9.8k");     
    curl_easy_setopt(curl,CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);         //set first option to enable gssnegotiate authentication
    curl_easy_perform(curl);
    //curl_easy_cleanup(curl);
    scanf("%d", &x);                            //last statement used to get delay in demo situations
}
return 0;

302 仍然出现,我必须手动设置用户代理(不太优雅)。因为follow location,基本解决了这个问题。谢谢。

最初的 302 错误仍然存​​在且未得到答复。

关于c++ - 无法获得协商身份验证以在 libcurl 程序中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15494618/

相关文章:

C++ GoogleTest - 静态成员和链接器错误 LNK 2001

json - 如何读取请求 JSON 正文以便在响应请求后使用它?

c - 为什么这两个字符串在c中不相等

php - curl 获取不到外部网站

php - PHP 中的 cURL 重试

c++ - "static property"和静态常量值有什么区别?

c++ - [] 在 C++ 中的奇怪用法。怎么了?

c++ - Win32 LoadBitmap 返回错误 1814

http - 为什么在 Git Bash 上找不到 ncat?

curl - 从 PyPi 下载轮子