Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何与JWT一起使用CakePHP3“身份验证”插件_Php_Authentication_Cakephp_Jwt_Cakephp 3.x - Fatal编程技术网

如何与JWT一起使用CakePHP3“身份验证”插件

如何与JWT一起使用CakePHP3“身份验证”插件,php,authentication,cakephp,jwt,cakephp-3.x,Php,Authentication,Cakephp,Jwt,Cakephp 3.x,我已经安装了CakePHP3.8,我需要使用JWT身份验证 我尝试过安装和配置CakePHP/Authentication,但无法配置 我的配置: PHP 7.2.19 MySQL 5.7.27 Apache 2.4.29 CakePHP3.8 身份验证插件1.1 firebase/php jwt 我遵循了指南的配置,并在AppController.php中添加了 // /src/Controller/Appcontroller.php public function initialize()

我已经安装了CakePHP3.8,我需要使用JWT身份验证

我尝试过安装和配置CakePHP/Authentication,但无法配置

我的配置:

PHP 7.2.19

MySQL 5.7.27

Apache 2.4.29

CakePHP3.8

身份验证插件1.1

firebase/php jwt

我遵循了指南的配置,并在AppController.php中添加了

// /src/Controller/Appcontroller.php
 public function initialize()
    {
        parent::initialize();
        $this->loadComponent('Authentication.Authentication', [
            'logoutRedirect' => '/administrators/login'  // Default is false
        ]);
....
在Application.php中

// /src/application.php
class Application extends BaseApplication implements AuthenticationServiceProviderInterface

....

public function getAuthenticationService(ServerRequestInterface $request, ResponseInterface $response)
    {
        $service = new AuthenticationService();
        $service->loadIdentifier('Authentication.JwtSubject');
        $service->loadAuthenticator('Authentication.Jwt', [
            'returnPayload' => false
        ]);
        return $service;
    }

....

public function middleware($middlewareQueue)
    {
....

        // Add the authentication middleware
        $authentication = new AuthenticationMiddleware($this);

        // Add the middleware to the middleware queue
        $middlewareQueue->add($authentication);

        return $middlewareQueue;
    }
如何第一次登录并检索JWT令牌

----------编辑----------

谢谢,您的解决方案非常有效

但是现在我遇到了CORS问题,在得到这个带有CORS错误的try one OPTIONS请求之前

我的AppController中有此CORS策略

        // Accepted all CORS
        $this->response = $this->response->cors($this->request)
            ->allowOrigin(['*'])
            ->allowMethods(['GET','POST','PUT','DELETE','OPTIONS','PATCH']) // edit this with more method
            ->allowHeaders(['X-CSRF-Token']) //csrf protection for cors
            ->allowCredentials()
            ->exposeHeaders(['Link'])
            ->maxAge(60)
            ->build();

您必须自己处理这个问题,即创建一个端点来处理登录请求,并且在成功验证后创建一个包含所需标识符的JWT令牌

例如,对于用户名/密码身份验证,您可以使用表单验证器和密码标识符:

$service->loadIdentifier'Authentication.Password'; $service->loadIdentifier'Authentication.JwtSubject'; $service->loadAuthenticator'Authentication.Form'[ 'loginUrl'=>'/users/login' ]; $service->loadAuthenticator'Authentication.Jwt'[ “returnPayload”=>错误 ]; 使用UsersController中的该示例创建一个类似这样的登录操作,这只是一个非常基本的、希望是自解释的示例,检查身份验证状态,如果有效,生成令牌,如果无效,生成错误:

公共功能登录 { 如果$this->Authentication->getResult->isValid{ $userId=$this->Authentication->getIdentityData'id'; $token=\Firebase\JWT\JWT::encode ['sub'=>$userId], \Cake\Utility\Security::getSalt ; $status=200; $response=[ “令牌”=>$token, ]; }否则{ $status=403; $response=[ '错误'=>'需要身份验证', ]; } 退还$this ->getResponse ->美元状态 ->使用类型“json” ->用stringbodyjson_编码$response; } 如果食谱中有一个完整的令牌身份验证示例,可能不会有什么坏处

另见


添加表单验证器,并具有允许通过用户名/电子邮件和密码登录的操作。登录成功后,您将生成令牌,并在以后的每个请求中使用它。表单验证器未从会话中持久化或读取数据!当它包含表单数据时,它将只为当前请求检索它。我创建的前一个插件还有一个与框架无关的版本,明天我将为JWT添加一个完整的示例:将它添加到文档中,在Cake中应该差不多一样,我建议使用lib而不是Cake插件,因为它不依赖CakePHP版本,当您需要更新框架时,它不会给您带来麻烦。此外,实现得到了清理,并正在使用类型的一切。我爱你,这项工作!谢谢但现在我遇到了CORS问题,请在主要帖子中披露。@DanieleCancani这是一个完全不同的问题,我建议你提出一个新问题,在这个问题上你可以适当地详细说明问题,提供代码,等等。