Iso*_*Iso 10 api session-cookies laravel laravel-4
我正在使用Laravel构建RESTful API.我使用Basic HTTP Auth(Authenticate header),使用此过滤器:
Route::filter('auth', function()
{
$credentials = ['email' => Request::getUser(), 'password' => Request::getPassword()];
if (!Auth::once($credentials)) {
$response = ['error' => true, 'message' => 'Unauthorized request'];
$code = 401;
$headers = ['WWW-Authenticate' => 'Basic'];
return Response::json($response, $code, $headers);
}
});
Run Code Online (Sandbox Code Playgroud)
它可以工作,但Laravel然后尝试为用户设置cookie(发送Set-Cookie标题).我尝试将session.driver配置键设置为array,只是为了看它现在发送一个Set-Cookie: laravel_session=deleted东西.
如何完全禁用此Set-Cookie标头?
谢谢.
seb*_*ebt 15
对于无状态API,没有cookie和干净的标头以下工作:
Route::filter('auth.basic', function()
{
Config::set('session.driver', 'array');
return Auth::onceBasic();
});
Run Code Online (Sandbox Code Playgroud)
请注意,上面使用的是Auth :: onceBasic(),由于某种原因仍然会发送"Set-Cookie"标头.根据文件,曾经的基督教是无国籍的; 也许cookie是出于信息目的而发送的,是调试模式的副作用,或者它可能是一个bug.无论哪种方式,仍然需要Config :: set(...).使用此过滤器快速卷曲路线会返回以下标题:
HTTP/1.1 200 OK
Date: Wed, 12 Feb 2014 02:34:26 GMT
Server: Apache/2.4.6 (Ubuntu)
X-Powered-By: PHP/5.5.3
Cache-Control: no-cache
X-Frame-Options: SAMEORIGIN
Transfer-Encoding: chunked
Content-Type: application/json
Run Code Online (Sandbox Code Playgroud)
Auth :: onceBasic()似乎是无状态REST API的好方法.每个客户端请求都经过身份验证,并且此方法中不使用会话cookie.
NB.其他路由未被上述过滤器捕获,仍将设置cookie(并发送"Set-Cookie"标头).因此,此解决方案适用于无状态API和有状态Web访问/管理员的常见情况.
要禁用Laravel 4控制器中所有路由的会话,请在类构造中设置会话驱动程序选项:
<?php
class ExampleController extends BaseController {
public function __construct()
{
Config::set('session.driver', 'array');
}
public function getExample()
{
return "This example response should have no session cookie.";
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8099 次 |
| 最近记录: |