Download the PHP package jundayw/laravel-oauth without Composer
On this page you can find all versions of the php package jundayw/laravel-oauth. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-oauth
介绍
为 SPA(单页应用程序)、移动应用程序和基于令牌的、简单的 API 提供轻量级身份验证系统。
允许应用程序的每个用户为他们的帐户生成多个 API 令牌。 这些令牌可以被授予指定允许令牌执行哪些操作的权限。
工作原理
为了解决两个独立问题而生
API 令牌
首先,它是一个简单的包,用于向用户发出 API 令牌,而不涉及 OAuth2
。
这个特性是通过将用户 API 令牌存储在单个数据库表中,并通过包含了有效 API 令牌的 Authorization
标识头对传入的请求进行身份验证而实现的。
SPA 身份验证
其次,提供了一种简单的方法来认证需要与基于 Laravel
的 API 进行通信的单页应用程序 (SPAs)。
这些 SPAs 可能与 Laravel
应用程序存在于同一仓库中,也可能是一个完全独立的仓库,例如使用 Vue CLI
或者 Next.js
创建的单页应用。
安装方法
您可以通过 Composer
软件包管理器安装:
接下来,你需要使用 vendor:publish
Artisan 命令发布的配置和迁移文件。
配置文件将会保存在 config 文件夹中:
或单独发布配置文件
或单独发布迁移文件
最后,您应该运行数据库迁移。
自定义迁移
如果你不想使用默认迁移,你应该在 App\Providers\AppServiceProvider
类的 register
方法中调用 OAuth::ignoreMigrations()
方法。
您可以通过执行以下命令导出默认迁移:
重写 OAuthToken
模型
通常,您应该在应用程序的服务提供器的 boot
方法中调用此方法:
重写 RefreshToken
模型
通常,您应该在应用程序的服务提供器的 boot
方法中调用此方法:
配置
配置文件 config/oauth.php
授权看守器
配置文件 config/auth.php
中, 将授权看守器 guards
的 driver
参数的值设置为 oauth
。
解决冲突
laravel
框架默认内置了(Sanctum
在处理 APP
开发时,无法实现令牌刷新功能)此时使用 OAuth
将造成冲突,解决方案:
使用 HasAccessTokens
功能替代 HasApiTokens
,如果你其他模块使用到了 Sanctum
也可以将 HasApiTokens
删除
发布 API Tokens
允许你发布 API 令牌,用于对你的应用程序的 API 请求进行身份验证。 使用 API 令牌发出请求时,令牌应作为 Bearer
令牌包含在 Authorization
请求头中。
要发布令牌,你可以使用 createToken 方法。 createToken 方法返回一个 Jundayw\LaravelOAuth\Token
实例。
你可以使用 HasAccessTokens
trait 提供的 tokens
Eloquent 关系访问用户的所有令牌:
刷新 API Tokens
令牌作用域
OAuth
内置 scopes
及 scope
中间件:
scopes
中间件可以分配给一个路由,以验证传入请求的令牌是否具有所有列出的能力:
scope
中间件可以分配给一个路由,以验证传入请求的令牌是否具有至少一个列出的能力:
保护路由
最佳实践
第一步:配置文件 config/auth.php
:
第二步:实现接口
第三步:配置文件 routes/web.php
:
All versions of laravel-oauth with dependencies
ext-json Version *