1.环境要求 PHP ^7.0 || ^8.0thinkphp ^5.1.10 || ^6.0.0 2.安装插件 composer require thans/tp-Jwt-auth//此举将生成jwt.php和.env配置文件php th
composer require thans/tp-Jwt-auth//此举将生成jwt.php和.env配置文件php think jwt:create
jwt.php
return [ 'secret' => env('JWT_SECRET'), //Asymmetric key 'public_key' => env('JWT_PUBLIC_KEY'), 'private_key' => env('JWT_PRIVATE_KEY'), 'password' => env('JWT_PASSWORD'), //JWT time to live 'ttl' => env('JWT_TTL', 15),//token过期时间,方便测试先调整到15秒过期 //Refresh time to live 'refresh_ttl' => env('JWT_REFRESH_TTL', 1), //单位分钟,指定token过期后,多长一段时间内,使用过期的token能够刷新,最好自动刷新,刷新后会在header里面返回,注意保存 //JWT hashing algorithm 'algo' => env('JWT_ALGO', 'HS256'), 'token_mode' =>['header', 'cookie', 'param'], 'blacklist_storage' => thans\jwt\provider\storage\Tp5::class,];
.env
APP_DEBUG = true[JWT]SECRET=ecaff98fa0d92f8abdcc8e1eee590bb4TTL=20
// 中间件配置return [ // 别名或分组 'alias' => [ 'jwtAuth' => \thans\jwt\middleware\JWTAuth::class, 'autoRefreshJwt' => \thans\jwt\middleware\JWTAuthAndRefresh::class ],];
use app\BaseController;use thans\jwt\facade\JWTAuth;class Hello extends BaseController{ protected $middleware = ['autoRefreshJwt']; public function testJwt():void{ $tokenStr = JWTAuth::getToken(); echo $tokenStr; }}
完成!
来源地址:https://blog.csdn.net/y_w_x_k/article/details/126308608
--结束END--
本文标题: 借助ThinkPHP6扩展插件JWT-AUTH实现jwt验证
本文链接: https://www.lsjlt.com/news/392808.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0