【发布时间】:2019-12-31 23:19:36
【问题描述】:
Slim 4 已经在这里,我正在尝试迁移到 Slim 4。一切都很好,但是当我尝试实现它时 CSRF 返回错误。我尝试了最简单的设置,但出现此错误:
Message:传递给 Slim\Csrf\Guard::__invoke() 的参数 2 必须是 Psr\Http\Message\ResponseInterface 的实例,给定的 Slim\Routing\RouteRunner 的实例,在/Volumes/Web/slim/vendor/slim/slim/Slim/MiddlewareDispatcher.php 在第 180 行
文件:/Volumes/Web/slim/vendor/slim/csrf/src/Guard.php
这是我的代码:
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Slim\Csrf\Guard;
require __DIR__ . '/../vendor/autoload.php';
/**
* Instantiate App
*
* In order for the factory to work you need to ensure you have installed
* a supported PSR-7 implementation of your choice e.g.: Slim PSR-7 and a supported
* ServerRequest creator (included with Slim PSR-7)
*/
$app = AppFactory::create();
$app->add(Guard::class);
// Add Routing Middleware
$app->addRoutingMiddleware();
/*
* Add Error Handling Middleware
*
* @param bool $displayErrorDetails -> Should be set to false in production
* @param bool $logErrors -> Parameter is passed to the default ErrorHandler
* @param bool $logErrorDetails -> Display error details in error log
* which can be replaced by a callable of your choice.
* Note: This middleware should be added last. It will not handle any exceptions/errors
* for middleware added after it.
*/
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
// Define app routes
$app->get('/', function (Request $request, Response $response, $args) {
$response->getBody()->write('Hello');
return $response;
});
// Run app
$app->run();
非常感谢任何帮助!谢谢!
【问题讨论】:
-
看起来您正在尝试实现 CSRF Token 而不是执行 CSRF attack,但您的问题标题暗示您正在尝试执行 csrf 攻击而不是实现令牌跨度>
-
@hanshenrik,我正在尝试实现一个 CSRF 令牌。
标签: php csrf slim middleware slim-4