×ðÁú¿­Ê±

Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel Passport¼ò»¯OAuth2Éí·ÝÑéÖ¤ £¿

Ëæ×ÅwebÓ¦ÓóÌÐòµÄÆÕ¼° £¬oauth2ÒѾ­³ÉΪ×îÊ¢ÐеÄÉí·ÝÑé֤ЭÒéÖ®Ò»¡£oauth2ͨ¹ýÊÚȨÁîÅÆÀ´»á¼ûÊܱ£»¤µÄ×ÊÔ´ £¬ÎªÓû§ÌṩÇå¾²ºÍ¿ìËٵĻá¼û¡£

LaravelÊÇÒ»¸öÊ¢ÐеÄPHP WebÓ¦ÓóÌÐò¿ò¼Ü £¬ÒѾ­³ÉΪWeb¿ª·¢ÕßµÄÊ×Ñ¡¡£Ëü¾ßÓÐÇ¿Ê¢µÄ¹¦Ð§ºÍ¼òÆÓµÄÓï·¨ £¬¿ÉÒÔ×ÊÖúWeb¿ª·¢Ö°Ô±¿ìËÙ¹¹½¨¸ßÖÊÁ¿µÄWebÓ¦ÓóÌÐò¡£Laravel PassportÊÇLaravelÖÐÒ»ÖÖºÜÊÇÇ¿Ê¢µÄOAuth2ʵÏÖ £¬Ëü¿ÉÒÔ×ÊÖú¿ª·¢Õß¼ò»¯OAuth2Éí·ÝÑéÖ¤¡£ÔÚ±¾ÎÄÖÐ £¬ÎÒÃǽ«Ì½ÌÖÔõÑùʹÓÃLaravel PassportÀ´ÊµÏÖOAuth2Éí·ÝÑéÖ¤¡£

Laravel PassportÏÈÈÝ

Laravel PassportÊÇLaravelÖеÄÒ»¸öOAuth2ЧÀÍÆ÷ʵÏÖ £¬Ëü¿ÉÒÔ×ÊÖú¿ª·¢Õß¿ìËÙ¹¹½¨OAuth2ЧÀÍÆ÷ £¬¼ò»¯OAuth2Éí·ÝÑéÖ¤µÄÀú³Ì¡£Ëü²»µ«ÌṩOAuth2Ô­Éú±ê×¼µÄʵÏÖ £¬»¹ÌṩÁËAPIÃÜÔ¿¡¢¿Í»§¶Ëƾ֤ºÍ»á¼ûÁîÅƵÈЧÀÍ¡£Laravel Passport»¹ÌṩÁËһϵÁеÄAPIÀ´ÖÎÀí¿Í»§¶Ë¡¢ÃÜÔ¿ºÍÁîÅÆ £¬²¢ÇÒ¿ÉÒÔÓëLaravelµÄÓû§ÈÏ֤ϵͳÍêÉƼ¯³É¡£

ʹÓÃLaravel Passport

Laravel PassportµÄ×°ÖÃÊǺÜÊǼòÆ £¬¿ÉÒÔʹÓÃComposerʵÏÖ¡£ÔÚÖÕ¶ËÖнøÈëÄúµÄLaravelÓ¦ÓóÌÐòĿ¼²¢ÊäÈëÒÔÏÂÏÂÁ

composer require laravel/passport

µÇ¼ºó¸´ÖÆ

½ÓÏÂÀ´ £¬ÎÒÃÇÐèÒªÔËÐÐLaravel PassportµÄ×°ÖÃÏÂÁî¡£ÎÒÃÇ¿ÉÒÔʹÓÃArtisanÏÂÁîpassport:install¾ÙÐÐ×°Ö᣸ÃÏÂÁÌìÉúÐëÒªµÄÊý¾Ý¿âǨá㡢һЩ¼ÓÃÜÃÜÔ¿ºÍÓ¦ÓóÌÐò»á¼ûÁîÅÆ¡£ÎÒÃÇ¿ÉÒÔÖ±½ÓÔËÐÐÒÔÏÂÏÂÁ

php artisan passport:install

µÇ¼ºó¸´ÖÆ

ÔÚÔËÐиÃÏÂÁîºó £¬ÄúÓ¦¸Ã¿´µ½ÀàËÆÒÔϵÄÊä³ö£º

