Laravel几秒钟后自动注销? [英] Laravel automatically logged out after few seconds?
问题描述
我正在使用Laravel 5和带有RESTFUL API的angularJs开发Web应用程序.
I am developing web application using Laravel 5 and angularJs with RESTFUL apis.
使用middleware
进行身份验证.我的问题是同时发送少量请求后,系统自动注销并从laravel端发送401异常.
Using middleware
to authentication purpose. My problem is after sending few request simultaneously,system automatically logged out and sending 401 exception from laravel side.
API基本控制器:
class ApiController extends BaseController {
use DispatchesCommands, ValidatesRequests;
function __construct() {
$this->middleware('api.auth');
}
}
中间件:
class APIMiddleware {
/**
* Handle an incoming request.
*
* @param Request $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next) {
if (!Auth::check()) {
abort(401, "Unauthorized");
}
return $next($request);
}
}
登录控制器
public function login(LoginRequest $request) {
if (Auth::check()) {
Auth::logout();
}
if (Auth::attempt(['email' => $request->input('email'), 'password' => $request->input('password')], $request->input('is_remember'))) {
return array(true);
} else {
abort(401, "Invalid email & password");
}
}
几个请求消失后,服务器注销并发送401异常.我陷入了这个问题.
After few request gone, Server log out and sends 401 exception. I am stuck with this issue.
推荐答案
现在我不是100%确定(根据您的设置,我什至不能说我90%确定),但是在更改我的session_driver
从file
到database
我似乎已经解决了此问题-也就是说,如果是同一问题.
Now I'm not 100% sure (and depending on your set-up I can't even say I'm 90% sure) But after changing my session_driver
from file
to database
I seem to have fixed this issue - that is if it's the same issue.
我想使用我的应用程序执行与您相同的操作-在页面启动时,我正在发出6个请求(这是 development ,我将其更改为一个,请不要哭泣).如果我加载此页面,它可以处理大约3个或4个请求,然后其他2-3个返回unauthorised
响应.它也仅在需要middleware => auth
的请求下发生.
I think do the samething as you with my app - that is on a start up of a page, I'm making 6 request (this is development and I will be changing it to one so please don't cry). If I load this page, it works with about 3 or 4 request, then the other 2-3 come back with a unauthorised
response. It also only happens on request that require middleware => auth
.
所以这是发生这种情况的我的理论:因为默认情况下,会话保存在文件中-在以下位置发出多个请求一次表示文件一次被打开6次-可能将其弄乱了(取决于您的计算机).因此,将会话更改为旨在一次具有数千个请求的数据库是可行的!
So here's my theory to why this is happening: Because, by default, sessions are saved in a file - making multiple requests at once means that file is being opened 6 times at once - probably messing it up (depending on your machine). Therefore changing the session to a database, which is designed to have thousands of requests at once, works!
解决方案:
- 转到您的
.env
文件,然后将SESSION_DRIVER=file
更改为SESSION_DRIVER=database
. - 接下来,您将需要创建会话迁移:
php artisan session:table
. - 现在,
composer dump-autoload
是一个很好的实践. - 最终迁移(
php artisan migrate
).
- Go to your
.env
file and changeSESSION_DRIVER=file
toSESSION_DRIVER=database
. - Next you will need to create a session migration:
php artisan session:table
. - Now
composer dump-autoload
for good practice. - Finally migrate (
php artisan migrate
).
注意:尽管我不是100%知道是这种情况,但是对我来说,这种解决方案有效.我也知道这个问题确实很老,但是我和我一起工作的开发人员以及我自己都遇到了这个问题,而且似乎没有解决方案,所以尽管我会发布这个问题.
NOTE: I'm not 100% sure though if this is the case, but for me this solution worked. I am also aware that this question is really old, but both the developers I work with and myself have had this issue and there doesn't seem to be a solution, so Just though I'd post this.
这篇关于Laravel几秒钟后自动注销?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!