当前位置:网站首页>Laravel8 uses passport authentication to log in and generate a token
Laravel8 uses passport authentication to log in and generate a token
2022-07-06 23:24:00 【Elante pace】
install passport
composer require laravel/passport
Execute the migration command
php artisan migrate
Create encryption keys required to generate security access tokens
php artisan passport:install
App\Models\ApiUser lead Trait
<?php
namespace App\Models;
use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as AuthUser;
class ApiUser extends AuthUser
{
use HasApiTokens, Notifiable;
}
App\Providers\AuthServiceProvider
Let go of 'App\Models\Model' => 'App\Policies\ModelPolicy' Notes
boot Next Passport::routes();
<?php
namespace App\Providers;
use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Models\Model' => 'App\Policies\ModelPolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Passport::routes();//token The period of validity of the certification Passport::tokensExpireIn(now()->addHour(2)); // Refresh token The period of validity of the certification Passport::refreshTokensExpireIn(now()->addDays(30)); Passport::personalAccessTokensExpireIn(now()->addMonths(6));}
}
stay config/auth.php Middle configuration
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],'apiweb'=>[
'driver' => 'session',
'provider' => 'apiusers',],
'api' => [
'driver' => 'passport',
'provider' => 'apiusers',
// 'hash' => false,
],
],'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ], 'apiusers' => [ 'driver' => 'eloquent', 'model' => \App\Models\ApiUser::class, ], ]
App\Http\Controllers\Api\LoginController
public function login(Request $request) { try { $this->validate($request,[ 'username'=>'required', 'password'=>'required' ]); } catch (\Exception $e){ return response()->json(['code'=>403,'msg'=>' Account number or password cannot be empty ','data'=>[]]); } // Verify account and password $bool = auth()->guard('apiweb')->attempt($request->except('/api/login')); if($bool) { $user = auth()->guard('apiweb')->user(); if($user->click > env('APINUM')) { return response()->json(['code'=>405,'msg'=>' The number of requests on that day has reached the maximum ','data'=>[]]); } $token = $user->createToken('api')->accessToken; $user->increment('click'); $data = [ 'expire'=>3600*24*30, 'token'=>$token ]; return response()->json(['code'=>400,'msg'=>' Generate token success ','data'=>$data]); }else{ return response()->json(['code'=>403,'msg'=>' Incorrect account or password ','data'=>[]]); } }
边栏推荐
- 吴恩达2022机器学习课程评测来了!
- 企業不想換掉用了十年的老系統
- 实现多彩线条摆出心形
- 请问async i/o可以由udf算子实现然后用sql api调用吗?目前好像只看到Datastre
- docker中mysql开启日志的实现步骤
- Detailed explanation of regular expression (regexp) in MySQL
- Dayu200 experience officer homepage AITO video & Canvas drawing dashboard (ETS)
- Should the jar package of MySQL CDC be placed in different places in the Flink running mode?
- Huawei cloud gaussdb (for redis) unveils issue 21: using Gauss redis to achieve secondary indexing
- 传统企业要为 Web3 和去中心化做的 11 个准备
猜你喜欢
使用MitmProxy离线缓存360度全景网页
Ajout, suppression et modification d'un tableau json par JS
Enterprises do not want to replace the old system that has been used for ten years
The problem that dockermysql cannot be accessed by the host machine is solved
DockerMySQL无法被宿主机访问的问题解决
[launched in the whole network] redis series 3: high availability of master-slave architecture
How to choose indoor LED display? These five considerations must be taken into account
PDF批量拆分、合并、书签提取、书签写入小工具
Station B Big utilise mon monde pour faire un réseau neuronal convolutif, Le Cun Forward! Le foie a explosé pendant 6 mois, et un million de fois.
NFTScan 开发者平台推出 Pro API 商业化服务
随机推荐
Station B Big utilise mon monde pour faire un réseau neuronal convolutif, Le Cun Forward! Le foie a explosé pendant 6 mois, et un million de fois.
[launched in the whole network] redis series 3: high availability of master-slave architecture
Implementation steps of mysql start log in docker
MySQL implementation of field segmentation from one line to multiple lines of example code
Dockermysql modifies the root account password and grants permissions
How can Oracle CDC deserialize with jsondebeziumdeserializationschema
UE4 blueprint learning chapter (IV) -- process control forloop and whileloop
新手问个问题,我现在是单机部署的,提交了一个sql job运行正常,如果我重启了服务job就没了又得
What does security capability mean? What are the protection capabilities of different levels of ISO?
The problem that dockermysql cannot be accessed by the host machine is solved
实现多彩线条摆出心形
Modules that can be used by both the electron main process and the rendering process
室内LED显示屏应该怎么选择?这5点注意事项必须考虑在内
每人每年最高500万经费!选人不选项目,专注基础科研,科学家主导腾讯出资的「新基石」启动申报...
让 Rust 库更优美的几个建议!你学会了吗?
DevSecOps软件研发安全实践——发布篇
docker mysql5.7如何设置不区分大小写
Hard core observation 545 50 years ago, Apollo 15 made a feather landing experiment on the moon
js對JSON數組的增删改查
今日睡眠质量记录78分