×ðÁú¿­Ê±¹ÙÍøµÇ¼

Ò»ÎÄÏÈÈÝLaravelÖÐTokenµÄʵÏÖ»úÖÆ

laravelÊÇÒ»¸öʹÓÃphp±à³ÌÓïÑÔ¿ª·¢µÄwebÓ¦Óÿò¼Ü £¬ËüµÄ׿ԽÌåÏÖµÃÒæÓÚÆäÄÚ²¿¼¯³É´ó×Ú¹¦Ð§Ç¿Ê¢µÄÀ©Õ¹°ü¡£ÆäÖоͰüÀ¨ÁËtokenµÄµ×²ãʵÏÖ¡£token £¬¼´ÁîÅÆ £¬ÊÇwebÓ¦ÓóÌÐò³£ÓõÄÒ»ÖÖÉí·ÝÑéÖ¤·½·¨ £¬Í¨³£ÓÃÓÚ± £»¤apiºÍwebЧÀÍÃâÊܲ»·¨»á¼û¡£ÔÚÕâƪÎÄÕÂÖÐ £¬ÎÒÃǽ«ÏÈÈÝlaravelÖÐtokenµÄʵÏÖ»úÖÆ¡£

Ò»¡¢ÁîÅƵĿ´·¨

Token £¬¹ËÃû˼Òå £¬¾ÍÊÇÖ¸Ò»ÖÖÁîÅÆ £¬ÊÇÒ»ÖÖ±ê¼Ç £¬¿ÉÒÔ´ú±íijÖÖÉí·ÝÐÅÏ¢»òÕßÊÚȨÐÅÏ¢¡£Ëüͨ³£ÓÉЧÀÍÆ÷±¬·¢²¢·¢·Å¸ø¿Í»§¶Ë £¬¿Í»§¶ËÊÕµ½ÁîÅƺó½«Æä´æ·ÅÔÚÍâµØ £¬ÔÚºóÐøÇëÇóʱ¼ÓÈëÇëÇóÍ·»òÕßÇëÇó²ÎÊýÖÐ £¬×÷ΪÉí·ÝÑéÖ¤»òÊÚȨµÄ±êʶ¡£Ð§ÀÍÆ÷ͨ¹ý¼ì²éÁîÅÆÊÇ·ñÓÐÓà £¬¾Í¿ÉÒÔÈ·¶¨ÇëÇóÊÇ·ñ¾ßÓÐÉí·ÝÑéÖ¤»òÕßÊÚȨÐÅÏ¢¡£

ÁîÅƵÄʹÓÃÄܹ»¸üÓÐÓõر £»¤WebÓ¦ÓÃÃâÊÜδ¾­ÊÚȨµÄ»á¼û £¬ÓÈÆäÊÇÔÚAPIºÍWebЧÀÍÖÐ £¬ÁîÅÆÊDZز»¿ÉÉٵġ£

¶þ¡¢ Laravel TokenµÄʵÏÖ

Laravel×÷Ϊһ¿îÓÅÒìµÄWebÓ¦Óÿò¼Ü £¬ÔÚÆäÄÚÖõÄAuth¹¦Ð§ÖÐÌṩÁËTokenµÄÖ§³Ö¡£ÔÚLaravelÖÐ £¬TokenµÄʵÏÖʹÓÃÁËLaravel SanctumÕâ¸öÀ©Õ¹°ü¡£

2.1 Laravel Sanctum

Laravel SanctumÊÇÒ»¿îÇáÁ¿¼¶µÄÉí·ÝÑéÖ¤°ü £¬¿ÉÒÔΪLaravelÓ¦ÓóÌÐòÌṩAPIÉí·ÝÑéÖ¤ £¬»ùÓÚAPIÃØÔ¿»òÕßToken £¬ÈÃÓ¦ÓóÌÐò¸üºÃµÄÔËÐÐÔÚÎÞ״̬ÇéÐÎÖÐ £¬ÈçSPAÓ¦ÓóÌÐò¡¢µ¥Ò³Ó¦ÓóÌÐòºÍÒƶ¯Ó¦ÓóÌÐòµÈ¡£Laravel SanctumÌṩÁËÒÔϹ¦Ð§£º

ÎÞÐèÉèÖü´¿É×îÏÈʹÓà £»

Ö§³ÖʹÓÃsession¡¢TokenÒÔ¼°APIÃØÔ¿µÈ¶àÖÖÉí·ÝÑéÖ¤·½·¨ £»

ÄÚÖÃÁ˶àÖÖÉí·ÝÑéÖ¤µÄʵÏÖ £¬Èçcookie¡¢Token¡¢authµÈ £»

ÌṩÁËÀû±ãµÄÉí·ÝÑéÖ¤ºÍTokenÌìÉú £»

