douan7529 2018-06-29 05:37
浏览 360
已采纳

即使设置了meta中的csrf_token,Laravel + ReactJS页面也已过期

I'm building a web application using Laravel 5.6 and ReactJS 16.4.1.

I have set the csrf_token on my master.blade.php on the <head> as follows:

<meta name="csrf-token" content="{{csrf_token()}}">

Then on my reactJs, I loaded my registration component like so:

export default class RegistrationForm extends Component {
    render() {
        return (
            <React.Fragment>
                <form method="post" action={this.props.actionUrl}>
                    <div className="form-group">
                      <label htmlFor="txtEmail">Email</label>
                      <input type="email" name="txtEmail" id="txtEmail" className="form-control" placeholder="Email" />
                    </div>
                    <div className="form-group">
                      <label htmlFor="exampleInputPassword1">Password</label>
                      <input type="password" name="txtPassword" id="txtPassword" className="form-control" placeholder="Password" />
                    </div>
                    <div className="text-right">
                        <button type="submit" name="btnRegister" id="btnRegister" className="btn btn-primary">Register</button>
                    </div>
                </form>
            </React.Fragment>
        );
    }
}

Then when I hit the register button, I am being redirected to a The page has expired due to inactivity. Please refresh and try again. page.

I'm not sure why this is still happening even I have the csrf-token meta already.

I have tried this already but still does not work.

But when I remove the VerifyCsrfToken on the Kernel.php, it works. \App\Http\Modules\Common\Middleware\VerifyCsrfToken::class

Is it safe to remove this class? Though I still want to have the csrf validation as to why I do not want to just remove it on the middleware.

  • 写回答

1条回答

  • dtkjthe4025 2018-07-02 03:00
    关注

    I've manually added a hidden field with the {{csrf_token()}} as the value and it now works. It seems that on reactjs, this field should be added manually aside from the meta csrf token.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站