Home > Backend Development > PHP Tutorial > Detailed explanation of the local switch of yii2 csrf

Detailed explanation of the local switch of yii2 csrf

*文
Release: 2023-03-19 07:54:01
Original
1656 people have browsed it

This article mainly introduces the example code for partially closing (opening) CSRF verification in yii2. The editor thinks it’s pretty good, so I’d like to share it with you now and give it as a reference. I hope to be helpful.

(1) For global use, we directly set enableCookieValidation to true in the configuration file

request => [ 
  'enableCookieValidation' => true, 
]
Copy after login

If you do not need to use csrf, set 'enableCookieValidation' => false , but this is unsafe, so enableCookieValidation in yii\web\request of yii2 is set to true by default, which means csrf is enabled by default, so we can also not configure this value and enable it by default.

If you enable csrf, because it is global, authentication will be required for any post request, so when we post data, we must set the csrf data to be hidden in the form.

<input type="hidden" name="_csrf" id=&#39;csrf&#39; value="<?= Yii::$app->request->csrfToken ?>">
Copy after login

When posting data, you must post this value. The value generated is request->csrfToken ?>, and an encrypted csrfToken is returned. .

So whether it is a post form or an ajax post, the value of csrfToken must be set, and it must be posted when submitting. If not, an error will occur and authentication will not pass.

(2) If you don’t want to use csrf verification in some controllers, what should you do?

The method is very simple, set it directly

public $enableCsrfValidation = false ,
Copy after login

Because this Controller inherits from yii\web\Controller, it will be equivalent to inheriting from the enableCsrfValidation attribute, then create the controller When the instance is used, the CSRF function will be turned off in this controller, and verification will not be performed when accessing the post of this controller.

For example, when we develop the API, when the WeChat interface needs to post data to our interface, since the WeChat side does not know the csrfToken, when accessing the post data, if it is turned on If it is a global csrf, it will definitely not be accessible successfully. So at this time, you need to turn off the csrf of this API.

3) What if you want to specifically close a certain action?

Sometimes in some functions, we need to turn off csrf verification in a certain action. We know that the verification of csrf is implemented in beforeAction($Action). Next, we can rewrite the beforeAction($action) method in the Controller.

public function beforeAction($action) { 
 
  $currentaction = $action->id; 
 
  $novalidactions = [&#39;dologin&#39;]; 
 
  if(in_array($currentaction,$novalidactions)) { 
 
    $action->controller->enableCsrfValidation = false; 
  } 
  parent::beforeAction($action); 
 
  return true; 
}
Copy after login

The parameter $action passed in is the controller for this access. The instantiated object contains a lot of information, which you can print and see.

First execute $action->id to obtain the current accessed action name. And $novalidactions is an array, which contains the action names. These actions are all operations that you need to turn off CSRF authentication (operations that need to turn off CSRF authentication).

Whether the action currently accessed is in this $novalidactions. If it is, it means that this action needs to turn off the csrf function, so set the controller instance to

$action->controller->enableCsrfValidation = false
Copy after login

Next, parent::beforeAction($action) is executed. At this time, the enableCsrfValidation of the controller instance in the passed in $action has changed to false.

In the end, true must be returned, otherwise, the action operation will not be executed.

(4) What if it is partially turned on?

First set

request => [
&#39;enableCookieValidation&#39; => false,
]
Copy after login

in the configuration file to not use csrf globally.

(a) To enable it in the controller, you only need to set

public $enableCsrfValidation = true
Copy after login

and the entire controller will be enabled

(b) To enable it in the action

public function beforeAction($action) {
$currentaction = $action->id;
$accessactions = [&#39;dologin&#39;];
i f(in_array($currentaction,$accessactions)) {
       $action->controller->enableCsrfValidation = true;
 }

    parent::beforeAction($action);
    return true;
}
Copy after login

$accessactions is the name of the action that needs to enable csrf. Set $action->controller->enableCsrfValidation = true, and the current operation can enable csrf.

Related recommendations:

Detailed explanation of how the Yii framework implements logging to custom files

Detailed explanation of Yii framework's simple extension class for batch insertion of data

##Detailed explanation of restful api authorization verification of yii2

The above is the detailed content of Detailed explanation of the local switch of yii2 csrf. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template