¸üºÃµÄ×Ô½ç˵Éí·ÝÑéÖ¤Á÷³Ì¡£

2.2 TokenµÄʵÏÖÔ­Àí

ÔÚLaravel SanctumÖÐ £¬TokenµÄʵÏÖÔ­ÀíÓësessionʵÏÖÔ­ÀíÀàËÆ¡£ÔÚÇëÇóÖÐ £¬¿Í»§¶ËÏòЧÀÍÆ÷·¢³öÇëÇóʱ £¬½«Token×÷ΪÇëÇó²ÎÊý»òHeaderÖеÄAuthorization×ֶη¢Ë͸øЧÀÍÆ÷¡£Ð§ÀÍÆ÷ÔÚ¼ì²éTokenÊÇ·ñÓÐÓà £¬²¢ÔÚÓÐÓÃÆÚÄÚ £¬¸øÓèÇëÇó²Ù×÷µÄȨÏÞ»òÕß·µ»Ø¹ýʧÐÅÏ¢¡£TokenµÄʵÏÖÁ÷³ÌÈçÏ£º

½¨ÉèToken£ºµ±Óû§µÇ¼ʱ £¬Sanctum½«Îª¸ÃÓû§ÌìÉúÒ»¸öËæ»úµÄToken £¬²¢½«¸ÃTokenÉúÑÄÔÚºǫ́Êý¾Ý¿âÖÐ £»

·¢ËÍToken£º½«Token×÷ΪÇëÇó²ÎÊý»òÕßHeaderÖеÄAuthorization×ֶη¢Ë͸øЧÀÍÆ÷ £»

TokenÑéÖ¤£ºÔÚЧÀÍÆ÷¶Ë £¬Sanctum½«¼ì²éÎüÊÕµ½µÄTokenÊÇ·ñÓÐÓà £¬²¢¾öÒéÊÚȨ»ò¾Ü¾ø £»

TokensÖÎÀí£ºSanctumÌṩÁËһϵÁÐAPIÀ´½¨Éè¡¢×÷·Ï¡¢²éÕÒºÍÑéÖ¤Token¡£

Èý¡¢ Laravel TokenµÄʹÓÃ

SanctumÌṩÁËÀû±ãÒ×ÓõÄAPIÀ´Ê¹ÓÃToken £¬°üÀ¨TokenµÄ½¨Éè¡¢×÷·Ï¡¢²éÕÒºÍÑéÖ¤µÈ¡£ÏÂÃæÊÇTokenµÄʹÓ÷½·¨£º

3.1 ×°ÖÃ Sanctum

ÔÚÓ¦ÓóÌÐòÖÐ £¬Ê×ÏÈÐèÒª½«SanctumµÄÒÀÀµ°üÒýÈëµ½Ó¦ÓõÄcomposer.jsonÎļþ£º

composer require laravel/sanctum

µÇ¼ºó¸´ÖÆ

×°ÖÃÍê³Éºó £¬ÐèÒªÔÚconfig/app.phpÎļþÖÐÌí¼ÓÈçÏÂÉèÖãº

'providers' => [ 
    // Other service providers... 
    Laravel\Sanctum\SanctumServiceProvider::class, 
],

µÇ¼ºó¸´ÖÆ

3.2 Ðû²¼ÉèÖÃ

ÔÚ×°ÖÃÍê³Éºó £¬ÐèÒªÔËÐÐÒÔÏÂÏÂÁîÀ´Ðû²¼SanctumµÄÉèÖÃÎļþ£º

php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"

µÇ¼ºó¸´ÖÆ

3.3 ÉèÖÃMiddleware

ÔÚʹÓÃSanctumʱ £¬ÐèÒªÔÚ¶ÔÓ¦µÄ·ÓÉÖÐÌí¼ÓÖÐÑë¼þ¡£ÔÚLaravelÖÐ £¬ÒѾ­ÄÚÖÃÁËAPIÈÏÖ¤µÄÖÐÑë¼þ £¬¿ÉÒÔÖ±½ÓŲÓá£

3.4 ½¨ÉèToken

ÔڵǼºó £¬¿ÉÒÔʹÓÃÈçÏ´úÂëÀ´ÎªÄ¿½ñÓû§½¨ÉèToken£º

use Illuminate\Http\Request; 
use Illuminate\Support\Facades\Hash; 
use Illuminate\Validation\ValidationException; 
use App\Models\User; 
use Illuminate\Support\Facades\Auth; 
use Illuminate\Support\Facades\Route; 

