laravel - 如何添加csrf token id重定向()->操作

标签 laravel csrf

我有以下代码块:

 return redirect()->action('mycontroller@myfunction',
              [
                  'data0' => $request->data0,
                  'data1' => $request->data1,
                  'data2' => $request->data2,
                  'data3' => $request->data3,

]);

显示错误:MethodNotAllowedHttpException 可能是因为 csrf token 未通过此路由传递,该路由是后路由。

Route::post('congrats', 'mycontroller@myfunction');

我不希望 data0、data1 等值显示在 url 中,所以这就是我希望它成为 POST 的原因。

如何在重定向操作中传递 csrf token ?还有其他方法吗?例如使用 GET 但隐藏特定数据?

最佳答案

像这样编辑您的 VerifyCsrfToken 类以禁用对路由的验证

  protected $except_urls = [
        'contact/create',
        'contact/update',
        ...
    ];

public function handle($request, Closure $next)
{
    $regex = '#' . implode('|', $this->except_urls) . '#';

    if ($this->isReading($request) || $this->tokensMatch($request) || preg_match($regex, $request->path()))
    {
        return $this->addCookieToResponse($request, $next($request));
    }

    throw new TokenMismatchException;
}

并更改内核以指向新的中间件:

protected $middleware = [
...

    'App\Http\Middleware\VerifyCsrfToken',
];

source

编辑

在 Laravel 文档中

将其添加到 VerifyCsrfToken

 protected $except = [
        'stripe/*',
    ];

关于laravel - 如何添加csrf token id重定向()->操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46794285/

相关文章:

mysql - Laravel Schema Builder 改变存储引擎

php - 三向(模型)关系

spring-boot - springdoc-openapi swagger-ui 中的 CSRF 支持

laravel - 如何使用 laravel 将多个图像上传到数据库?

php - 使用 Artisan 的 Laravel 迁移设置失败

php - 代码接收是否正常工作?

php - Laravel 在 api 中间件上启用 csrf 保护

python - django csrf_token 不打印隐藏的输入字段

security - 在单个页面上为多个表单生成 CSRF token

http - CSRF token的理解