Laravel ??: Laravel Passport? ???? APIOAuth2 ??? ???? ??? ??????
Jun 15, 2023 am 10:28 AMLaravel? ?? ?? ?? PHP ??????, ???? ?? ???? ???? ?? ???? ????. Laravel? ?? OAuth2 ??? ???? ?? API ???? Laravel Passport? ???? ??? ??? ???? ?? ?? ?? ?? ???(???)? ?????.
OAuth2? ?? ????? ????? ? ? ??? ???????? ?? ???? ?? ?? ?? ????????. OAuth2? ????? ??? ???? ?????? API? ??? ? ??? ?? ??? ???? ???. Laravel Passport? OAuth2 ??? ???? ?? ??? ?????.
? ???? Laravel Passport? ???? OAuth2 ??? ???? ??? ?????. ???? ?? Laravel ?????? ?? ???? ??? ?????.
Laravel Passport ??
Composer? ???? Laravel Passport? ??? ? ????. Laravel ??????? ?? ?????? ?? ??? ?????:
composer require laravel/passport
??? ???? ?? ??? ???? Laravel Passport? ??? ???? ???? ???:
php artisan migrate
???? ?? ??? ???? ??? ?? ????? Laravel Passport? ??:
php artisan passport:install
??? ?? ??? ??? ????? ? ?????.
Passport ??
??? ??? ? ???????? Laravel Passport? ???? ???. ??? ??? ??? ????:
Enable Passport
Laravel Passport? ?????? ?? Passport::route? ???? ???. () ??. ? ??? ??? ??? ?????:
//在bootstrap/app.php文件中加入下面這行代碼 LaravelPassportPassport::routes();
?? ?? ?? ?? ???
????? ??? ??? ? ???? ?????. ??? ??? ? ???? ??? ?? ???? AuthServiceProvider?? Passport::refreshTokensExpireIn() ???? ??? ? ????. AuthServiceProvider? ?? ?? ??? ?????.
//在AppProvidersAuthServiceProvider.php文件中加入下面這段代碼 use LaravelPassportPassport; //其他代碼... public function boot() { $this->registerPolicies(); Passport::routes(); Passport::tokensExpireIn(Carbon::now()->addDays(15)); Passport::refreshTokensExpireIn(Carbon::now()->addDays(30)); }
? ??? tokensExpireIn()
???? ?? ?? ??? 15?? ???? refreshTokensExpireIn()
? ???? ???? ?? ??? ??? ?? ??? 30?? ?????. tokensExpireIn()
方法將令牌的過(guò)期時(shí)間設(shè)置為15天,而refreshTokensExpireIn()
方法將自動(dòng)刷新的令牌的到期時(shí)間設(shè)置為30天。
在User模型中使用Passport
現(xiàn)在需要在User模型中啟用Laravel Passport功能??梢酝ㄟ^(guò)在User類上使用Passport::use()方法來(lái)實(shí)現(xiàn):
//在app/User.php文件中加入下面這段代碼 use LaravelPassportHasApiTokens; class User extends Authenticatable { use HasApiTokens, Notifiable; }
創(chuàng)建OAuth2客戶端
在應(yīng)用程序中使用OAuth2身份驗(yàn)證時(shí),需要通過(guò)授權(quán)服務(wù)器來(lái)生成API令牌。為了與授權(quán)服務(wù)器通信,需要?jiǎng)?chuàng)建客戶端??梢酝ㄟ^(guò)運(yùn)行passport:client
命令來(lái)創(chuàng)建客戶端:
php artisan passport:client --password
運(yùn)行此命令后,會(huì)提示輸入應(yīng)用程序名稱,如果不想輸入名稱,則可以選擇使用默認(rèn)值,回車(chē)即可。
運(yùn)行完成后,將會(huì)生成一個(gè)client_id和client_secret。client_id和client_secret將用于與授權(quán)服務(wù)器進(jìn)行通信。
創(chuàng)建API路由
在創(chuàng)建OAuth2客戶端后,需要?jiǎng)?chuàng)建API路由以使授權(quán)服務(wù)器可以訪問(wèn)API。
//在routes/api.php文件中加入以下代碼 Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user(); });
在這個(gè)例子中,通過(guò)使用auth:api
Authorization: Bearer [access_token]OAuth2 ????? ?? ???????? OAuth2 ??? ??? ? ?? ??? ?? API ??? ???? ???. ?? ??? ????? ?????? ???? ???.
passport:client
??? ???? ?????? ??? ? ????. rrreee
? ??? ??? ? ?????? ??? ????? ???? ?????. ??? ???? ???? ??? ?????. ???? ????? ???? Enter? ?????. ??? ???? client_id? client_secret? ?????. client_id ? client_secret? ?? ??? ???? ? ?????. API ?? ??????OAuth2 ?????? ??? ? API? ?? ??? ???? ? ??? API ??? ???? ???. ??rrreee??? ?????auth:api
????? ???? ??? ??? OAuth2 ??? ?????. ??? API ??? ???? ??? ? ??? 401 Unauthorized HTTP ??? ?????. ??????? ?? HTTP ?? ????????? API? ??? ??? OAuth2 ?? ??? ???? ??? ??? ?????. ????Postman? ?? ??? ???? API? ???? ? ??? ???? ??? API ??? ????? ???. Postman??? ??? Authorization ??? ??? ? ????. ?? ?? ??rrreee??access_token? ??? ??? ???? ??? ???. ??????????Laravel Passport? ?? ??? ???? ??? API ??? ?? ??? ? ?? ??? ???? ?? OAuth2 ?????. ? ????? Laravel Passport? ???? OAuth2 ??? ???? ??? ?? ??? ??? OAuth2 ????? ? API ??? ???? HTTP ??? ?? ???? ??? ???????. ??????? API ??? ???? ?? ?? Laravel Passport? ???? ?? ? ??? ? ????. ??? ??? Laravel ??: Laravel Passport? ???? APIOAuth2 ??? ???? ??? ??????? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

