Home PHP Framework Laravel Laravel's form forgery and CSRF protection

Laravel's form forgery and CSRF protection

Dec 14, 2020 am 11:23 AM
laravel

We know that the most popular API design specification currently is restFul API design. Restful has five common HTTP methods, namely: get, post, put, patch and delete. It is very easy to construct the get or post method using the html form, but the other three methods are not supported. But in laravel, you can use the other three methods mentioned above through form forgery technology.

Recommended tutorial: "laravel framework"

Preparation work

First we have to prepare Work well done. We need to create two routes: a form route and a route that accepts forms.

// 表单页
Route::get('form', function () {
    return view('form');
});

// 接受表单请求
Route::any('getform', function () {
    return \Illuminate\Support\Facades\Request::method();
});
Copy after login

At the beginning, we made the simplest get request form, the content is as follows:

<form method="get" action="/getform">
    <input type="submit" value="sub" />
</form>
Copy after login

After clicking the submit button, 'GET' appears in the browser, indicating that the get request was successfully sent and accepted. .

CSRF protection

Then, we change to the post method, then refresh and click the submit button to see what happens. You will find an error with "page expired" and status code 419. Why can't laravel accept post requests? Here we will introduce laravel's default CSRF protection mechanism.

In order to prevent cross-site request forgery attacks, laravel provides CSRF token protection. Therefore, for all methods except the get method request, the reader needs to add the CSRF token to the form, as follows:

<input type="hidden" name="_token" value="{{csrf_token()}}">
Copy after login

It also has an abbreviation method, as follows:

@csrf
Copy after login

Turn off the CSRF protection function

It is generally not recommended to turn off the CSRF function of the entire site. It is very simple to turn it off, just comment out the

\App\Http\Middleware\VerifyCsrfToken::class
Copy after login

line in the Kernel.php file.

CSRF whitelist

Often we need to set a group of URLs that do not require CSRF protection, such as API interfaces provided by third parties, we It is hoped that all external API interfaces will not require CSRF protection. Then, you can use the CSRF whitelist function to set the whitelist in the app/Http/Middleware/VerifyCsrfToken.php file. As follows:

class VerifyCsrfToken extends Middleware
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        /* 这里是白名单列表 */
        &#39;http://example.com/api/*&#39;,
        &#39;api/*&#39;,
        &#39;a/b/*&#39;
    ];
}
Copy after login

Note: To facilitate testing, the csrf function will be automatically turned off when testing the environment

##Form forgery

After learning the CSRF protection mechanism, let’s take a look at how to forge the form. It is also very simple to forge a form. You only need to add

<input type="hidden" name="_method" value="PUT">
Copy after login

or abbreviated to

@method(&#39;PUT&#39;)
Copy after login

in the form. The following is a form for forging a put request

@csrf @method(&#39;PUT&#39;)
Copy after login

The above is the detailed content of Laravel's form forgery and CSRF protection. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Laravel - Artisan Commands Laravel - Artisan Commands Aug 27, 2024 am 10:51 AM

Laravel - Artisan Commands - Laravel 5.7 comes with new way of treating and testing new commands. It includes a new feature of testing artisan commands and the demonstration is mentioned below ?

Laravel - Pagination Customizations Laravel - Pagination Customizations Aug 27, 2024 am 10:51 AM

Laravel - Pagination Customizations - Laravel includes a feature of pagination which helps a user or a developer to include a pagination feature. Laravel paginator is integrated with the query builder and Eloquent ORM. The paginate method automatical

How to get the return code when email sending fails in Laravel? How to get the return code when email sending fails in Laravel? Apr 01, 2025 pm 02:45 PM

Method for obtaining the return code when Laravel email sending fails. When using Laravel to develop applications, you often encounter situations where you need to send verification codes. And in reality...

Laravel schedule task is not executed: What should I do if the task is not running after schedule: run command? Laravel schedule task is not executed: What should I do if the task is not running after schedule: run command? Mar 31, 2025 pm 11:24 PM

Laravel schedule task run unresponsive troubleshooting When using Laravel's schedule task scheduling, many developers will encounter this problem: schedule:run...

In Laravel, how to deal with the situation where verification codes are failed to be sent by email? In Laravel, how to deal with the situation where verification codes are failed to be sent by email? Mar 31, 2025 pm 11:48 PM

The method of handling Laravel's email failure to send verification code is to use Laravel...

How to implement the custom table function of clicking to add data in dcat admin? How to implement the custom table function of clicking to add data in dcat admin? Apr 01, 2025 am 07:09 AM

How to implement the table function of custom click to add data in dcatadmin (laravel-admin) When using dcat...

Laravel Redis connection sharing: Why does the select method affect other connections? Laravel Redis connection sharing: Why does the select method affect other connections? Apr 01, 2025 am 07:45 AM

The impact of sharing of Redis connections in Laravel framework and select methods When using Laravel framework and Redis, developers may encounter a problem: through configuration...

Laravel - Dump Server Laravel - Dump Server Aug 27, 2024 am 10:51 AM

Laravel - Dump Server - Laravel dump server comes with the version of Laravel 5.7. The previous versions do not include any dump server. Dump server will be a development dependency in laravel/laravel composer file.

See all articles