【问题标题】:Laravel 5 - Logout a user from all of his devicesLaravel 5 - 从他的所有设备中注销用户
【发布时间】:2015-09-01 18:15:24
【问题描述】:

用户已登录。他还已经登录了 3 台不同的计算机。 现在用户更改了他的密码。

我想做点什么让他从他的所有设备上注销。默认情况下,如果我们在一台设备上更改密码,其他设备上不会发生任何事情。

首先想到的是在中间件(每个请求)中检查密码,这不好并且会显着降低性能。

我如何在 Laravel 5 中做到这一点?

AND 最好的方法是什么?大型网站如何从所有设备上注销用户?

【问题讨论】:

  • 是否可以通过用户 ID 获取会话?这样,如果用户更改了密码,您可以检查他们是否还有其他活动会话,然后将其删除?
  • 您使用的是哪种会话驱动程序?
  • @michael 我正在使用 memcached
  • 可能没有安装redis!!

标签: php session laravel laravel-5 logout


【解决方案1】:

在最新版本的 Laravel 5.6 中

你可以从

auth()->logoutOtherDevices();

logoutOtherDevices

More Info

【讨论】:

    【解决方案2】:

    我做了类似的事情。起初,我将会话保存在 Redis 中。对于每个登录,我在成功验证后保存会话 ID 并将其与用户 ID(它是数组)相关联。如果用户更改密码,您可以删除除当前用户会话之外的所有用户会话(使用会话 ID)。如果用户注销,您可以从用户会话数组中删除会话 ID。 (我认为您可以使用 MySQL 或其他存储来保存用户和会话 ID 之间的关系)

    我使用的保存会话 ID(当用户登录时)

    $redis = \LRedis::connection();
    $redis->sadd('user:sessions:' . $userId, Session::getId());
    

    用于从用户会话数组中删除会话 ID(如果用户注销或手动注销)

    $redis->srem('user:sessions:' . $userId, $sessionId);
    

    删除 Laravel 会话(从其他设备注销用户)

    $redis->del('laravel:' . $sessionId);
    

    获取用户的所有会话 ID

    $redis->smembers('user:sessions:' . $userId);
    

    从所有设备注销使用循环

    $userSessions = $redis->smembers('user:sessions:' . $userId);
    $currentSession = Session::getId()
    foreach ($userSessions as $sessionId) {
        if ($currentSession == $sessionId) {
            continue;  //if you want don't remove current session
        }
    
        $redis->del('laravel:' . $sessionId);
        $redis->srem('user:sessions:' . $userId, $sessionId);
    }
    

    【讨论】:

    • 我已经实施了您建议的相同流程,但我面临以下错误。您能帮我找出答案吗? ConnectionException in AbstractConnection.php line 168: No connection could be made because the target machine actively refused it. [tcp://127.0.0.1:6379]
    • 您好尝试使用redis cli连接到redis redis-cli
    • 我更新了几乎相同的问题。你能帮我找出来吗? stackoverflow.com/questions/37405740/…
    • 当用户使用记住我功能时如何使会话失效?每当我使会话无效时,由于 记住我 功能,laravel 会创建一个新会话。没有记住我功能,它工作得很好! (我正在尝试回答这个问题:stackoverflow.com/q/50147632/6168395
    【解决方案3】:

    From laravel documents:

    Invalidating Sessions On Other Devices

    Laravel 提供了一种使 a 无效和“注销”的机制 在其他设备上处于活动状态且不会失效的用户会话 他们当前设备上的会话。

    首先,您应该确保 Illuminate\Session\Middleware\AuthenticateSession 中间件是 在您的 app/Http/Kernel.php 类的网络中显示和取消评论 中间件组:

    'web' => [
        // ...
        \Illuminate\Session\Middleware\AuthenticateSession::class,
        // ...
    ],
    

    然后,您可以在 Auth 外观上使用 logoutOtherDevices 方法。 此方法要求用户提供他们当前的密码,这 您的应用程序应通过输入表单接受:

    use Illuminate\Support\Facades\Auth;
    
    Auth::logoutOtherDevices($password);
    

    logoutOtherDevices方法被调用时,用户的其他 会话将完全失效,这意味着它们将被“记录 在他们之前认证过的所有守卫中。

    【讨论】:

      【解决方案4】:

      使其他设备上的会话无效

      Laravel provides a mechanism for invalidating and "logging out" a user's sessions that are active on other devices without invalidating the session on their current device.
      
      First, you should make sure that the Illuminate\Session\Middleware\AuthenticateSession middleware is present and un-commented in your app/Http/Kernel.php class' web middleware group:
      
      'web' => [
          // ...
          \Illuminate\Session\Middleware\AuthenticateSession::class,
          // ...
      ],
      
      Then, you may use the logoutOtherDevices method on the Auth facade. This method requires the user to provide their current password, which your application should accept through an input form:
      
      use Illuminate\Support\Facades\Auth;
      
      Auth::logoutOtherDevices(request('password'));
      
      When the logoutOtherDevices method is invoked, the user's other sessions will be invalidated entirely, meaning they will be "logged out" of all guards they were previously authenticated by.
      

      【讨论】:

        猜你喜欢
        • 2016-09-24
        • 1970-01-01
        • 2020-05-10
        • 2016-11-04
        • 2022-10-09
        • 2016-09-22
        • 2016-08-14
        • 2019-04-30
        • 2022-11-10
        相关资源
        最近更新 更多