【问题标题】:Laravel: disallow direct access to Post method by usersLaravel:禁止用户直接访问 Post 方法
【发布时间】:2019-01-31 15:07:53
【问题描述】:

我有一个用 Laravel 开发的网站。

问题:

我有一个路由方法(POST)

Route::post('/profile/edit/save', 'ProfileController@save');

如果我输入此网址“mywebsite.com/profile/edit/save”,我会收到错误消息 Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException

我的 .env 文件:

 APP_NAME=Laravel
 APP_ENV=production
 APP_DEBUG=false
 APP_LOG_LEVEL=debug
 APP_URL=http://localhost

在我的服务器上运行此代码时出现此错误,在 Localhost 中我没有收到任何错误。

我已经清除了缓存但它不起作用

我该如何解决这个问题?

【问题讨论】:

  • 如果要通过浏览器访问,需要用GET方式声明路由,而不是POST方式。
  • 可能你还没有理解我的问题。如果我网站上的用户或黑客看到表单的源代码,并且可能希望查看将发布数据的 URL,当他在浏览器中键入此 URL 时,他将收到此页面:i.stack.imgur.com/z9Fl8.png
  • 查看此解决方案:laravel.io/forum/…
  • 感谢您的回复我找到了另一个解决方案您可以看到我的回答

标签: laravel laravel-5 laravel-routing laravel-exceptions


【解决方案1】:

我已经通过在 app\Exceptions\handler.php 中添加它来解决它

    public function render($request, Exception $exception)
        {   

             if($exception instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException){
                 return redirect('/');
             }

        return parent::render($request, $exception);
    }

【讨论】:

    【解决方案2】:

    您无法像上面尝试的那样访问帖子网址 您只能通过提交这样的表单来访问...

    <form action="route('post_insert')" method="post">
      {{csrf_field()}}
       <input type="text" />
    <input type="submit"/>
    </form>
    

    您的路线必须包含此名称...

    Route::post('/profile/save', 'ProfileController@save')->name('post_insert');
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    相关资源
    最近更新 更多