[PHP] - Laravel - CSRF token禁用方法
前文
CSRF攻击和漏洞的参考文章:
http://www.cnblogs.com/hyddd/archive/2009/04/09/1432744.html
Laravel默认是开启了CSRF功能,需要关闭此功能有两种方法:
方法一
打开文件:app\Http\Kernel.php
把这行注释掉:
'App\Http\Middleware\VerifyCsrfToken'
方法二
打开文件:app\Http\Middleware\VerifyCsrfToken.php
修改为:
<span style="color: #000000;">php namespace App\Http\Middleware; </span><span style="color: #0000ff;">use</span><span style="color: #000000;"> Closure; </span><span style="color: #0000ff;">use</span> Illuminate\Foundation\Http\Middleware\VerifyCsrfToken <span style="color: #0000ff;">as</span><span style="color: #000000;"> BaseVerifier; </span><span style="color: #0000ff;">class</span> VerifyCsrfToken <span style="color: #0000ff;">extends</span><span style="color: #000000;"> BaseVerifier { </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> handle(<span style="color: #800080;">$request</span>, Closure <span style="color: #800080;">$next</span><span style="color: #000000;">) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 使用CSRF //return parent::handle($request, $next); // 禁用CSRF</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$next</span>(<span style="color: #800080;">$request</span><span style="color: #000000;">); } }</span>
CSRF的使用有两种,一种是在HTML的代码中加入:
<span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="hidden"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="_token"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="{{ csrf_token() }}"</span> <span style="color: #0000ff;">/></span></span>
另一种是使用cookie方式。
使用cookie方式,需要把app\Http\Middleware\VerifyCsrfToken.php修改为:
<span style="color: #000000;">php namespace App\Http\Middleware; </span><span style="color: #0000ff;">use</span><span style="color: #000000;"> Closure; </span><span style="color: #0000ff;">use</span> Illuminate\Foundation\Http\Middleware\VerifyCsrfToken <span style="color: #0000ff;">as</span><span style="color: #000000;"> BaseVerifier; </span><span style="color: #0000ff;">class</span> VerifyCsrfToken <span style="color: #0000ff;">extends</span><span style="color: #000000;"> BaseVerifier { </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> handle(<span style="color: #800080;">$request</span>, Closure <span style="color: #800080;">$next</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">return</span> parent::addCookieToResponse(<span style="color: #800080;">$request</span>, <span style="color: #800080;">$next</span>(<span style="color: #800080;">$request</span><span style="color: #000000;">)); } }</span>
使用cookie方式的CSRF,可以不用在每个页面都加入这个input的hidden标签。
当然,也可以对指定的表单提交方式使用CSRF,如:
<span style="color: #000000;">php namespace App\Http\Middleware; </span><span style="color: #0000ff;">use</span><span style="color: #000000;"> Closure; </span><span style="color: #0000ff;">use</span> Illuminate\Foundation\Http\Middleware\VerifyCsrfToken <span style="color: #0000ff;">as</span><span style="color: #000000;"> BaseVerifier; </span><span style="color: #0000ff;">class</span> VerifyCsrfToken <span style="color: #0000ff;">extends</span><span style="color: #000000;"> BaseVerifier { </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> handle(<span style="color: #800080;">$request</span>, Closure <span style="color: #800080;">$next</span><span style="color: #000000;">) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> Add this:</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$request</span>->method() == 'POST'<span style="color: #000000;">) { </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$next</span>(<span style="color: #800080;">$request</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$request</span>->method() == 'GET' || <span style="color: #800080;">$this</span>->tokensMatch(<span style="color: #800080;">$request</span><span style="color: #000000;">)) { </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$next</span>(<span style="color: #800080;">$request</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span><span style="color: #000000;"> TokenMismatchException; } }</span>
只对GET的方式提交使用CSRF,对POST方式提交表单禁用CSRF
修改CSRF的cookie名称方法
通常使用CSRF时,会往浏览器写一个cookie,如:
要修改这个名称值,可以到打开这个文件:vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php
找到”XSRF-TOKEN“,修改它即可。
当然,你也可以在app\Http\Middleware\VerifyCsrfToken.php文件中,重写addCookieToResponse(...)方法做到。
另外,如需要对指定的页面不使用CSRF,可以参考如下文章:
http://www.camroncade.com/disable-csrf-for-specific-routes-laravel-5/

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

Laravel is suitable for projects that teams are familiar with PHP and require rich features, while Python frameworks depend on project requirements. 1.Laravel provides elegant syntax and rich features, suitable for projects that require rapid development and flexibility. 2. Django is suitable for complex applications because of its "battery inclusion" concept. 3.Flask is suitable for fast prototypes and small projects, providing great flexibility.

The future of C will focus on parallel computing, security, modularization and AI/machine learning: 1) Parallel computing will be enhanced through features such as coroutines; 2) Security will be improved through stricter type checking and memory management mechanisms; 3) Modulation will simplify code organization and compilation; 4) AI and machine learning will prompt C to adapt to new needs, such as numerical computing and GPU programming support.

C isnotdying;it'sevolving.1)C remainsrelevantduetoitsversatilityandefficiencyinperformance-criticalapplications.2)Thelanguageiscontinuouslyupdated,withC 20introducingfeatureslikemodulesandcoroutinestoimproveusabilityandperformance.3)Despitechallen

Laravel optimizes the web development process including: 1. Use the routing system to manage the URL structure; 2. Use the Blade template engine to simplify view development; 3. Handle time-consuming tasks through queues; 4. Use EloquentORM to simplify database operations; 5. Follow best practices to improve code quality and maintainability.

Laravel's MVC architecture improves the structure and maintainability of the code through models, views, and controllers for separation of data logic, presentation and business processing. 1) The model processes data, 2) The view is responsible for display, 3) The controller processes user input and business logic. This architecture allows developers to focus on business logic and avoid falling into the quagmire of code.