// ½¨ÉèToken 
Route::post('/api/token/create', function (Request $request) { 
    $request->validate([ 
        'email' => 'required|email', 
        'password' => 'required', 
    ]); 

    $user = User::where('email', $request->email)->first(); 

    if (! $user || ! Hash::check($request->password, $user->password)) { 
        throw ValidationException::withMessages([ 
            'email' => ['The provided credentials are incorrect.'], 
        ]); 
    } 

    return $user->createToken($request->header('User-Agent'))->plainTextToken; 
});

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄ´úÂëÖÐ £¬¿ÉÒÔ¿´µ½ÔÚ½¨ÉèTokenʱ £¬Ê¹ÓûúеµÄUser-Agent×÷ΪÌØÁíÍâ²ÎÊý¡£ÕâÀïµÄUser-AgentÊÇÒ»¸öHTTPÍ· £¬¼Í¼ÁËä¯ÀÀÆ÷»òÓ¦ÓóÌÐòµÄÏà¹ØÐÅÏ¢¡£Õâ¸öÐÅÏ¢½«×÷ΪTokenµÄÒ»²¿·Ö £¬Ê¹µÃTokenÒ»µ©±»µÁÓûòÕ߶ñÒâʹÓà £¬¿ÉÒÔºÜÈÝÒ׵ط¢Ã÷ºÍ×÷·Ï¡£

3.5 ×÷·ÏToken

½¨ÉèµÄTokenÒ»µ©±»µÁÓûòÕßʧЧ £¬¿ÉÒÔʹÓÃÈçÏ´úÂë×÷·Ï£º

Auth::user()->tokens()->delete();

µÇ¼ºó¸´ÖÆ

3.6 ÑéÖ¤À©Õ¹

Sanctum»¹ÌṩÁËÓÅÒìµÄÑéÖ¤À©Õ¹ £¬¿ÉÒÔÀû±ãµØ¾ÙÐлá¼û¿ØÖÆ £¬´úÂëÈçÏ£º

use Illuminate\Http\Request; 
use Illuminate\Support\Facades\Hash; 
use Illuminate\Validation\ValidationException; 
use App\Models\User; 
use Illuminate\Support\Facades\Auth; 
use Illuminate\Support\Facades\Route; 
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable 
{
    use HasApiTokens, Notifiable;
}

µÇ¼ºó¸´ÖÆ

ʹÓÃÉÏÊö´úÂëÖ®ºó £¬ÎÒÃǾͿÉÒÔÔÚUserÄ£×ÓÖÐʹÓÃcan½Ó¿ÚÀ´¾ÙÐлá¼û¿ØÖÆ £¬´úÂëÈçÏ£º

$request->user()->can('update', $post);

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄ´úÂëÖÐ £¬can½«Æ¾Ö¤Ä¿½ñÓû§µÄ½ÇÉ«¡¢È¨ÏÞºÍÕ½ÂÔ £¬ÅжÏÓû§ÊÇ·ñÓÐȨ¾ÙÐиüвÙ×÷¡£ÐèҪעÖصÄÊÇ £¬Óû§ÐèҪ׼ȷʵÏÖ×Ô¼ºµÄ»á¼û¿ØÖÆÂß¼­¡£

ËÄ¡¢ ×ܽá

ÔÚ±¾ÎÄÖÐ £¬ÎÒÃÇÏÈÈÝÁËLaravelÖÐTokenµÄµ×²ãʵÏÖ»úÖÆ £¬ÓÈÆäÊÇʹÓÃSanctumÀ©Õ¹°üµÄ·½·¨¡£SanctumÌṩÁËÀû±ãÒ×ÓõÄAPI £¬Äܹ»¿ìËÙ¼¯³Éµ½Ó¦ÓóÌÐòÖÐ £¬²¢Ìá¸ßÓ¦ÓóÌÐòµÄÇå¾²ÐÔ¡£TokenµÄʹÓ÷½·¨¡¢½¨Éè¡¢×÷·ÏºÍÖÎÀí £¬ÒÔ¼°»á¼û¿ØÖƵȷ½Ãæ¾ù¾ÙÐÐÁËÏêϸµÄ½â˵¡£

ÔÚ½ñÌìµÄ»¥ÁªÍøÌìÏÂÖÐ £¬Ëæ×ÅAPIºÍWebЧÀ͵ÄÆÕ±éÓ¦Óà £¬Token×÷ΪһÖÖÉí·ÝÑéÖ¤·½·¨ £¬½«ÔÚÐí¶àÓ¦ÓóÌÐòÖлñµÃÔ½·¢ÆÕ±éµÄÓ¦Óá£Laravel¿ò¼ÜÌṩÁËÓÅÒìµÄTokenʵÏÖ»úÖÆ £¬¿ÉÒÔ¸üºÃµØ± £»¤WebÓ¦ÓÃÃâÊܲ»·¨»á¼û¡£

ÒÔÉϾÍÊÇÒ»ÎÄÏÈÈÝLaravelÖÐTokenµÄʵÏÖ»úÖƵÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