【问题标题】:Customize Passport Queries time of authenticating自定义身份验证的 Passport 查询时间
【发布时间】:2019-05-13 09:22:42
【问题描述】:

对于每个请求,我发现会触发 4 个查询来验证用户和令牌。其中一种是根据用户id获取用户(select * from user)。此查询由 Passport/Laravel 触发,但我想要修改此查询以添加一个状态字段检查,以检查是否有任何用户在令牌有效期内变为无效。如果我们只检查 id,那么如果任何用户变为非活动状态(通过更改状态,我们也将无法阻止用户,因为删除用户的令牌对我来说不是一个好的解决方案)。

在 Passport Laravel 的每个请求上触发的查询:

select * from oauth_access_tokens where id = ? 
select * from user where id = ? limit 1 ["2"] 
select * from oauth_access_tokens where id = ? 
select * from oauth_clients where id = ?

那么,谁能告诉我如何在令牌验证时更改护照中的“select * from user where id”查询。

【问题讨论】:

    标签: laravel laravel-5 laravel-passport


    【解决方案1】:

    您可以将此方法添加到您的 User 模型(或您使用护照进行身份验证的任何模型)上

        ...
        public function findForPassport($username)
        {
            return $user = (new self)->where('email', $username)->where('is_active', 1)->first();
        }
        ...
    

    当然,只要返回 Illuminate\Contracts\Auth\Authenticatable 合同,您可以通过您使用的任何列(和/或任何查询约束)修改 is_active

    【讨论】:

    • 无需创建新实例,只需return $this->where(...)->first();
    【解决方案2】:

    我不会尝试修改护照的默认行为,因为我不知道它现在和未来的升级可能会影响什么。

    您最好的选择可能是挂钩护照事件并将您的业务逻辑应用于事件触发时调用的侦听器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-07
      • 2018-10-15
      • 2020-04-01
      • 2022-12-10
      • 1970-01-01
      • 2018-09-26
      • 2021-02-06
      • 2010-12-17
      相关资源
      最近更新 更多