wirecore / cakephp_jwt
CakePHP插件,用于通过JSON Web Tokens进行用户身份验证
0.2.4
2022-09-16 04:18 UTC
Requires
- cakephp/cakephp: ^4.0
- firebase/php-jwt: ^5.2
Requires (Dev)
- phpunit/phpunit: ^8.0
README
CakePHP_JWT 是一个 CakePHP 4 插件,允许您通过 JSON Web Tokens 进行用户身份验证
安装
使用包管理器 composer 安装 CakePHP_JWT。
composer require wirecore/cakephp_jwt
用法
首先,您需要在项目的 Application.php 中加载此插件。
$this->addPlugin('Wirecore/CakePHP_JWT');
插件加载后,您需要加载插件的 JWT 组件。
$this->loadComponent("Wirecore/CakePHP_JWT.Jwt");
这就是您需要做的全部。所有操作现在都受到身份验证保护。
可选地,您可以设置一些组件配置。
$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 ]);
方法
要允许未认证的操作,使用以下方法
$this->Jwt->allowUnauthenticated(['index']);
插件会自动通过传递的 JWT Token 匹配用户。要获取此用户,请使用以下方法
$this->Jwt->getIdentity();
要生成新的访问令牌,请使用以下方法
$this->Jwt->generateAccessToken($userId);
要生成新的刷新令牌,请使用以下方法
$this->Jwt->generateRefreshToken($userId);
要设置刷新令牌cookie,请使用以下方法。该方法将生成新的刷新令牌。
$this->Jwt->setRefreshTokenCookie($userId);
要刷新访问和刷新令牌,请使用此方法
$this->Jwt->refreshTokens();
最佳实践
以下是一个 AuthController 的示例,您可以使用它。您需要稍作修改,但基本概念应该是相同的
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); }
贡献
欢迎提交拉取请求。对于重大更改,请先提交一个问题以讨论您希望更改的内容。
请确保根据需要更新测试。
开发设置
在根目录中,您会找到一个可以用于开发插件的 docker-compose 文件。遵循安装部分的介绍,并在 CakePHP 安装的 composer.json 中更改以下行。
"autoload": { "psr-4": { "App\\": "src/", "Wirecore\\CakePHP_JWT\\": "plugins/Wirecore/CakePHP_JWT/src/", "Wirecore\\CakePHP_JWT\\Test\\": "plugins/Wirecore/CakePHP_JWT/tests/" } },
添加上述行后,您需要在 php-fpm 容器中运行。
composer dumpautoload
此外,在项目中还有一个 .devcontainer 文件夹,建议在 VS Code 中进行开发。