当前位置:网站首页>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'=>[]]); } }
边栏推荐
- The statement that allows full table scanning does not seem to take effect set odps sql. allow. fullscan=true; I
- 同一个作业有两个source,同一链接不同数据库账号,为何第二个链接查出来的数据库列表是第一个账号的
- Is "applet container technology" a gimmick or a new outlet?
- 企業不想換掉用了十年的老系統
- Motion capture for snake motion analysis and snake robot development
- Bipartite graph determination
- 吴恩达2022机器学习课程评测来了!
- asp读取oracle数据库问题
- 浅谈现在的弊端与未来的发展
- Redis persistence mechanism
猜你喜欢
Stop saying that microservices can solve all problems
Method of canceling automatic watermarking of uploaded pictures by CSDN
让我们,从头到尾,通透网络I/O模型
Koa2 addition, deletion, modification and query of JSON array
The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
Word2vec (skip gram and cbow) - pytorch
Isomorphism + cross end, knowing applet +kbone+finclip is enough!
C three ways to realize socket data reception
Motion capture for snake motion analysis and snake robot development
JS addition, deletion, modification and query of JSON array
随机推荐
DR-Net: dual-rotation network with feature map enhancement for medical image segmentation
Is the more additives in food, the less safe it is?
(DART) usage supplement
How to choose indoor LED display? These five considerations must be taken into account
[compilation principle] LR (0) analyzer half done
不要再说微服务可以解决一切问题了
实现多彩线条摆出心形
js對JSON數組的增删改查
UE4 blueprint learning chapter (IV) -- process control forloop and whileloop
Today's sleep quality record 78 points
监控界的最强王者,没有之一!
Hard core observation 545 50 years ago, Apollo 15 made a feather landing experiment on the moon
#DAYU200体验官# 在DAYU200运行基于ArkUI-eTS的智能晾晒系统页面
European Bioinformatics Institute 2021 highlights report released: nearly 1million proteins have been predicted by alphafold
面试题:AOF重写机制,redis面试必问!!!
[launched in the whole network] redis series 3: high availability of master-slave architecture
What can be done for traffic safety?
Some suggestions for foreign lead2022 in the second half of the year
Isomorphism + cross end, knowing applet +kbone+finclip is enough!
Cloud native (32) | kubernetes introduction to platform storage system