简体   繁体   English

Laravel CSRF令牌到期

[英]Laravel CSRF token expire

I am new to Laravel 5.4 and have just created a login form. 我是Laravel 5.4的新手,并且刚刚创建了一个登录表单。 The thing which I have noticed is, if I left the login for some 2 hrs and returned back, my csrf token would get expire. 我注意到的是,如果我离开登录名约2个小时然后返回,我的csrf令牌将失效。 Is it possible to show a javascript alert box that the session has expired and after clicking the OK button it will take me to the login page again? 是否可以在会话结束时显示一个JavaScript警报框,单击“确定”按钮后,它将带我再次进入登录页面?

You can do something like In your view. 您可以按照自己的观点进行操作。 using the auth helper function. 使用auth helper函数。

@if (! auth()->check())
      <script type="text/javascript">
                alert('session expired!!')
      </script>
@endif

Edit: 编辑:

Okay the above function runs only once when the page is loaded and does not meet OPs requirements so to solve this we can try and directly catch the token-mismatch exception in the back-end side. 好的,以上功能仅在页面加载且不满足OP要求时运行一次,因此要解决此问题,我们可以尝试直接在后端捕获token-mismatch异常。

So In method render() of /app/Exceptions/Handler.php you could try something like: 因此,在/app/Exceptions/Handler.php render()方法中,您可以尝试执行以下操作:

if ( $e instanceof \Illuminate\Session\TokenMismatchException ) {
    return redirect()->route('login');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM