You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<h1><p align="center">JWT-AUTH</p></h1>
<p align="center"> thinkphp的jwtJSON Web Token身份验证包。支持Header、Cookie、Param等多种传参方式。包含验证、验证并且自动刷新等多种中间件。</p>
[thinkphp6.0的demo下载](https://gitee.com/thans/jwt-auth/attach_files/306748/download)
## 支持Swoole
## 环境要求
1. php ^7.0 || ^8.0
2. thinkphp ^5.1.10 || ^6.0.0
## 说明
> 目前支持如下三大类型加密方式RSA,HASH,DSA。再各分256、384、512位。
默认是HS256即hash 256位加密。
>需要修改加密方式请修改参数ALGO参数选项
* HS256
> 备注hash 256位
* HS384
> 备注hash 384位
* HS512
> 备注hash 512位
* RS256
> 备注rsa 256位
* RS384
> 备注rsa 384位
* RS512
> 备注rsa 512位
* ES256
> 备注dsa 256位
* ES384
> 备注dsa 384位
* ES512
> 备注dsa 512位
> 重要RSA和DSA 都是非对称加密方式除了修改参数ALGO外需要配置PUBLIC_KEY、PRIVATE_KEY两个参数
> 这两个参数支持文本不要开头、结尾和换行或密钥文件路径。如果密钥设置了密码请配置好参数PASSWORD
> env文件不支持内容有等于号遇到这种情况
>1、使用路径 2、生成没有等于号的密钥。
## 安装
第一步:
```shell
$ composer require thans/tp-jwt-auth
```
第二步:
```shell
$ php think jwt:create
```
此举将生成jwt.php和.env配置文件。不推荐直接修改jwt.php
同时env中会随机生成secret。请不要随意更新secret也请保障secret安全。
## 使用方式
对于需要验证的路由或者模块添加中间件:
```php
thans\jwt\middleware\JWTAuth::class,
```
示例:
```php
use thans\jwt\facade\JWTAuth;
$token = JWTAuth::builder(['uid' => 1]);//参数为用户认证的信息,请自行添加
JWTAuth::auth();//token验证
JWTAuth::refresh();//刷新token会将旧token加入黑名单
$tokenStr = JWTAuth::token()->get(); //可以获取请求中的完整token字符串
$payload = JWTAuth::auth(); //可验证token, 并获取token中的payload部分
$uid = $payload['uid']->getValue(); //可以继而获取payload里自定义的字段比如uid
```
token刷新说明
> token默认有效期为60秒如果需要修改请修改env文件。
> refresh_ttl为刷新token有效期参数单位为分钟。默认有效期14天。
> token过期后旧token将会被加入黑名单。
> 如果需要自动刷新,请使用中间件 thans\jwt\middleware\JWTAuthAndRefresh::class,
> 自动刷新后会通过header返回请保存好。注意此中间件过期后第一次访问正常第二次进入黑名单。
token传参方式如下
> 可通过jwt.php配置文件内token_mode参数来调整参数接收方式及优先级
> token_mode默认值为['header', 'cookie', 'param'];
> 在某些前后端分离的情况下可选择取消cookie接收方式来避免token冲突
- 将token加入到url中作为参数。键名为token
- 将token加入到cookie。键名为token
- 将token加入header如下Authorization:bearer token值
- 以上三种方式任选其一即可。推荐加入header中。
#### 其他操作
1. 拉黑Token JWTAuth::invalidate($token);
2. 查询Token是否黑名单 JWTAuth::validate($token);
#### 常见问题
- 使用RSA256方式的时候请使用文本形式。如下
![image](https://thans.cn/Snipaste_2020-01-18_17-25-52.png)
## 联系&打赏
[打赏名单](SUPPORT.md)
![image](https://thans.cn/others/thans.jpeg)
## 参考与借鉴
https://github.com/tymondesigns/jwt-auth
## 感谢
- jwt-auth
- php
- lcobucci/jwt
- thinkphp
## 下一步
- 支持动态配置
## License
MIT