ellipse / session-validation
Psr-15 中间件,允许验证用户会话所有权
1.0.2
2018-02-23 14:01 UTC
Requires
- php: >=7.0
- psr/http-message: ^1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- eloquent/phony-kahlan: ^1.0
- kahlan/kahlan: ^4.0
This package is auto-updated.
Last update: 2024-08-26 00:06:22 UTC
README
此包提供了一个 Psr-15 中间件,允许验证用户会话所有权。
需求 php >= 7.0
安装 composer require ellipse/session-validation
运行测试 ./vendor/bin/kahlan
使用 validate session 中间件
此中间件允许开发者定义一个可调用对象,从正在处理的 Psr-7 请求中生成客户端签名。此签名存储在会话中,以便后续请求的客户端签名可以与保存的签名进行比较。当它们不匹配时,会话数据将被清除,并且会话 ID 将被重新生成。
传递给中间件的可调用对象将使用正在处理的 Psr-7 请求进行调用,并且必须返回一个包含客户端特定数据的关联数组。例如,它可以是 IP 地址或用户代理。
当然,此中间件可以在 ellipse/session-start 的 StartSessionMiddleware
之后使用,以便启动会话。
<?php namespace App; use Psr\Http\Message\ServerRequestInterface; use Ellipse\Session\ValidateSessionMiddleware; // This callable must return an associative array. $signature = function (ServerRequestInterface $request) { // Here we assume another middleware set the client ip attribute of the request. return [ 'client-ip' => $request->getAttribute('client-ip'), ]; }; // When using this middleware the session data are tied to the client ip address. // Any attempt to access the session data with a different client ip address would // invalidate the client session. Obvioulsy this middleware should only be processed // after the session has started. $middleware = new ValidateSessionMiddleware($signature);