Download the PHP package irooit/jwt-auth without Composer

On this page you can find all versions of the php package irooit/jwt-auth. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package jwt-auth

很抱歉,由于我的个人失误导致3.0.3版本的发布使用了master的分支,所以导致代码回滚到了2.0.x,非常抱歉,已经发布了最新的3.0.4,使用了3.0.x最新的代码分支,composer require phper666/jwt-auth:~3.0.0 使用就能更新到

建议使用3.x版本,具体请切换3.0.1的分支查看

基于Hyperf(https://doc.hyperf.io/#/zh/README) 框架的 jwt 鉴权(json web token)组件。

采用基于https://github.com/lcobucci/jwt/tree/3.3 进行封装。

黑名单的设置参考了这篇文章https://learnku.com/articles/17883

注意:

1、由于 Hyperf 可以升级 1.1 版本,如果你用 1.1 版本,请需要修改 jwt-authcomposer.json 文件,把依赖 Hyperf 的组件版本全部改为 ~1.1.0 或者使用 jwt-auth^2.0.1 版本,这个版本是针对 Hyperf1.1 版本的
2、composer.json不在依赖安装hyperf的包,需要自行依赖安装,具体依赖的包如下:

为什么要这样做?因为发现1.1.x和2.0.x的升级不影响该包的代码
如果你使用hyperf 1.1.x,你可以:

如果你使用hyperf 2.0.x,你可以:

说明:

jwt-auth 支持单点登录、多点登录、支持注销 token(token会失效)、支持刷新 token

单点登录:只会有一个 token 生效,一旦刷新 token ,前面生成的 token 都会失效,一般以用户 id 来做区分

多点登录:token 不做限制,一旦刷新 token ,则当前 token 会失效

注意:使用单点登录或者多点登录时,必须要开启黑名单,并且使用 Hyperf 的缓存(建议使用 redis 缓存)。如果不开启黑名单,无法使 token 失效,生成的 token 会在有效时间内都可以使用(未更换证书或者 secret )。

单点登录原理:JWT 有七个默认字段供选择。单点登录主要用到 jti 默认字段,jti 字段的值默认为用户 id。当生成 token 时,getToken 方法有一个 $isInsertSsoBlack 参数来控制是否会把前面生成的 token 都失效,默认是失效的,如果想不失效,设置为 false 即可。但是如果是调用 refreshToken 来刷新 token 或者调用 logout 注销token,默认前面生成的 token 都会失效。
jwt 的生成的 token 加入黑名单时,会把用户 id 作为缓存的键,当前时间作为值,配置文件中的 blacklist_cache_ttl 作为缓存的失效时间。每次生成 token 或者刷新 token 时,会先从 token 中拿到签发时间和 jti 值找到对应的缓存拿到时间,拿到时间后跟 token 的签发时间对比,如果签发时间小于等于拿到的时间值,则 token 判断为失效的。(jti 在单点登录中,存的值是用户 id)

多点登录原理:多点登录跟单点登录差不多,唯一不同的是jti的值不是用户 id,而是一个唯一字符串,每次调用 refreshToken 来刷新 token 或者调用 logout 注销 token 会默认把请求头中的 token 加入到黑名单,而不会影响到别的 token

token 不做限制原理:token 不做限制,在 token 有效的时间内都能使用,你只要把配置文件中的 blacklist_enabled 设置为 false 即可,即为关闭黑名单功能

使用:

1、拉取依赖

如果你使用 Hyperf 1.0.x 版本,则

如果你使用 Hyperf 1.1.x 版本,则

2、发布配置
3、jwt配置

去配置 config/autoload/jwt.php 文件或者在配置文件 .env 里配置

更多的配置请到 config/autoload/jwt.php 查看

4、全局路由验证

config/autoload/middlewaress.php 配置文件中加入 jwt 验证中间件,所有的路由都会进行 token 的验证,例如:

5、局部验证

config/routes.php 文件中,想要验证的路由加入 jwt 验证中间件即可,例如:

6、注解的路由验证

请看官方文档:https://doc.hyperf.io/#/zh/middleware/middleware 在你想要验证的地方加入 jwt 验证中间 件即可。

7、模拟登录获取token

注意:暂时不支持传入用户对象获取 token,后期会支持

7、路由
8、鉴权

在需要鉴权的接口,请求该接口时在 HTTP 头部加入

9、结果
请求:http://{your ip}:9501/login,下面是返回的结果
请求:http://{your ip}:9501/v1/data
10、例子文件
11、获取解析后的 token 数据

提供了一个方法 getParserData 来获取解析后的 token 数据。 例如:$this->jwt->getParserData()

12、建议

目前 jwt 抛出的异常目前有两种类型 Phper666\JwtAuth\Exception\TokenValidExceptionPhper666\JwtAuth\Exception\JWTException,TokenValidException 异常为 token 验证失败的异常,会抛出 401 ,JWTException 异常会抛出 400,最好你们自己在项目异常重新返回错误信息


All versions of jwt-auth with dependencies

PHP Build Version
Package Version
Requires php Version >=7.2
ext-swoole Version >=4.4
lcobucci/jwt Version ~4.1.0
nesbot/carbon Version ^1.0 || ^2.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package irooit/jwt-auth contains the following files

Loading the files please wait ....