我想用laravel创建Web服务
问题描述:
我想用laravel创建Web服务,但我不这样做。我想用laravel创建Web服务
我使用的路线有一个人是这样的:
Route::group(array('prefix' => 'api/v1', 'before' => 'basic.outh'), function(){
Route::resource('url', '[email protected]');
Route::resource('show', '[email protected]');
Route::resource('destroy', '[email protected]');
});
但这路由过滤只想用户名,例如:
Route::filter('auth.basic', function()
{
return Auth::basic("username");
});
我想使我的系统一样笨的RESTful API。这个有可能?
你能告诉我任何例子吗?
答
是的,绝对有可能。
就个人而言,我建议使用OAuth2进行基于令牌的验证,这更适合于API。 OAuth的学习曲线相当陡峭,但幸运的是,有一个Laravel(OAuth2包装器)的包,使其非常容易,因为它会为您生成和验证令牌。
套餐:
https://github.com/lucadegasperi/oauth2-server-laravel
例子:
我有一个类似的设置。下面的代码并不意味着替换通过文档,但这就像你的路线看起来像使用这个包装。
Route::group(['prefix' => 'api/v1', 'before' => 'apiErrors'], function()
{
// Returns a valid token based on grant_type and credentials when a request is made to the accessToken endpoint.
// I use 'client_credentials' and 'refresh_token' for APIs serving mobile apps, for example. You can use that, or roll your own.
Route::post('accessToken', function()
{
return AuthorizationServer::performAccessTokenFlow();
});
// 'oauth' filter makes sure there is a valid token present
Route::group(['before' => 'oauth'], function()
{
// Your protected endpoints
Route::resource('url', '[email protected]');
Route::resource('show', '[email protected]');
Route::resource('destroy', '[email protected]');
});
});
太好了,非常感谢:) – hkucuk 2014-08-29 07:05:43
@ c-griffin我们可以同时使用令牌csrf令牌和oauth2。 API表单的CSRF令牌和API的Oauth2令牌 – 2016-11-30 12:25:09
通过oAuth规范,令牌传递到标题中。如果您使用的是https,则标头将被加密。但总之,没有任何东西可以阻止任何人使用oAuth和CSRF相互结合。 – 2016-12-05 23:06:06