wsdltophp/wssecurity

允许轻松向 SOAP 请求添加 Ws Security 标头

2.0.6 2024-09-04 18:27 UTC

README

WsSecurity 向您的 SOAP 请求添加 WSSE 认证标头

License Latest Stable Version TeamCity build status Scrutinizer Code Quality Code Coverage Total Downloads StyleCI SymfonyInsight

如何使用

此仓库包含多个类,可以单独使用,但现阶段最好只使用 WsSecurity 类。

WsSecurity 类提供了一个静态方法,该方法接受创建 Ws-Security 用户名认证标头所需的参数。

具体来说,您需要使用 composer 将此仓库包含到项目中(composer require wsdltophp/wssecurity),然后使用它,例如

use WsdlToPhp\WsSecurity\WsSecurity;

/**
 * @var \SoapHeader
 */
$soapHeader = WsSecurity::createWsSecuritySoapHeader('login', 'password', true);
/**
 * Send the request
 */
$soapClient = new \SoapClient('wsdl_url');
$soapClient->__setSoapHeaders($soapHeader);
$soapClient->__soapCall('echoVoid', []);

WsSecurity::createWsSecuritySoapHeader 参数的顺序如下 ($username, $password, $passwordDigest = false, $addCreated = 0, $addExpires = 0, $returnSoapHeader = true, $mustunderstand = false, $actor = null, $usernameId = null, $addNonce = true)

  • $username: 您的登录/用户名
  • $password: 您的密码
  • $passwordDigest: 如果您的密码需要加密,则将其设置为 true
  • $addCreated: 使用 PHP 的 time 函数设置为您创建此标头的时间,否则传递 0
  • $addExpires: 设置为标头将过期的秒数,否则为 0
  • $returnSoapHeader: 如果您想获取用于创建 \SoapVar 对象,并将其用于创建 \SoapHeader 对象,则将其设置为 false,然后您将不得不自己创建 \SoapHeader 对象
  • $mustunderstand: \SoapClient 类的经典选项
  • $actor: \SoapClient 类的经典选项
  • $usernameId: 附加到 UsernameToken 元素的 ID,可选
  • $addNonce: 默认为 true,如果为 true,则向标头添加 nonce 元素,如果为 false,则不添加 nonce 元素到标头

另一种使用方法

创建 Security 类的实例

use WsdlToPhp\WsSecurity\WsSecurity;

$wsSecurity = new WsSecurity('login', 'password', true, /*$addCreated*/ time());

// access its properties to alter them
$wsSecurity->getSecurity()->getTimestamp()->setAttribute('wsu:Id', 'AnyRequestValue');

// Get the SoapHeader
$header = $security->getSoapHeader($returnSoapHeader = true, $mustunderstand = false, $actor = null);

使用 Docker 进行测试

感谢 phpfarm 的 Docker 镜像,您可以使用 cli 在任何 PHP 版本下本地运行测试

  • php-7.4

首先,您需要创建您的容器,您可以使用 docker-compose 在项目的根目录中运行以下命令行来执行此操作

$ docker-compose up -d --build

然后您将有一个名为 ws_security 的容器,您可以在其中运行 composer 命令和 php cli 命令,例如

# install deps in container (using update ensure it does use the composer.lock file if there is any)
$ docker exec -it ws_security php /usr/bin/composer update
# run tests in container
$ docker exec -it ws_security php -dmemory_limit=-1 vendor/bin/phpunit

常见问题解答

如果您有任何问题,请随时 创建问题

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件