为什么我的 LWP::UserAgent 凭据不起作用?

发布于 2024-08-12 15:12:53 字数 769 浏览 9 评论 0原文

我正在尝试访问受保护的文件。服务器正在使用摘要身份验证 - 我可以从打印输出的响应中看到这一点。 以下是示例代码:

use LWP;
use strict;

my $url = 'http://somesite.com/aa/bb/cc.html';
my $username = 'scott';
my $password = 'tiger';

my $browser = LWP::UserAgent->new('Mozilla');
$browser->credentials("http://somesite.com:80","realm-name",$username=>$password);
my $response=$browser->get($url);

print $response->content;

当我尝试从浏览器访问该资源时,我从弹出窗口中获取了领域的名称。相同的用户名和密码在浏览器中运行得非常好,我可以看到内容,但当我运行上面的脚本时,它总是显示 401 Authorization required

LWP 是如何工作的?

我是否需要要求 LWP 发送用户名和密码的 MD5 哈希(摘要),还是像在内部一样检查要使用的身份验证并发送相应的(基本/摘要)发送凭据的方式。 我的问题是

  1. 如何设置 LWP 以便它发送用户名和密码的摘要?
  2. 如果服务器使用 Windows NTLM 身份验证协议怎么办?遇到这样的情况我该怎么办呢?

非常感谢任何快速帮助!

I'm trying to access a protected file. Server is using digest authentication - which I can see from the printed out response.
Here is the sample code:

use LWP;
use strict;

my $url = 'http://somesite.com/aa/bb/cc.html';
my $username = 'scott';
my $password = 'tiger';

my $browser = LWP::UserAgent->new('Mozilla');
$browser->credentials("http://somesite.com:80","realm-name",$username=>$password);
my $response=$browser->get($url);

print $response->content;

Name of the realm I got it from the popup window I get when I try to access that resource from the browser. Same username and password are working extremely fine in the browser and I'm able to see the content but when I run the above script it always says 401 Authorization required.

How does LWP work?

Do I need to ask LWP to send MD5 hash (digest) of the username and password or is it like internally it checks which authentication to use and sends the corresponding (basic/digest) way of sending credentials.
My questions are

  1. How can I set LWP so that it sends digest of username and password?
  2. What if the server is using windows NTLM authentication protocol? How should I go about in such a situation?

any quick help is highly appreciated !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浮生面具三千个 2024-08-19 15:12:53

请考虑以下 LWP::UserAgent 模块文档的摘录:

$ua->credentials( $netloc, $realm )
$ua->credentials( $netloc, $realm, $uname, $pass )

获取/设置用于领域的用户名和密码。

$netloc":" 形式的字符串。用户名和密码只会传递到该服务器。示例:

$ua->credentials("www.example.com:80", "Some Realm", "foo", "secret");

更改

$browser->credentials("http://somesite.com:80","realm-name",$username=>$password);

$browser->credentials("somesite.com:80","realm-name",$username=>$password);

Consider the following excerpt from the LWP::UserAgent module's documentation:

$ua->credentials( $netloc, $realm )
$ua->credentials( $netloc, $realm, $uname, $pass )

Get/set the user name and password to be used for a realm.

The $netloc is a string of the form "<host>:<port>". The username and password will only be passed to this server. Example:

$ua->credentials("www.example.com:80", "Some Realm", "foo", "secret");

Change

$browser->credentials("http://somesite.com:80","realm-name",$username=>$password);

to

$browser->credentials("somesite.com:80","realm-name",$username=>$password);
摇划花蜜的午后 2024-08-19 15:12:53

HTTP GET Authed Request 也可以按如下方式完成

use LWP::UserAgent;

my $address = "localhost";
my $port = "8080";
my $username = "admin";
my $pass = "password";

my $browser = LWP::UserAgent->new;
my $req =  HTTP::Request->new( GET => "http://$address:$port/path");
$req->authorization_basic( "$username", "$pass" );
my $page = $browser->request( $req );

HTTP GET Authed Request can also be done as follows

use LWP::UserAgent;

my $address = "localhost";
my $port = "8080";
my $username = "admin";
my $pass = "password";

my $browser = LWP::UserAgent->new;
my $req =  HTTP::Request->new( GET => "http://$address:$port/path");
$req->authorization_basic( "$username", "$pass" );
my $page = $browser->request( $req );
旧时模样 2024-08-19 15:12:53

当您遇到此类问题时,请使用 HTTP 嗅探器来监视事务,以便您可以看到程序发送的标头。在这种情况下,您可能根本没有发送凭据,因为 HTTP 状态是 401 而不是 403。这通常意味着您在凭据方面犯了错误,如 gbacon 在他的回答中指出

When you have these sorts of issues, use an HTTP sniffer to watch the transaction so you can see the headers your program is sending. In this case, you're probably not sending the credentials at all since the HTTP status is 401 instead of 403. That usually means you've made a mistake with the credentials, as gbacon notes in his answer.

撞了怀 2024-08-19 15:12:53

我通过在 Red Hat 7 上安装 perl-NTLM.noarch 解决了这个问题。

I solved this by installing perl-NTLM.noarch on Red Hat 7.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文