我正在建立一个基于slim3框架的网上商店。我需要处理服务器到服务器的POST请求以确认付款是否成功。我将csrf添加到容器中,如下所示:
$container['csrf'] = function($container) {
return new \Slim\Csrf\Guard;
};
并将其添加到应用中,如下所示:
$app->add($container->csrf);
它运作良好。但是现在我需要能够在某个路径上添加一个例外,这样我才能得到他们发送的帖子请求。 到目前为止,我找不到合适的解决方案。
有什么建议吗?
答案 0 :(得分:5)
如果您需要从中间件中排除一条路线,则有两种选择:
选项1:对您的路线进行分组。
您可以将所有路线分组,但不包括:
<?php
$app->group('', function() {
// All routes declarations....
})->add($container->csrf); // Add middleware to all routes within the group
// Declare your "exceptional" route outside the group
$app->post('my-special-route-that-has-no-csrf-middleware', 'routeProcessor');
选项2:使用您自己的中间件
不要直接使用\Slim\Csrf\Guard
,而是使用扩展它的中间件。您的中间件将检查路由,如果路由是&#34;例外&#34;一,它会跳过。
将此添加到设置中,因为您需要访问中间件中的路由:
$container['settings'] => [
'determineRouteBeforeAppMiddleware' => true
];
创建扩展orginial \Slim\Csrf\Guard
的中间件:
<?php
class MyCsrfMiddleware extends Slim\Csrf\Guard
{
// This method is processing every request in your application
public function processRequest($request, $response, $next) {
// Check if it's your "exceptional" route
$route = $request->getAttribute('route');
if ($route == 'my-special-path') {
// If it is - just pass request-response to the next callable in chain
return $next($request, $response);
} else {
// else apply __invoke method that you've inherited from \Slim\Csrf\Guard
return $this($request, $response, $next);
}
}
}
/////////////
$container['csrf'] = function($container) {
return new MyCsrfMiddleware; // Now the container returns your middleware under 'csrf' key
};
现在只需将中间件添加到\Slim\App
实例:
$app->add('csrf:processRequest');
答案 1 :(得分:1)
如果有人还在讨论这个问题(特别是如果你想使用webhooks),那就不行了。
我在Georgy的回答的帮助下找到了一个更简单的解决方案。
只需对实际的 Slim \ Csrf \ Guard&#39; Guard.php&#39; 文件及其 __ invoke 方法进行以下修改。或者只是复制并粘贴以下代码......
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
$route = $request->getAttribute('routeInfo');
$routeRequestInfo = $route['request'];
$requestUrl = $routeRequestInfo[1];
if ($requestUrl == 'http://yoursite/the-url-you-want-to-exempt')
{
//This will just return the request to your application with applying the csrf.
return $next($request, $response);
}
else
{
$this->validateStorage();
// Validate POST, PUT, DELETE, PATCH requests
if (in_array($request->getMethod(), ['POST', 'PUT', 'DELETE', 'PATCH'])) {
$body = $request->getParsedBody();
$body = $body ? (array)$body : [];
$name = isset($body[$this->prefix . '_name']) ? $body[$this->prefix . '_name'] : false;
$value = isset($body[$this->prefix . '_value']) ? $body[$this->prefix . '_value'] : false;
if (!$name || !$value || !$this->validateToken($name, $value)) {
// Need to regenerate a new token, as the validateToken removed the current one.
$request = $this->generateNewToken($request);
$failureCallable = $this->getFailureCallable();
return $failureCallable($request, $response, $next);
}
}
// Generate new CSRF token if persistentTokenMode is false, or if a valid keyPair has not yet been stored
if (!$this->persistentTokenMode || !$this->loadLastKeyPair()) {
$request = $this->generateNewToken($request);
}
// Enforce the storage limit
$this->enforceStorageLimit();
}
return $next($request, $response);
}
答案 2 :(得分:0)
$container['csrf'] = function($container) {
$guard = new \Slim\Csrf\Guard;
$guard->setFailureCallable(function($request, $response, $next) use ($container) {
$request = $request->withAttribute("csrf_status", false);
if($request->getAttribute('csrf_status') === false) {
if($request->getAttribute('route')->getName()=== 'your-route-name'){
return $next($request, $response);
}
return $response->withStatus(400)->withRedirect($container['router']->pathFor('home'));
} else {
return $next($request, $response);
}
});
return $guard;
};