Ç°ÆÚ×¼±¸
Ç°ÑÔ£¬ÎªÊ²Ã´×oÒÔ¼°Òª×o¸oɶ
±¾ÈËÐÕСÃu°×£¬²»ÕÛ²»¿Û±a³Ì½iС°×Ò»Ãu£¬µ«ÊÇ×Ô´Ó´oÒ»ÄÇ»a¶u½Ó´¥µ½±a³ÌÕa¼þÆaeÃiµÄÊÂÇe£¬¾ÍÍeÍeÈ«È«µÄÏÝÈeµÄ³ÌÐoµÄÊÀ½ç¡£
Õa²»£¬×i½uÓÖ¿ªÊ¼ÕÛÌÚAPPÁË£¬»°ËµÏÖÔÚ¿ª¢Ò»¿iAPPÕaeÊÇÈÝÒ×£¬Ö»ÓÃJavaScriptºÍÒ»µaµaHTML+css¼¼Êo¾Í¿ÉÒÔÍe³É¡£µ«ÊÇ×oAPPµÄºǫ¾Í²»Ò»ÑuÁË¡£¿ª¢ÁËAPP£¬ÏeÈöÁµaÊý¾Ý½øÈ¥£¬ÄÇÎÒÃǾÍҪȥ¿ª*¢¸oºǫÁË¡£
laravel¿o¼Ü£¬ÊÇÎÒ×iϲ»¶µÄPHP¿o¼ÜÁË£¬Ã»ÓÐÖ®Ò»¡£È¥Äe¾ÍÔø¾ÓÃlaravelдÁËÎҵĸoÈËÍøÕ¾µ«´Ö²Ú³Ì¶ÈÈÃÎÒÊ®*ÖÁ³ºi£¬ºÃÁ˲»³¶ÁË£¬ÈÃÎÒÃÇÖ±½Ó½øÈeÖ÷Ìa¡ª¡ªÏÈ°²×°laravel°É£¡
*»u´¡»¾³ÅaÖÃ**
¾ßÌaµÄ²½ÖeÖ±½Ó¿´Îĵµ°Élaravel5.2°²×°
ÎÒ×Ô¼ºµÄ»¾³ÊÇwin10ÉÏÃae°²×°ÁËwampsrver2.5£¬µ«ÊÇÕaÀiÖµµÃºÃºÃ×¢Òaһϣ¬ÓÃwampsrver2.5ÁË»°£¬Õa¼¸¸oµØ½Òª¸Ä¶¯Ò»Ï¡£¹ØÓÚÕa¸oÇe¿´Îҵıʼǵa»÷Ô¤ÀÀ
¹¤¾ß£ºsublime
a¯ÀÀÆ÷£ºchrome£¨ÒªÓõ½µÄ²a¼þpostman£(C)
¹ØÓÚAPI
API£¨Application Programming Interface,Ó¦ÓóÌÐo±a³Ì½Ó¿Ú£(C)ÊÇÒ»Ð(C)Ô¤Ïȶ¨ÒaµÄº¯Êý£¬Ä¿µÄÊÇÌa¹(C)Ó¦ÓóÌÐoÓe¿ª¢ÈËÔ±»uÓÚijÈi¼þ»oÓ²¼þµÃÒÔÃÎÊÒ»×eÀý³ÌµÄÄÜÁ¦£¬¶øÓÖÎÞÐeÃÎÊÔ´Âe£¬»oÀi½aÄÚ²¿¹¤×÷»uÖƵÄϸ½Ú¡£
ÐeҪעÒaµÄÊÇ£ºAPIÓÐËuµÄ¾ßÌaÓÃ;£¬ÎÒÃÇÓ¦¸ÃÇa³þËuÊǸÉɶµÄ¡£ÃÎÊAPIµÄʱºoÓ¦¸ÃÊaÈeʲô¡£*ÃÎʹýAPI¹ýºoÓ¦¸ÃµÃµ½Ê²Ã´¡£
ÔÚ¿ªÊ¼Ée¼ÆAPIʱ£¬ÎÒÃÇÓ¦¸Ã×¢ÒaÕa8µa
ÕaÀiµÄÄÚÈÝÕª³×Ô´oÉñµÄ²(C)¿Í
ºoÐøµÄ¿ª*¢¼Æ»®¾ÍΧÈÆ×ÅÕa¸o½øÐÐÁË¡££¨ÕaeÐĺðoµÄ×ܽa£(C)
1.RestfulÉe¼ÆÔÔo
2.APIµÄÃuÃu
3.APIµÄ°²È«ÐÔ
4.APIµ»ØÊý¾Ý
5.ͼƬµÄ´¦Ài
6.µ»ØµÄÌaʾÐÅÏ¢
7.ÔÚÏßAPI²aÊÔÎĵµ
8.ÔÚappÆo¶¯Ê±£¬µ÷ÓÃÒ»¸o³oʼ»¯API»ñÈ¡±ØÒªµÄÐÅÏ¢
*ÓÃlaravel¿ª¢API
**
¾ÍÔÚÎÒÉϳi×ÅÒª²»Òª´ÓÁa¿ªÊ¼Ñ§Ï°µÄʱºo£¬ÕÒµ½ÁËÕa¸o²a¼þdingo/apiÄÇôÏÖÔÚ¾ÍÀ´°²×°°É£¡
Ê×ÏÈÒ»¶¨ÊÇÏÂÔصÄû´i
ÔÚа²×°ºÃµÄlaravelµÄcomposer.json¼ÓÈeÈçÏÂÄÚÈÝ
È»ºo´o¿ªcmdÖ´ÐÐ
composer update
ÔÚconfig/app.phpÖеÄprovidersÀiÌi¼Ó
App\Providers\OAuthServiceProvider::class,
Dingo\Api\Provider\LaravelServiceProvider::class,
LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider::class,
LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider::class,
ÔÚaliasesÀiÌi¼Ó
'Authorizer' => LucaDegasperi\OAuth2Server\Facades\Authorizer::class,
ÐÞ¸Äapp/Http/Kernel.phpÎļþÀiµÄÄÚÈÝ
protected $middleware = [\LucaDegasperi\OAuth2Server\Middleware\OAuthExceptionHandlerMiddleware::class,
];
protected $routeMiddleware = [
'oauth' => \LucaDegasperi\OAuth2Server\Middleware\OAuthMiddleware::class,
'oauth-user' => \LucaDegasperi\OAuth2Server\Middleware\OAuthUserOwnerMiddleware::class,
'oauth-client' => \LucaDegasperi\OAuth2Server\Middleware\OAuthClientOwnerMiddleware::class,
'check-authorization-params' => \LucaDegasperi\OAuth2Server\Middleware\CheckAuthCodeRequestMiddleware::class,
'csrf' => \App\Http\Middleware\VerifyCsrfToken::class,
];
È»ºoÖ´ÐÐ
php artisan vendor:publish
php artisan migrate
ÔÚ.envÎļþÀiÌi¼ÓÕaÐ(C)ÅaÖÃ
API_STANDARDS_TREE=x
API_SUBTYPE=rest
API_NAME=REST
API_PREFIX=api
API_VERSION=v1
API_CONDITIONAL_REQUEST=true
API_STRICT=false
API_DEBUG=true
API_DEFAULT_FORMAT=json
ÐÞ¸Äapp\config\oauth2.phpÎļþ
'grant_types' => [
'password' => [
'class' => 'League\OAuth2\Server\Grant\PasswordGrant',
'access_token_ttl' => 604800,
'callback' => '\App\Http\Controllers\Auth\PasswordGrantVerifier@verify',
],
],
н¨Ò»¸o*þÎñÌa¹(C)Õߣ¬ÔÚapp/ProvidersÏÂн¨OAuthServiceProvider.phpÎļþÄÚÈÝÈçÏÂ
namespace App\Providers;
use Dingo\Api\Auth\Auth;
use Dingo\Api\Auth\Provider\OAuth2;
use Illuminate\Support\ServiceProvider;
class OAuthServiceProvider extends ServiceProvider
{
public function boot()
{
$this->app[Auth::class]->extend('oauth', function ($app) {
$provider = new OAuth2($app['oauth2-server.authorizer']->getChecker());
$provider->setUserResolver(function ($id) {
// Logic to return a user by their ID.
});
$provider->setClientResolver(function ($id) {
// Logic to return a client by their ID.
});
return $provider;
});
}
public function register()
{
//
}
}
È»ºo´o¿ªroutes.phpÌi¼ÓÏa¹ØÂ*ÓÉ
//Get access_token
Route::post('oauth/access_token', function() {
return Response::json(Authorizer::issueAccessToken());
});
//Create a test user, you don't need this if you already have.
Route::get('/register',function(){
$user = new App\User();
$user->name="tester";
$user->email="test@test.com";
$user->password = \Illuminate\Support\Facades\Hash::make("password");
$user->save();
});
$api = app('Dingo\Api\Routing\Router');
//Show user info via restful service.
$api->version('v1', ['namespace' => 'App\Http\Controllers'], function ($api) {
$api->get('users', 'UsersController@index');
$api->get('users/{id}', 'UsersController@show');
});
//Just a test with auth check.
$api->version('v1', ['middleware' => 'api.auth'] , function ($api) {
$api->get('time', function () {
return ['now' => microtime(), 'date' => date('Y-M-D',time())];
});
});
*ֱ𴴽¨BaseController.phpºÍUsersController.phpÄÚÈÝÈçÏÂ
//BaseController
namespace App\Http\Controllers;
use Dingo\Api\Routing\Helpers;
use Illuminate\Routing\Controller;
class BaseController extends Controller
{
use Helpers;
}
//UsersController
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
class UsersController extends BaseController
{
public function index()
{
return User::all();
}
public function show($id)
{
$user = User::findOrFail($id);
// Êý×eÐÎʽ
return $this->response->array($user->toArray());
}
}
ËaeºoÔÚapp/Http/Controllers/Auth/Ï´´½¨PasswordGrantVerifier.phpÄÚÈÝÈçÏÂ
namespace App\Http\Controllers\Auth;
use Illuminate\Support\Facades\Auth;
class PasswordGrantVerifier
{
public function verify($username, $password)
{
$credentials = [
'email' => $username,
'password' => $password,
];
if (Auth::once($credentials)) {
return Auth::user()->id;
}
return false;
}
}
´o¿ªÊý¾Ý¿aµÄoauth_client±iÐÂÔoÒ»ÌoclientÊý¾Ý
INSERT INTO 'oauth_clients' ('id', 'secret', 'name', 'created_at', 'updated_at') VALUES ('1', '2', 'Main website', '2016¨C03¨C13 23:00:00', '0000¨C00¨C00 00:00:00');
ËaeºoµÄ¾ÍÊÇÈ¥Óa¿iµÄ²aÊÔÁË£¬ÕaÀiÒª²aÊÔµÄAPIÓÐ
ÐÂÔoÒ»¸oÓû§
¶ÁÈ¡ËuÓÐÓû§ÐÅÏ¢
Ö»*µ»ØÓû§idΪ4µÄÐÅÏ¢
»ñÈ¡access_token
http://localhost/oauth/access_token
ÀuÓÃtokenÖµ»ñµÃʱ¼a£¬tokenÖµÕýȲÅÄܵ»ØÕýÈ*Öµ
´o¿ªPostMan