Download the PHP package phper666/jwt-auth without Composer

On this page you can find all versions of the php package phper666/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

基于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、只支持php >= 7.4版本,如果php < 7.4,请使用3.x版本
2、支持多应用单点登录、多应用多点登录
3、如有建议欢迎给我邮件,[email protected]
4、hyperf请使用2.x版本以上

你可以:

说明:

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

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

多应用多点登录:在该配置应用下token 不做限制,一旦刷新 token ,则当前配置应用的 token 会失效

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

多应用单点登录原理:JWT 有七个默认字段供选择。单点登录主要用到 jti 默认字段,jti 字段的值默认为缓存到redis中的key(该key的生成为场景值+存储的用户id(sso_key)),这个key的值会存一个签发时间,token检测会根据这个时间来跟token原有的签发时间对比,如果token原有时间小于等于redis存的时间,则认为无效

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

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

使用:

1、拉取依赖

使用 Hyperf 2.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 数据。 例如:JWTUtil::getParserData($this->request)

12、如何支持每个场景生成的token不能互相访问各个应用

具体你可以查看Phper666\JWTAuth\Middleware\JWTAuthSceneDefaultMiddleware和Phper666\JWTAuth\Middleware\JWTAuthSceneApplication1Middleware这两个中间件,根据这两个中间件你可以编写自己的中间件来支持每个场景生成的token不能互相访问各个应用

13、建议

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

14、更新
15、兼容迁移

All versions of jwt-auth with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4
ext-swoole Version >=4.4
lcobucci/jwt Version 4.1.5
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 phper666/jwt-auth contains the following files

Loading the files please wait ....