在Laravel中处理过期的令牌 [英] Handling expired token in Laravel

查看:83
本文介绍了在Laravel中处理过期的令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在laravel 5中处理过期令牌的最佳方法是什么?

What is the best way to handle expired tokens in laravel 5.

我的意思是我有一个页面,它包含一些执行ajax请求的链接.当页面加载时,它们工作正常,但是当我等待一段时间后,出现TOKEN MISMATCH错误.

I mean I have a page and it has some links which perform ajax requests. They work fine when the page is loaded but when I wait for sometime then I get a TOKEN MISMATCH error.

现在,我必须刷新页面以使其再次运行.但是,我不想刷新页面.我想要一种刷新令牌的方法或其他解决方法来修复它.

Now, I have to refresh the page to make it work again. BUT, I don't want to refresh the page. I want some way to refresh the token or some other work around to make it fix.

我希望你明白我的意思.

I hope you got my point.

推荐答案

一种解决方法是,实际上每隔一定时间获取新令牌,否则就会破坏csrf令牌的用途:

a work around for it, is to actually get the new token every certain time, otherwise you are defeating the purpose of the csrf token:

<html>
    <head>
        <meta name="csrf_token" content="{{ csrf_token() }}">
    </head>
    <body>
        <script type="text/javascript">
            var csrfToken = $('[name="csrf_token"]').attr('content');

            setInterval(refreshToken, 3600000); // 1 hour 

            function refreshToken(){
                $.get('refresh-csrf').done(function(data){
                    csrfToken = data; // the new token
                });
            }

            setInterval(refreshToken, 3600000); // 1 hour 

        </script>
    </body>
</html>

在拉拉维(Laravel)路线

In laravel routes

Route::get('refresh-csrf', function(){
    return csrf_token();
});

对于任何语法错误,我深表歉意,已经很长时间没有使用jquery了,但是我想你会明白的

I apologize in case of any syntax errors, haven't used jquery for long time, but i guess you get the idea

这篇关于在Laravel中处理过期的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