如果有人仍在为此烦恼(尤其是如果您想使用 webhook),则不会。
在 Georgy 的回答的帮助下,我找到了一个更简单的解决方案。
只需对实际的Slim\Csrf\Guard 'Guard.php'文件及其__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);
}