php - 何时重新生成 session ID Laravel?

标签 php laravel http laravel-6

我正在构建一个带有登录名的应用程序,我已经阅读了有关使用 $request->session()->regenerate(); 重新生成 session ID 的信息。但我不明白我应该在哪里使用它,有人可以解释我应该在何时何地重新生成 session ID?

最佳答案

如果您看到默认身份验证,那么您可以看到他们正在重新生成 session 的每个登录身份验证。

protected function sendLoginResponse(Request $request)
    {
        $request->session()->regenerate();

        $this->clearLoginAttempts($request);

        if ($response = $this->authenticated($request, $this->guard()->user())) {
            return $response;
        }

        return $request->wantsJson()
                    ? new JsonResponse([], 204)
                    : redirect()->intended($this->redirectPath());
    }
主要目的是重新生成 session ID 通常是为了防止恶意用户利用 session 固定攻击 在您的申请中。
什么是 session 固定?

Session Fixation is an attack that permits an attacker to hijack a valid user session. The attack explores a limitation in the way the web application manages the session ID, more specifically the vulnerable web application. When authenticating a user, it doesn’t assign a new session ID, making it possible to use an existent session ID. The attack consists of obtaining a valid session ID (e.g. by connecting to the application), inducing a user to authenticate himself with that session ID, and then hijacking the user-validated session by the knowledge of the used session ID. The attacker has to provide a legitimate Web application session ID and try to make the victim’s browser use it.


根据文档

Regenerating the session ID is often done in order to prevent malicious users from exploiting a session fixation attack on your application.

Laravel automatically regenerates the session ID during authentication if you are using one of the Laravel application starter kits or Laravel Fortify; however, if you need to manually regenerate the session ID, you may use the regenerate method:

$request->session()->regenerate(); If you need to regenerate the session ID and remove all data from the session in a single statement, you may use the invalidate method:

$request->session()->invalidate();


引用:https://laravel.com/docs/8.x/session
引用:https://owasp.org/www-community/attacks/Session_fixation

关于php - 何时重新生成 session ID Laravel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68083063/

相关文章:

http - 非 2xx 状态代码响应是否应包含 CORS 特定 header

java - 来自java的多部分文件上传发布请求

ios - 如何在不被请求的情况下将信息从服务器发送到 iOS

php - 输出 1/0//Yes/No 的比较工具

Laravel 获取配置变量

javascript - 使用 Laravel 在服务器上上传声音 Blob

linux - 使用 Linux 用户凭证的 Laravel 身份验证

php - MySQL 仅针对特定日期的无效日期时间格式

php - 当我想为 braintree 上的每次点击结账生成 token 时,我遇到了 ssl 证书错误

php - 如何简化我的 php 代码(插入、更新和删除)