在 Laravel 中使 API 安宁的最佳方式
Best way to make restfull API in Laravel
我正在用 Laravel 4 为外部网站做一个 web scraping 的 restfull API。
目标站点有一个登录表单,因此每个请求都需要身份验证。
如果用户想要 post 或查看某些内容,他向我的服务器发出请求,然后向目标服务器发出另一个请求,获取信息,并将其编码为 JSON。
我的问题是如何在 API 请求中获取凭据?
现在我有类似 http://myapi.local/login
的东西(这向 http://externalsite.com/admin/login
发出请求),
POST 参数是 username=test&password=1234
并且 returns 一个 session ID
然后对于每个操作,我将 session ID 附加到我的 api 请求
http://myapi.local/posts/all?session_id=4D2FtE...
但这根本不是restfull,所以最好是使用HTTP Basic Auth,即为每个请求做一次登录
url: http://myapi.local/posts/all
header: Authorization: Basic dGVzdDoxMjM0
并在我的控制器中调用登录功能。
速度较慢,因为它每次向目标站点发出两个请求,但似乎更好,因为我没有保存任何 session 或凭据。
如何处理 Laravel 中的授权 header?解码 base64 然后拆分凭据?
有更好的方法吗?
谢谢!
Laravel 自己处理基本身份验证,唯一要做的就是考虑在哪里可以使用过滤器(Laravel 使用过滤器处理基本身份验证),所以:
a) 在路线中:
Route::get('posts/all', array('before' => 'auth.basic', function()
{
// Only authenticated users may enter...
}));
b) 控制器中的构造函数(我更喜欢这个):
function __construct() {
$this->beforeFilter('auth.basic');
}
如果适用于您的情况,也请进行此调整,如 laravel 文档所述:
By default, the basic filter will use the email column on the user
record when authenticating. If you wish to use another column you may
pass the column name as the first parameter to the basic method in
your app/filters.php file:
Route::filter('auth.basic', function()
{
return Auth::basic('username');
});
已编辑
在您的情况下,您可能希望以这两种方法为基础实现自定义过滤器。
/**
* Get the credential array for a HTTP Basic request.
*/
function getBasicCredentials(Request $request, $field)
{
return array($field => $request->getUser(), 'password' => $request->getPassword());
}
/**
* Get the response for basic authentication.
*
* @return \Symfony\Component\HttpFoundation\Response
*/
function getBasicResponse()
{
$headers = array('WWW-Authenticate' => 'Basic');
return new Response('Invalid credentials.', 401, $headers);
}
查看默认实现 here:
我正在用 Laravel 4 为外部网站做一个 web scraping 的 restfull API。
目标站点有一个登录表单,因此每个请求都需要身份验证。
如果用户想要 post 或查看某些内容,他向我的服务器发出请求,然后向目标服务器发出另一个请求,获取信息,并将其编码为 JSON。
我的问题是如何在 API 请求中获取凭据?
现在我有类似 http://myapi.local/login
的东西(这向 http://externalsite.com/admin/login
发出请求),
POST 参数是 username=test&password=1234
并且 returns 一个 session ID
然后对于每个操作,我将 session ID 附加到我的 api 请求
http://myapi.local/posts/all?session_id=4D2FtE...
但这根本不是restfull,所以最好是使用HTTP Basic Auth,即为每个请求做一次登录
url: http://myapi.local/posts/all
header: Authorization: Basic dGVzdDoxMjM0
并在我的控制器中调用登录功能。
速度较慢,因为它每次向目标站点发出两个请求,但似乎更好,因为我没有保存任何 session 或凭据。
如何处理 Laravel 中的授权 header?解码 base64 然后拆分凭据?
有更好的方法吗?
谢谢!
Laravel 自己处理基本身份验证,唯一要做的就是考虑在哪里可以使用过滤器(Laravel 使用过滤器处理基本身份验证),所以:
a) 在路线中:
Route::get('posts/all', array('before' => 'auth.basic', function()
{
// Only authenticated users may enter...
}));
b) 控制器中的构造函数(我更喜欢这个):
function __construct() {
$this->beforeFilter('auth.basic');
}
如果适用于您的情况,也请进行此调整,如 laravel 文档所述:
By default, the basic filter will use the email column on the user record when authenticating. If you wish to use another column you may pass the column name as the first parameter to the basic method in your app/filters.php file:
Route::filter('auth.basic', function()
{
return Auth::basic('username');
});
已编辑
在您的情况下,您可能希望以这两种方法为基础实现自定义过滤器。
/**
* Get the credential array for a HTTP Basic request.
*/
function getBasicCredentials(Request $request, $field)
{
return array($field => $request->getUser(), 'password' => $request->getPassword());
}
/**
* Get the response for basic authentication.
*
* @return \Symfony\Component\HttpFoundation\Response
*/
function getBasicResponse()
{
$headers = array('WWW-Authenticate' => 'Basic');
return new Response('Invalid credentials.', 401, $headers);
}
查看默认实现 here: