詳解laravel安裝使用Passport(Api認(rèn)證)
Laravel通過(guò)傳統(tǒng)的登錄表單已經(jīng)讓用戶(hù)認(rèn)證變得很簡(jiǎn)單,但是API怎么辦?API通常使用token進(jìn)行認(rèn)證并且在請(qǐng)求之間不維護(hù)session狀態(tài)。Laravel使用Laravel Passport讓API認(rèn)證變得輕而易舉,Passport基于Alex Bilbie維護(hù)的League OAuth2 server,可以在數(shù)分鐘內(nèi)為L(zhǎng)aravel應(yīng)用提供完整的OAuth2服務(wù)器實(shí)現(xiàn)。
中文文檔
http://laravelacademy.org/post/6813.html
安裝
composer require laravel/passport
接下來(lái),在配置文件 config/app.php 的providers 數(shù)組中注冊(cè) Passport 服務(wù)提供者:
Laravel\Passport\PassportServiceProvider::class,
Passport 遷移將會(huì)為應(yīng)用生成用于存放客戶(hù)端和訪問(wèn)令牌的數(shù)據(jù)表 (遷移文件位置/vendor/laravel/passport/database)
php artisan migrate
創(chuàng)建生成安全訪問(wèn)令牌時(shí)用到的加密密鑰及私人訪問(wèn)和密碼訪問(wèn)客戶(hù)端。
php artisan passport:install
Trait 添加到 App\User 模型中,這個(gè) Trait 會(huì)給這個(gè)模型提供一些輔助函數(shù),用于檢查已認(rèn)證用戶(hù)的令牌和使用作用于。
Laravel\Passport\HasApiTokens

然后在 AuthServiceProvider 的 boot 方法中添加 Passport::roues();

最后,修改文件 config/auth.php 中 api 部分的授權(quán)保護(hù)項(xiàng)( driver )改為 passport 。此調(diào)整會(huì)讓你的應(yīng)用程序在接收到 API 的授權(quán)請(qǐng)求時(shí)使用 Passport 的 TokenGuard 來(lái)處理
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
增加測(cè)試路由
Route::group(['namespace' => 'api'], function () {
Route::post('/login', 'UserController@login');
});
Route::group(['middleware' => 'auth:api', 'namespace' => 'api'], function() {
Route::get('V1/test/passport', 'UserController@passport');
});
增加控制器
php artisen make:controller UserController
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
class UserController extends Controller
{
public function __construct()
{
$this->content = array();
}
public function login()
{
// dd(request('name'));
if(Auth::attempt(['name' => request('name'), 'password' => request('password')]))
{
$user = Auth::user();
$this->content['token'] = $user->createToken('Pi App')->accessToken;
$status = 200;
} else {
$this->content['error'] = "未授權(quán)";
$status = 401;
}
return response()->json($this->content, $status);
}
public function passport()
{
return response()->json(['user' => Auth::user()]);
}
}
通過(guò)postman進(jìn)行簡(jiǎn)單測(cè)試獲取token


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于PHP實(shí)現(xiàn)短信驗(yàn)證碼接口(容聯(lián)運(yùn)通訊)
本文分步驟給大家講解了短信驗(yàn)證碼接口(容聯(lián)運(yùn)通訊)實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-09-09
Zend Framework生成驗(yàn)證碼并實(shí)現(xiàn)驗(yàn)證碼驗(yàn)證功能(附demo源碼下載)
這篇文章主要介紹了Zend Framework生成驗(yàn)證碼并實(shí)現(xiàn)驗(yàn)證碼驗(yàn)證功能,較為詳細(xì)的分析講述了Zend Framework實(shí)現(xiàn)操作驗(yàn)證碼操作的具體步驟與相關(guān)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-03-03
用PHP提取中英文詞語(yǔ)以及數(shù)字的首字母的方法介紹
本篇我在小編為大家介紹,用PHP提取中英文詞語(yǔ)以及數(shù)字的首字母的方法。需要的朋友參考下2013-04-04
Laravel 5框架學(xué)習(xí)之Eloquent 關(guān)系
Eloquent是Laravel的原始ActiveRecord是實(shí)現(xiàn)的,建立在Laravel的Fluent Query Builder之上的,所以Eloquent類(lèi)和Fluent類(lèi)是一樣的,能實(shí)現(xiàn)復(fù)雜的SQL語(yǔ)句和非常直觀表達(dá)出的表與表之間的關(guān)系2015-04-04
laravel實(shí)現(xiàn)前后臺(tái)路由分離的方法
今天小編就為大家分享一篇laravel實(shí)現(xiàn)前后臺(tái)路由分離的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
php教程之魔術(shù)方法的使用示例(php魔術(shù)函數(shù))
這篇文章主要介紹了php的魔術(shù)方法的使用示例(php魔術(shù)函數(shù)),需要的朋友可以參考下2014-02-02
ThinkPHP行為擴(kuò)展Behavior應(yīng)用實(shí)例詳解
這篇文章主要介紹了ThinkPHP行為擴(kuò)展Behavior應(yīng)用實(shí)例,對(duì)于讀者深入了解ThinkPHP框架程序設(shè)計(jì)大有幫助,需要的朋友可以參考下2014-07-07