Encryption keys generated successfully.
Client ID: 1
Client secret: 9CrrluKEDJ3xvTLyHoSz3g3b5W3H4EBvLIS1G3V5

µÇ¼ºó¸´ÖÆ

ÒÔÉÏÊä³öÖеĿͻ§¶ËIDºÍ¿Í»§¶ËÃØÔ¿ÊÇÄúÔÚÓ¦ÓóÌÐòÖÐ×¢²áOAuth2¿Í»§¶ËËùÐèµÄÐÅÏ¢¡ £¿Í»§¶Ëƾ֤ʹ¿Í»§¶ËÄܹ»»ñÈ¡Óû§ÁîÅƲ¢»á¼ûÄúµÄAPIЧÀÍÆ÷¡£

ÏÖÔÚ £¬ÎÒÃÇÒѾ­×°ÖÃÁËLaravel Passport £¬½ÓÏÂÀ´ÈÃÎÒÃÇÀ´¿´¿´ÔõÑùʹÓÃËüÀ´ÊµÏÖOAuth2ÈÏÖ¤¡£

ÌìÉúAPIÁîÅÆ

Laravel PassportÌṩÁËÒ»¸öAPIÁîÅÆÓÃÓÚÿ¸öÓû§ £¬¸ÃÁîÅÆ¿ÉÓÃÓÚ»á¼ûÊܱ£»¤µÄAPI¶Ëµã¡£ÒªÌìÉúAPIÁîÅÆ £¬ÇëʹÓÃÒÔÏ´úÂ룺

$user = AppModelsUser::find(1);

$token = $user->createToken('MyApp')->accessToken;

µÇ¼ºó¸´ÖÆ

ÔÚÕâ¸öÀý×ÓÖÐ £¬ÎÒÃÇʹÓÃcreateTokenÒªÁìÀ´ÌìÉúAPIÁîÅÆ¡£ÎÒÃÇÐèÒªÌṩһ¸ö±êʶ×Ö·û´® £¬ÒÔ±ãÔÚδÀ´Äܹ»Ê¶±ðÁîÅÆ¡£´ËÒªÁ콫·µ»ØÒ»¸öAccessTokenʵÀý £¬ÎÒÃÇ¿ÉÒÔʹÓÃaccessTokenÊôÐÔÀ´»ñÈ¡ÁîÅÆ×Ö·û´®¡£

ҪʹÓÃAPIÁîÅÆÀ´»á¼ûÊܱ£»¤µÄAPI¶Ëµã £¬ÎÒÃÇÐèÒª½«ÁîÅƸ½¼Óµ½HTTPÇëÇóµÄÍ·ÖС£Ê¹ÓÃÒÔÏ´úÂ룺

$request->header('Authorization', 'Bearer '.$accessToken);

µÇ¼ºó¸´ÖÆ

ÔÚÕâ¸öÀý×ÓÖÐ £¬ÎÒÃÇʹÓÃÇëÇóÍ·AuthorizationÀ´ÌṩAPIÁîÅÆ £¬ÆäÖÐÁîÅÆ×Ö·û¹´Í¨³£ÒÔǰ׺¡°Bearer¡±¿ªÍ·¡£

ÔÚÇëÇóÍ·ÖÐÌí¼ÓAPIÁîÅÆºó £¬Äú¿ÉÒÔ»á¼ûÊܱ£»¤µÄAPI¶Ëµã £¬²¢ÇÒЧÀÍÆ÷½«Ê¹ÓÃAPIÁîÅÆÀ´¾ÙÐÐÉí·ÝÑéÖ¤¡£

¿Í»§¶Ëƾ֤

OAuth2¿Í»§¶Ëƾ֤ÔÊÐí¿Í»§¶Ë´ú±íÓû§´ÓOAuth2ЧÀÍÆ÷»ñÈ¡»á¼ûÁîÅÆ¡£ÔÚLaravel PassportÖÐ £¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´×¢²áOAuth2¿Í»§¶Ë£º

use LaravelPassportClient;

$client = new Client;

$client->name = 'MyApp';
$client->redirect = 'http://example.com/callback';

$client->save();

$client->createToken()->accessToken;

µÇ¼ºó¸´ÖÆ

