Perl SOAP :: LITE - 无法验证?

时间:2013-02-18 12:50:00

标签: perl soap authorization

我正在尝试 - 并且失败 - 使用SOAP :: LITE编写一个简单的测试应用程序来访问HP Service Manager站点。 (我也尝试过SOAP :: WSDL,但是进一步的阅读让我相信我真的不需要做什么。)

我是SOAP的新手,对这些软件包不熟悉,也是我想要做的新事物,所以我不知道我做错了多少事情,但我想我已经把它简化了在我看来应该工作的几行代码。我认为基本问题在于身份验证,但我也考虑过这可能是我试图通过SOAP调用服务管理器例程的方式。

这是我发现的第一种方法:

#!/usr/local/perl5/bin/perl
use SOAP::Lite+trace => 'all';

$USER = "working_user_name";
$PASS = "working_password";

sub SOAP::Transport::HTTP::Client::get_basic_credentials {
        print "###\n";
        print "###\n";
        print "### Authenticating...\n";
        print "###\n";
        print "###\n";
        return $USER => $PASS;
}


my $soap = SOAP::Lite->new(proxy => 'http://<hp_service_manager>:13080/SM/7/ws');
$soap->default_ns('http://schemas.hp.com/SM/7');
my $som = $soap->call('RetrieveList');
die $som->faultstring if ($som->fault);
print $som->result, "\n";

我试过的LWP方法:

#!/usr/local/perl5/bin/perl
use SOAP::Lite+trace => 'all';
use LWP::UserAgent;
use LWP::Debug;
LWP::Debug::level('+');

$USER = "working_user_name";
$PASS = "working_password";

my @ua_args = (keep_alive => 1);
my @credentials = ($SERVICE_NS, "", $USER, $PASS);
my $schema_ua = LWP::UserAgent->new(@ua_args);
$schema_ua->credentials(@credentials);

my $soap = SOAP::Lite->new(proxy => 'http://<hp_service_manager>:13080/SM/7/ws', @ua_args, credentials => \@credentials);
$soap->default_ns('http://schemas.hp.com/SM/7');
my $som = $soap->call('RetrieveList');
die $som->faultstring if ($som->fault);
print $som->result, "\n";

问题在于,无论我做什么,我都会得到“未经授权”的回复。这是HP Service Manager安装。我将为我正在尝试的内容提供WSDL条目。我怀疑它是否有用。

<soap:operation soapAction="RetrieveList" style="document"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>

虽然这里有更多来自哪里,但我相信这是相关的输出:

SOAP::Transport::HTTP::Client::send_receive: HTTP::Request=HASH(0x845c630)
SOAP::Transport::HTTP::Client::send_receive: POST http://<service_manager>:13080/SM/7/ws HTTP/1.1
Accept: text/xml
Accept: multipart/*
Content-Length: 488
Content-Type: text/xml; charset=utf-8
SOAPAction: "#default_ns"

<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><default_ns><c-gensym3 xsi:type="xsd:string">http://schemas.hp.com/SM/7</c-gensym3></default_ns></SOAP-ENV:Body></SOAP-ENV:Envelope>
SOAP::Transport::HTTP::Client::send_receive: HTTP::Response=HASH(0x8597238)
SOAP::Transport::HTTP::Client::send_receive: HTTP/1.1 401 Unauthorized
Connection: close
Date: Fri, 15 Feb 2013 19:42:06 GMT
Server: Apache-Coyote/1.1
WWW-Authenticate: Basic realm="CASM"
Content-Length: 40
Content-Type: text/html;charset=utf-8
Client-Date: Fri, 15 Feb 2013 19:42:07 GMT
Client-Peer: nnn.nn.nn.nn:13080
Client-Response-Num: 1

<HTML><BODY>Not Authorized</BODY></HTML>

有人可以帮忙吗?我已经尝试过几乎所有我能够找到的例子,然后尝试了这些例子的变体,我从来没有设法得到任何进一步的。在每种情况下,我都会收到“未经授权”的回复。

谢谢,

肖恩。

2 个答案:

答案 0 :(得分:4)

不要误导:SOAP :: Lite的调试输出不显示Authorization标头(出于某种原因?)。

“覆盖get_basic_credentials”-method全局设置用户名/密码,无论域和域如何。这实际上是一件坏事......

这段(测试过的)代码应该可以解决您的问题:

#!/usr/local/perl5/bin/perl -w
use strict;
use SOAP::Lite+trace => 'all';

my $soap = SOAP::Lite->new('proxy' => [
                        'http://<yourdomain>:<yourport>/<yourpath>',
                        'credentials' => [
                                "<yourdomain>:<yourport>",
                                "<yourrealm>",
                                "<yourusername>",
                                "<yourpassword>"
                        ]
                ]);
my $som = $soap->RetrieveList();
die $som->faultstring if ($som->fault);
print $som->result, "\n";

<yourrealm>”应设置为“CASM”,“<yourpath>”应设置为“/ SM / 7 / ws”,“<yourport>”应设置为“13080”。

旧回复:请尝试这样做(注意额外的星号):

*SOAP::Transport::HTTP::Client::get_basic_credentials = sub { 
 return ($USER, $PASS);
}

答案 1 :(得分:0)

你找到这个指南了吗? http://www.perl.com/pub/2001/04/24/soap.html#security%20%28ssl,%20basic/digest%20authentication,%20cookiebased%20authentication,%20ticketbased%20authentication,%20access%20control%29

还是这个答案? Adding authHeader to Perl SOAP::Lite request

# create header for requests 
my $authHeader = SOAP::Header->name("xsd:authHeader" => 
\SOAP::Header->value(
    SOAP::Header->name('xsd:user')->value($s7user)->type(''),
    SOAP::Header->name('xsd:password')->value($s7pass)->type(''),
    SOAP::Header->name('xsd:appName')->value('TestApp')->type(''),
    SOAP::Header->name('xsd:appVersion')->value('0.03')->type('')
));

# create data to pass as method paramaters
my $params = SOAP::Data->name('ns:email')->value($s7user)->type('');

# request method
$soap->call('checkLogin', $params, $authHeader);