Laravel CSRF代币 [英] Laravel CSRF Token
问题描述
我应该在一开始就这么说,我在FronEnd中使用AngularJS,并且我正在通过XHR发出所有请求.
我正在针对每个用户请求使用CSRF Token
开发一个应用程序.
I should have said this at the start, I'm using AngularJS in the FronEnd, and I'm making all the request via XHR.
I'm developing an Application using CSRF Token
for every user request.
每个请求后,我应该重新生成Token
吗?
Should I regenerate the Token
after each request?
类似
Session::forget("_token") and Session::put("_token", RANDOM_SOMETHING)
还是每个用户Session
使用相同的内容就足够了?
Or is it enough to use the same one each user Session
?
有什么好处吗?
推荐答案
使用使用Blades模板的Laravel 5,这非常容易.
With Laravel 5 using Blades templates, it's pretty easy.
如果只需要csrf令牌的值,则可以通过编写以下内容来生成它:
If you only want the value of the csrf token, you can generate it by writing:
{{ csrf_token() }}
它会像这样生成令牌值:
which generates the token value like this:
7YC0Sxth7AYe4RFSjzaPf2ygLCecJhPbyXhz6vvF
如果您使用的是表单,则可以在表单内部添加以下代码行:
If you are using forms, you can add the following line of code inside the form:
{{ csrf_field() }}
这将生成这样的html:
which will generate html like this:
<input type="hidden" name="_token" value="7YC0Sxth7AYe4RFSjzaPf2ygLCecJhblahblah">
这篇关于Laravel CSRF代币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!