当前位置:网站首页>laravel 8 使用passport 进行Auth验证及颁发token
laravel 8 使用passport 进行Auth验证及颁发token
2022-06-11 15:55:00 【陈卿诺语】
在 Laravel 中,实现基于传统表单的登陆和授权已经非常简单,但是如何满足 API 场景下的授权需求呢?在 API 场景里通常通过令牌来实现用户授权,而非维护请求之间的 Session 状态。在 Laravel 项目中使用 Passport 可以轻而易举地实现 API 授权认证,Passport 可以在几分钟之内为你的应用程序提供完整的 OAuth2 服务端实现。Passport 是基于由 Andy Millington 和 Simon Hamp 维护的 League OAuth2 server 建立的。
安装
在开始之前,请通过 Composer 包管理器安装 Passport:
composer require laravel/passport
aravel 默认使用 utf8mb4 编码,它支持在数据库中储存 emojis 。如果你是在版本低于 5.7.7 的 MySQL 或者版本低于 10.2.2 的 MariaDB 上创建索引,那你就需要手动配置数据库迁移的默认字符串长度。即在 AppServiceProvider 中调用 Schema::defaultStringLength 方法来配置它:
use Illuminate\Support\Facades\Schema;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
Passport 服务提供器使用框架注册自己的数据库迁移目录,因此在注册提供器后,就应该运行 Passport 的迁移命令来自动创建存储客户端和令牌的数据表:
php artisan migrate接下来,运行 passport:install 命令来创建生成安全访问令牌时所需的加密密钥,同时,这条命令也会创建用于生成访问令牌的「个人访问」客户端和「密码授权」客户端:
php artisan passport:install在模型中应用
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;
{方法里
use HasApiTokens;
} 将配置文件 config/auth.php 中授权看守器 guards 的 api 的 driver 选项改为 passport
'api' => [
'driver' => 'passport',
'provider' => 'users',
],生成token
$data = \request()->all();
$res = auth()->guard('web')->attempt($data);
if(!$res){
return json_encode(['code' => 500,'msg' => '登录失败','data' => []]);
}
$user = auth()->guard('web')->user();
$token = $user->createToken('api')->accessToken;边栏推荐
- Heartless sword English Chinese bilingual poem 001 Spring outing
- Docker uses redis image
- dapr 思维导图
- What is the future of software testing in 2022? Do you need to understand the code?
- GO语言-值类型和引用类型
- 鼻孔插灯,智商上升,风靡硅谷,3万就成
- PostgreSQL create database
- How to optimize the performance of compose? Find the answer through "underlying principles" | developers say · dtalk
- Go language - value types and reference types
- 【愚公系列】2022年06月 .NET架构班 077-分布式中间件 ScheduleMaster加载程序集定时任务
猜你喜欢

Maui introductory tutorial series (1. framework introduction)

Take you in-depth understanding of AGC cloud database

Nat Commun|語言模型可以學習複雜的分子分布

使用Cloud DB构建APP 快速入门-快应用篇

书籍《阅读的方法》读后感

openGauss AI能力升级,打造全新的AI-Native数据库

Aaai2022 latest "time series data processing" report, 127 pages of PPT describing time series data processing and medical application progress

Classmate, have you heard of mot?

Nielseniq announces appointment of Tracey Massey as chief operating officer

前沿科技探究之AI工具:Anomaly-detection
随机推荐
[daily question series]: how to test web forms?
Detailed explanation of opengauss multi thread architecture startup process
Verification code is the natural enemy of automation? Ali developed a solution
从屡遭拒稿到90后助理教授,罗格斯大学王灏:好奇心驱使我不断探索
Db4ai: database driven AI
openGauss并行解码浅谈
GO语言-Slice切片
YEF 2022昨日开幕,多网络平台全程免费直播,开启在线技术盛宴!
WGet command use
Code farming essential SQL tuning (Part 2)
[Yugong series] June 2022 Net architecture class 077 distributed middleware schedulemaster loading assembly timing task
CLP information - No. 1 central document on Strengthening Rural Revitalization financial services
Go language - value types and reference types
PyQt5 使QPlainTextEdit控件支持行号显示
TC8:UDP_MessageFormat_01-02
Streaking? Baa!
面试经典题目:怎么做的性能测试?【杭州多测师】【杭州多测师_王sir】
Ai4db: AI slow SQL root cause analysis
Database design recommendations
I have used 20K for 5 years and met all major manufacturers. These high-frequency interview questions have helped you figure out