PHP?? ?? ??? ???? ? ?? ?? ??? ????. 1. php.ini? ?? ??? ??; 2. ? ?? (? : Apache? Setenv ?? nginx? FastCGI_Param)? ??????. 3. PHP ?????? putenv () ??? ??????. ? ??? Php.ini? ????? ??? ???? ??? ???? ? ?? ??? ?? ???? ????? ???? Putenv ()? ?? ??? ?????. ?? ???? ?? ?? (? : php.ini ?? ? ?? ??)? ???? ????. ?? ?? ??? ??? ?? ??? ????? ???? ?? ????.

Laravel? ?? ??? ?? ?? ??? ?? ?? ??? ???? ??? ??????. ?? ???? ?? ??? ????? ? ???? I/O ?? ? ?? ?? ??? ???? ???? ??? ?? ? ????. 1. ?? ????? ?? ? ? ???????? ??? ????? ?? ???? ??????. 2. ??? ? ??? ?? ? ? PhPartisAnconfig? ?? ???????. 3. ?? ??? ??? ??? ???? ?? ?? ?? ???? ???? ????. 4. ?? ?? ??? ???? ?? ??? ??? .env ??? ???? ?? ???????.

PHP ????? ?? ??? ??? ? ??? ??? CI (Continuous Integration) ????? ???? ? ????. 1. DockerFile? ???? ?? ???, ?? ??, ??? ?? ? ?? ??? ???? PHP ??? ?????. 2. Gitlabci? ?? CI/CD ??? ???? .gitlab-ci.yml ??? ?? ??, ??? ? ?? ??? ???? ?? ??, ??? ? ??? ?????. 3. PHPUNIT? ?? ??? ??? ??? ???? ?? ?? ? ???? ???? ????????. 4. Kubernetes? ?? ?? ?? ??? ???? ?? .yaml ??? ?? ?? ??? ?????. 5. Dockerfile ??? ? ??? ??? ??????

??? ?? ??? PHP ???? ?? ?? ??? ???? ?? ???????. RBAC (Role-Based Access Control) ??? ?? ???, ?? ? ??? ???? ??? ?? ?? ? ??? ?????. ?? ???? ??? ?????. 1. ???, ?? ? ??? ? ???? user_roles ? role_permissions? 3 ?? ?? ???; 2. $ user-> can ( 'edit_post')? ?? ???? ?? ?? ??? ?????. 3. ??? ???? ??? ??????. 4. ?? ??? ???? ?? ?? ?? ? ??? ? ???? ???? ?? ??? ? ?? ??? ?????. 5. ??? ??? ?? ?? ???? ?? ???? "??"? ??????.

Laravel? eloquentscopes? ?? ??? ??? ??? ?????? ?? ?? ??? ????? ?????. 1. ?? ??? ???? ???? ???? ???? Post :: published (); 2. ??? ??? ?? ??? ???? ???? ?? ??? ?? ?? ?? ??? ???? ???? ??? ?????? ??? ???? ???????. 3. ????? ?? ?? ?? ??? ??? ?? ?? ??? ?? ? ? ??? ?? ? ? ?? ?? ??? ?????. 4. ?? ??? ? ??? ?? ???? ? ??? ? ?? ??, ?? ??, ?? ???? ? ?? ?????????.

CreateAhelpers.phpfileInapp/helperswithCustOmFunctionsikeFormatPrice, isactiveroute, andisAdmin.2.addTheFileTothe "??"sectionOfcomposer.jsonUnderAutoLoad.3.runcomposerDump-AUTOLOADTOMAKETHINGTICTIONSGLOBELYAVAILABLE.4.USETHEHELPERFUNCUNTION

?? ?? ?? : ?? ????? PHP? ?? Error_Log ()? ??? ? ????. ????? ???? ??? ?? ??? ?????? ???? ?? ??? ? ?? ??? ???? ??? ?? ???, ??, ?? ? ?? ? ?? ?? ??? ???? ??? ??????. 2. ??? ?? ?? : ??? ??? ??? ??? ? ??? ?? ??? ??? ?? ??? ??? ??????? ??????. MySQL/PostgreSQL? ???? ??? ? ???? ??????. Elasticsearch Kibana? ? ???/? ???? ?????. ???, ??? ?? ? ??? ? ?? ??? ?? ??????. 3. ?? ? ?? ????? : ??, ???, ?? ? ??? ??? ??????. Kibana? ?? ????? PHP ??? ?? ?? ?????? ???? ???? ?????? ???? ??? ? ?? ??? ??? ? ????.

??, ??, ?? ?? ? ?? ??? ???? ?? ??? ?? ? ?? ???? ?????. 2. ?? ???? ???? ?? ??? ??? SONGSTOMONY ? HASMANY ?? ??; 3. ?? ? ? ?? ? ?? ??? ????? (?? ???? ?? ??? ? ??). 4. ?? ? ?? ??? ???? ?? ??? ???? ?? ? ?? ??? ???? ?? ??? ?????. 5. ?? ???? ??? ?? (?? ??)? ???? ?? ????? ??????. 6. ?? ??? ?? ??? ???? Laravel Signature URL? ???? ??? ??????. 7. ? ?? ?? ? ? ?? ??? ?? ?? ??? ?? ??? ?????. ?????? ??, ?? ?? ??? ??????????.
