perl – LWP :: UserAgent HTTP基本身份验证

前端之家收集整理的这篇文章主要介绍了perl – LWP :: UserAgent HTTP基本身份验证前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图运行这个perl5程序:
  1. #!/usr/bin/env perl
  2.  
  3. use strict;
  4. use warnings;
  5. use LWP;
  6.  
  7. my $ua = LWP::UserAgent->new('Mozilla');
  8. $ua->credentials("test.server.com:39272","realm-name",'user_name','some_pass');
  9. my $res = $ua->get('http://test.server.com:39272/');
  10.  
  11. print $res->content;

另一方面我有HTTP ::守护进程:

  1. #!/usr/bin/env perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use HTTP::Daemon;
  7.  
  8. my $hd = HTTP::Daemon->new or die;
  9.  
  10. print "Contact URL: ",$hd->url,"\n";
  11. while (my $hc = $hd->accept) {
  12. while (my $hr = $hc->get_request) {
  13. if ($hr->method eq 'GET') {
  14. print $hr->as_string,"\n";
  15. }
  16. }
  17. $hc->close;
  18. undef($hc);
  19. }

它只是打印:

  1. Contact URL: http://test.server.com:39272/
  2. GET / HTTP/1.1
  3. Connection: TE,close
  4. Host: test.server.com:39272
  5. TE: deflate,gzip;q=0.3
  6. User-Agent: libwww-perl/6.03

所以我看到LWP :: UserAgent不发送HTTP Basic auth,但是我不知道为什么.

我在这个网站上看到一些帖子,但是他们有这个相同的基本代码,它没有
工作…

如果我使用HTTP :: Request,它的工作原理:

  1. my $req = GET 'http://test.server.com:39272/';
  2. $req->authorization_basic('my_id','my_pass');
  3. my $res = $ua->request($req);

输出

  1. GET / HTTP/1.1
  2. Connection: TE,close
  3. Authorization: Basic bXlfaWQ6bXlfcGFzcw==
  4. Host: test.server.com:39272
  5. TE: deflate,gzip;q=0.3
  6. User-Agent: libwww-perl/6.03

之前我做过错了吗?

解决方法

如果服务器告诉它试图访问该领域,LWP将只发送一个领域的凭据.特定用户可能只能访问特定领域或为不同的领域使用不​​同的密码. LWP不知道哪一个在没有领域的情况下选择其凭据.此外,LWP不会使用您存储在凭据中的数据,除非受到挑战.你没有这样做

如果通过指定授权标题直接提供凭据,则不进行任何领域检查.如果您自己设置它,您可以随时发送任何标题,所以看到它并不奇怪.

你只需要一个更好的测试服务器:

  1. use strict;
  2. use warnings;
  3.  
  4. use HTTP::Daemon;
  5. use HTTP::Status;
  6.  
  7. my $server = HTTP::Daemon->new or die;
  8.  
  9. print "Contact URL: ",$server->url,"\n";
  10. while (my $connection = $server->accept) {
  11. while (my $request = $connection->get_request) {
  12. print $request->as_string;
  13. unless( $request->header( 'Authorization' ) ) {
  14. $connection->send_response( make_challenge() )
  15. }
  16. else {
  17. $connection->send_response( make_response() )
  18. }
  19. }
  20. $connection->close;
  21. }
  22.  
  23. sub make_challenge {
  24. my $response = HTTP::Response->new(
  25. 401 => 'Authorization required',[ 'WWW-Authenticate' => 'Basic realm="Buster"' ],);
  26. }
  27.  
  28. sub make_response {
  29. my $response = HTTP::Response->new(
  30. 200 => 'Huzzah!',[ 'Content-type' => 'text/plain' ],);
  31.  
  32. $response->message( 'Huzzah!' );
  33. }

当您运行客户端一次,应该有两个请求:

  1. GET / HTTP/1.1
  2. Connection: TE,close
  3. Host: macpro.local:52902
  4. TE: deflate,gzip;q=0.3
  5. User-Agent: libwww-perl/6.02
  6.  
  7. GET / HTTP/1.1
  8. Connection: TE,close
  9. Authorization: Basic dXNlcl9uYW1lOnNvbWVfcGFzcw==
  10. Host: macpro.local:52902
  11. TE: deflate,gzip;q=0.3
  12. User-Agent: libwww-perl/6.02

猜你在找的Perl相关文章