Laravel 几秒后自动退出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29350564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:15:43  来源:igfitidea点击:

Laravel automatically logged out after few seconds?

phpangularjssessionlaravellaravel-5

提问by gsk

I am developing web application using Laravel 5 and angularJs with RESTFUL apis.

我正在使用 Laravel 5 和带有 RESTFUL api 的 angularJs 开发 Web 应用程序。

Using middlewareto authentication purpose. My problem is after sending few request simultaneously,system automatically logged out and sending 401 exception from laravel side.

使用middleware身份验证的目的。我的问题是在同时发送几个请求后,系统自动注销并从 laravel 端发送 401 异常。

API base controller:

API 基础控制器:

class ApiController extends BaseController {

    use DispatchesCommands, ValidatesRequests;

    function __construct() {
        $this->middleware('api.auth');
    }

}

Middleware:

中间件:

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);
    }

}

Log in controller

登录控制器

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");
    }
}

After few request gone, Server log out and sends 401 exception. I am stuck with this issue.

几个请求消失后,服务器注销并发送 401 异常。我被这个问题困住了。

回答by Ashley Wrench

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_driverfrom fileto databaseI seem to have fixed this issue - that is if it's the same issue.

现在我不是 100% 确定(并且根据您的设置,我什至不能说我有 90% 确定)但是在将我的session_driverfrom更改为 之后filedatabase我似乎已经解决了这个问题——如果是同样的问题.

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 developmentand I will be changing it to oneso 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 unauthorisedresponse. It also only happens on request that require middleware => auth.

我想和你一样用我的应用程序做同样的事情 - 那是在一个页面的启动时,我提出 6 个请求(这是开发,我将把它改成一个,所以请不要哭)。如果我加载此页面,它可以处理大约 3 或 4 个请求,然后其他 2-3 个请求返回unauthorised响应。它也仅在 require 的请求时发生middleware => auth

So here's my theory to why this is happening:Because, by default, sessionsare saved in a file- making multiple requests at once means that fileis 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!

所以这是我为什么会发生这种情况的理论:因为默认情况下,会话保存在一个文件中- 一次发出多个请求意味着该文件一次被打开 6 次 - 可能会搞砸(取决于你的机器)。因此,将会话更改为旨在同时处理数千个请求的数据库是有效的!

SOLUTION:

解决方案:

  1. Go to your .envfile and change SESSION_DRIVER=fileto SESSION_DRIVER=database.
  2. Next you will need to create a session migration: php artisan session:table.
  3. Now composer dump-autoloadfor good practice.
  4. Finally migrate (php artisan migrate).
  1. 转到您的.env文件并更改SESSION_DRIVER=fileSESSION_DRIVER=database.
  2. 接下来,您将需要创建会话迁移:php artisan session:table
  3. 现在composer dump-autoload进行良好的练习。
  4. 最后迁移(php artisan migrate)。

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.

注意:如果是这种情况,我不是 100% 确定,但对我来说,这个解决方案有效。我也知道这个问题真的很老,但是我合作的开发人员和我自己都遇到了这个问题并且似乎没有解决方案,所以尽管我会发布这个。

回答by gsk

It may be a problem that you are accessing the user variable illegally. Please use Auth::check()before accessing Auth::user()This seems to work for my project. Optionally you can try for changing the session driver from .env file.

可能是您非法访问用户变量的问题。请Auth::check()在访问之前使用Auth::user()这似乎适用于我的项目。或者,您可以尝试从 .env 文件更改会话驱动程序。

回答by ako

Managed to figure it out.. Since i use laravel for pretty much all my projects, I forgot to change the session name, as a result, one session was overwriting the other, causing the auto-loggout.. So if you have multiple laravel projects running, make sure they all have different session names. Hope this helps someone in future ! Here is a Laracast thread on this issue.

设法弄明白了.. 因为我几乎所有的项目都使用 laravel,所以我忘记更改会话名称,结果,一个会话覆盖了另一个会话,导致自动注销.. 所以如果你有多个 laravel正在运行的项目,请确保它们都有不同的会话名称。希望这对将来的人有所帮助! 这是有关此问题的 Laracast 线程。

For me this was the process to solve the problem:

对我来说,这是解决问题的过程:

  1. Cleared my browser's cookies for localhost.
  2. Changed value of cookiekey in app/session.php.
  3. Ran php artisan config:clear.
  1. 为本地主机清除了浏览器的 cookie。
  2. 更改了cookiekey in 的值app/session.php
  3. php artisan config:clear

回答by Carsim Rheedwan Adedotun Holuw

I solved the same issue by clearing cache using php artisan cache:clear and also running composer dump-autoload. Hope this works for you.

我通过使用 php artisan cache:clear 清除缓存并运行 composer dump-autoload 解决了同样的问题。希望这对你有用。

回答by Cleber de Souza Alcantara

I had a similar problem this week. I have a server with multiple Laravel applications. One application was logging the other out.

这周我遇到了类似的问题。我有一个带有多个 Laravel 应用程序的服务器。一个应用程序正在注销另一个。

The problem had to do with session management. The session name was the same for all the applications. Changing it would be enough to avoid different applications conflict. However, I can have different instances of the same application in the server (for testing purposes, for example). So, changing only the session name would not be enough.

问题与会话管理有关。所有应用程序的会话名称都相同。改变它就足以避免不同的应用程序冲突。但是,我可以在服务器中拥有相同应用程序的不同实例(例如,出于测试目的)。因此,仅更改会话名称是不够的。

To solve my problem properly, I used the session path to make the configuration unique per instance. In the config/session.php, I defined something like this:

为了正确解决我的问题,我使用会话路径使每个实例的配置唯一。在 中config/session.php,我定义了这样的东西:

'cookie' => 'systemx_session',
'path' => parse_url(env('APP_URL', 'http://localhost'), PHP_URL_PATH),

I use the parse_urlfunction with the environment variable APP_URLbecause my server has the instances deployed under something like http://example.com/systemx.

我将该parse_url函数与环境变量一起使用,APP_URL因为我的服务器将实例部署在类似http://example.com/systemx.

I hope this helps someone who might end up having the same kind of problem.

我希望这可以帮助那些可能最终遇到同样问题的人。

回答by Arifur Rahman

I think you copied an old project for a new application, so you need to change the config/session.php

我认为您为新应用程序复制了一个旧项目,因此您需要更改 config/session.php

'cookie' => 'new_session',

回答by George Sharvadze

Might be useful for someone: Had the very same problem. I've changed the cookie name in session settings. By default it is laravel_session, so try setting it to something else

可能对某人有用:有同样的问题。我在会话设置中更改了 cookie 名称。默认情况下它是 laravel_session,因此尝试将其设置为其他内容