ÎÒÃÇ¿ÉÒÔʹÓÃÉÏÊöÒªÁìÀ´½¨ÉèOAuth2¿Í»§¶Ë £¬²¢½«¿Í»§¶ËIDºÍ¿Í»§¶ËÃØÔ¿ÓÃÓÚ»ñÈ¡»á¼ûÁîÅÆ¡£

OAuth2ÊÚȨÂë

OAuth2ÊÚȨÂëÊÇÒ»ÖÖÁ÷³Ì £¬ÔÊÐíÓû§Í¨¹ý¿Í»§¶Ë´ú±íÓû§ÇëÇóOAuth2ЧÀÍÆ÷»ñÈ¡»á¼ûÁîÅÆ¡£Laravel PassportÖ§³ÖOAuth2ÊÚȨÂëÁ÷³Ì £¬»¹ÌṩÁËÓÃÓÚÖÎÀíÊÚȨÂëµÄAPI¡£ÀýÈç £¬ÒÔÏÂÊÇÔõÑù½¨ÉèOAuth2ÊÚȨÂëµÄʾÀý£º

use LaravelPassportPassport;
use IlluminateHttpRequest;

Route::get('/redirect', function (Request $request) {
    $query = http_build_query([
        'client_id' => 'client-id',
        'redirect_uri' => 'http://example.com/callback',
        'response_type' => 'code',
        'scope' => '',
    ]);

    return redirect('http://example.com/oauth/authorize?'.$query);
});

Route::get('/callback', function (Request $request) {
    $token = Request::create(
        'http://example.com/oauth/token',
        'POST',
        [
            'grant_type' => 'authorization_code',
            'client_id' => 'client-id',
            'client_secret' => 'client-secret',
            'redirect_uri' => 'http://example.com/callback',
            'code' => $request->code,
        ]
    );

    $response = Route::dispatch($token);

    return json_decode((string) $response->getContent(), true);
})->name('callback');

µÇ¼ºó¸´ÖÆ

ÔÚÉÏÃæµÄ´úÂëÖÐ £¬ÎÒÃÇÊ×ÏÈʹÓÃhttp_build_queryº¯ÊýÌìÉúҪת´ï¸øOAuth2ЧÀÍÆ÷µÄÅÌÎÊ×Ö·û´®¡£È»ºóÎÒÃÇÖض¨Ïòµ½OAuth2ЧÀÍÆ÷ £¬²¢×ª´ïÅÌÎÊ×Ö·û´®×÷Ϊ²ÎÊý¡£µ±Óû§ÊÚȨºó £¬OAuth2ЧÀÍÆ÷½«Öض¨Ïò»Ø×ðÁú¿­Ê±Ó¦ÓóÌÐò¡£ÔÚ×ðÁú¿­Ê±»Øµ÷·ÓÉÖÐ £¬ÎÒÃÇÌìÉúÒ»¸ö»á¼ûÁîÅÆ £¬ÒÔ±ãÔÚδÀ´ÓÃÓÚ»á¼ûÊܱ£»¤µÄAPI¶Ëµã¡£

½áÂÛ

ÔÚ±¾ÆªÎÄÕÂÖÐ £¬ÎÒÃÇѧϰÁËÔõÑùʹÓÃLaravel PassportÀ´¼ò»¯OAuth2Éí·ÝÑéÖ¤µÄÀú³Ì¡£ÎÒÃÇÏàʶÁËÔõÑùÌìÉúAPIÁîÅÆ¡¢ÉèÖÿͻ§¶Ëƾ֤ºÍʵÏÖOAuth2ÊÚȨÂëÁ÷³Ì¡£Laravel PassportÌṩÁËÒ»¸öÈÝÒ×Ã÷È·µÄAPI £¬Ê¹ÎÒÃÇÄܹ»¿ìËÙʵÏÖÇå¾²µÄOAuth2Éí·ÝÑéÖ¤¡£ÈôÊÇÄúÏëÔÚLaravelÓ¦ÓóÌÐòÖоÙÐÐOAuth2Éí·ÝÑéÖ¤ £¬ÄÇôLaravel Passport½«ÊÇÄúµÄÊ×Ñ¡½â¾ö¼Æ»®¡£

ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel Passport¼ò»¯OAuth2Éí·ÝÑéÖ¤ £¿µÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

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

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

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