wirecore/cakephp_jwt 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wirecore/cakephp_jwt

Composer 安装命令:

composer require wirecore/cakephp_jwt

包简介

CakePHP plugin for authenticating users by JSON Web Tokens

README 文档

README

CakePHP_JWT is a Plugin for CakePHP 4 that allows you to authenticate users by JSON Web Tokens

Installation

Use the package manager composer to install CakePHP_JWT.

composer require wirecore/cakephp_jwt

Usage

First you need to load the plugin in the Application.php of your project.

$this->addPlugin('Wirecore/CakePHP_JWT');

After the plugin was loaded you need to load the JWT component of the plugin.

$this->loadComponent("Wirecore/CakePHP_JWT.Jwt");

This was all what you need. All actions are now authentication protected.

Optional you can set some component configuration.

$this->loadComponent("Wirecore/CakePHP_JWT.Jwt", [
    'tokenExpiration' => 900, // default is 900 seconds
    'headerParam' => 'Authorization', // default is Authorization
    'usersTable' => 'Users', // default is Users
    'unauthorizedExceptionText' => 'You are not authorized to access that location', // default is You are not authorized to access that location
    'encryptionKey' => '', // default is used the salt of your application
    'refreshTokenName' => 'refresh_token', // default is refresh_token
    'refreshTokenSecure' => false, // default is false
    'refreshTokenHttpOnly' => true, // default is true
    'hostAddPort' => false // if by generation the refresh token server path is not available, it used the host server variable. by enabling this option it add the current available port to the host 
]);

Methods

To allow actions unauthenticated use this:

$this->Jwt->allowUnauthenticated(['index']);

The Plugin matches automaticly a user by the transferd JWT Token. To get this User use:

$this->Jwt->getIdentity();

To generate a new access token use this:

$this->Jwt->generateAccessToken($userId);

To generate a new refresh token use this:

$this->Jwt->generateRefreshToken($userId);

To set the refresh token cookie use this. The method generate a new refresh token.

$this->Jwt->setRefreshTokenCookie($userId);

To refresh access and refresh token use this method:

$this->Jwt->refreshTokens();

Best Practise

Here is a example of a AuthController that you can use. You must change a bit things but the basic concept should be the same

public function initialize():void{
    parent::initialize();
    $this->Jwt->allowUnauthenticated(['login', 'refreshToken']);
}

public function login(){

    $response = $this->getResponse();
    $data = $this->request->getData();

    // <-- checking user password here

    $userId = 123; // for exmaple here is userId 123

    // password correct
    $token = $this->Jwt->generateAccessToken($userId); // access token for 15 minute authentication
    $this->Jwt->setRefreshTokenCookie($userId); // refresh token for refreshing the access token

    $response = $response->withStatus(200);

    $this->set('token', $token);
    $this->viewBuilder()->setOption('serialize', 'token');
    $this->viewBuilder()->setClassName('Json');
    $this->setResponse($response);

}

public function refreshToken(){

    $response = $this->getResponse();

    // <-- checking user password here

    $token = $this->Jwt->refreshTokens(); // generate a new access token for 15 minutes and actualize the refresh token cookie

    $response = $response->withStatus(200);

    $this->set('token', $token);
    $this->viewBuilder()->setOption('serialize', 'token');
    $this->viewBuilder()->setClassName('Json');
    $this->setResponse($response);

}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Development Setup

In the root folder you find a docker-compose file what you can use to develop the plugin. Follow the introduction at the installation section, and change the following lines in the composer.json of the cakephp installation.

"autoload": {
    "psr-4": {
        "App\\": "src/",
        "Wirecore\\CakePHP_JWT\\": "plugins/Wirecore/CakePHP_JWT/src/",
        "Wirecore\\CakePHP_JWT\\Test\\": "plugins/Wirecore/CakePHP_JWT/tests/"
    }
},

After adding the previous lines you need to run in the php-fpm container.

composer dumpautoload

In addition you find a .devcontainer folder in the project that is recommended for development in VS Code.

License

MIT

统计信息

  • 总下载量: 539
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-05-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固