6

使用SoapHeader实现Soap请求验证

 3 years ago
source link: https://blogread.cn/it/article/1258?f=hot1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

使用SoapHeader实现Soap请求验证

浏览:4603次  出处信息

在PHP的Soap Extension中, 对于SoapServer来说, 并没有方法可用得到/处理客户端发送的SoapHeader信息.
网上也有很多人认为, 只能通过读取POST过来的请求XML文件, 分析, 才能得到客户端发送过来的SoapHeader.

但, 其实在SoapServer端, 其实是有一种办法, 可用把SoapHeader当作一个请求来处理, 从而获取到客户端提交的SoapHeader信息.

假设客户端代码如下:

  1. <?php
  2. * 保存用户名和密码的载体
  3. class SoapUserInfo {
  4.       * @var char $name
  5.     public $name;
  6.       * @var char $password
  7.     public $password;
  8.     public function __construct($l, $p) {
  9.         $this->Password = $p;
  10.         $this->Username = $l;

然后客户端生成SoapHeader

  1. <?php
  2.     $soap_header = new SoapHeader("http://www.laruence.com", 'Authorise'
  3.                , new SoapUserInfo('laruence', 'password'), false, SOAP_ACTOR_NEXT);

也许细心的同学会注意到第4个参数FALSE第5个参数SOAP_ACTOR_NEXT, 这是什么呢? 我最后再讲.

然后, 创建客户端, 绑定SoapHeader

  1. <?php
  2.     $client = new SoapClient($wsdl);
  3.     $client->__setSoapHeaders(array($soap_header));
  4.     $client->__soapCall('request', array());

现在, 客户端已经发起了请求, 请求中也包含了SoapHeader, 其中有了我们验证需要的用户名/密码信息.

那么, 在服务端, 该如何做呢?

  1. <?php
  2. $server = new SoapServer('laruence.wsdl');
  3. $server->setClass('InterfaceClass');
  4. $server->handle();

关键的地方就在, 服务端接收请求以后, 会实例化一个处理类, 然后分析SoapHeader, 接着就会调用InterfaceClass::Authorise这个方法(Authorise是我们请求头中的变量名), 所以, 我们就可用在InterfaceClass类中, 定义个Authorise方法, 并在这个方法中对SoapHeader中的信息做验证.

然后, 请求体(Soap body)中的方法被调用, 因为不论Authorise方法返回什么(除非exit), 请求体中的方法一定会被调用, 所以要寻找个变量记录验证的结果.

  1. <?php
  2. class InterfaceClass {
  3.       * @var bool $authorized
  4.     private $authorized = FALSE;
  5. * Authentication function
  6. * @param string username
  7. * @param string password
  8.     public function Authentication($username, $password) {
  9.           $this->authorized = validateUser($username, $password);
  10. * Test method
  11.     public function request(){
  12.           if ($this->authorized) {
  13.                //验证成功, 继续处理.
  14.           } else {
  15.                //验证失败, 拒绝请求.

当然, 对于网上说的另外一种方法, 通过分析请求的XML文件, 也可以:

  1. <?php
  2. class InterfaceClass {
  3.       * @var bool $authorized
  4.     private $authorized = FALSE;
  5.     function __construct() {
  6.          $xml = file_get_contents('php://input');
  7.          //分析xml, 获得SoapHeader数据, 验证

Must Understand

这个参数指明了, 是否服务端必须要了解SoapHeader, 如果这个参数为真, 而服务端并不能识别响应的Header, 则会引发一个Soap Fault(Header not understood).

SOAP_ACTOR_NEXT

actor指明了SoapHeader要传递给谁, 被谁处理.

SOAP_ACTOR_NEXT的意思就是, 下一个接受到这个请求头的Service, 在本文的例子中只有一个Server,当然也就没有关系了.

在SoapServer的构造函数中, 我们可以指明一个Server的Actor, 比如:

  1. <?php
  2. $server = new SoapServer($wsdl, array('actor' => 'laruence'));

这样, 我们就可以在Client的SoapHeader中, 通过设置actor是laruence, 来让指定的Server来获得我们设置的头部的信息.

觉得文章有用?立即:

和朋友一起 共学习 共进步!

建议继续学习:

QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK