通过 Devise for Rails 使用每个用户的“maximum_attempts”值锁定用户
Locking a user using a per-user `maximum_attempts` value with Devise for Rails
作为事实上的标准,我们都在 Rails 应用程序中使用 Devise 进行登录,并在特定次数的失败尝试后使用 Lockable 模块锁定用户。
从 Devise 的 source code 和配置选项 config.maximum_attempts = 20
,我开始了解当用户尝试提供错误的登录凭据时 Devise 如何执行锁定。配置是在 initializers
.
中的 Rails 应用程序启动时静态定义的
我的期望是动态设置 maximum_attempts
– 这可能吗?如果是这样,请指导我。
我在每个管理员下面都有一个超级管理员和用户。基于超级管理员,我想在运行时为每个用户设置不同的 failed_attempt
值。
一种可能的方法是猴子修补 you linked to 的 Devise 代码,其中定义了 attempts_exceeded?
。这是对需要覆盖的内容的猜测:
module Devise::Models::Lockable
# assumes that the User model has a `superadmin` relation
# that has a `maximum_attempts` attribute
def attempts_exceeded?
self.failed_attempts >= self.superadmin.maximum_attempts
end
def last_attempt?
self.failed_attempts == self.superadmin.maximum_attempts - 1
end
end
这应该可行,但这意味着无论何时更新 Devise,都存在相关代码被破坏的风险,后果未知。因此,您必须在每次更新之前查看对 Devise 的更改。如果您因此而不愿更新 Devise,如果您更新到具有固定安全问题的 Devise 版本太慢,最终可能会导致安全问题。所以要小心那些可能的问题。
一种需要更多前期工作的更安全的方法是从您自己的代码中手动锁定用户。 Devise::Models::Lockable
的文档提到了一个 public 方法 lock_access!
that locks the user when you call it. You can set the global config.maximum_attempts
to some really high value such as 25. Then, in some callback on the model (I’m not sure which callback), call a method lock_access_based_on_superadmin_limit!
that calls lock_access!
if necessary according to your custom rules. The following definition is adapted from part of Devise’s valid_for_authentication?
:
class User
# …
def lock_access_based_on_superadmin_limit!
if failed_attempts >= superadmin.maximum_attempts
lock_access! unless access_locked?
end
end
end
作为事实上的标准,我们都在 Rails 应用程序中使用 Devise 进行登录,并在特定次数的失败尝试后使用 Lockable 模块锁定用户。
从 Devise 的 source code 和配置选项 config.maximum_attempts = 20
,我开始了解当用户尝试提供错误的登录凭据时 Devise 如何执行锁定。配置是在 initializers
.
我的期望是动态设置 maximum_attempts
– 这可能吗?如果是这样,请指导我。
我在每个管理员下面都有一个超级管理员和用户。基于超级管理员,我想在运行时为每个用户设置不同的 failed_attempt
值。
一种可能的方法是猴子修补 you linked to 的 Devise 代码,其中定义了 attempts_exceeded?
。这是对需要覆盖的内容的猜测:
module Devise::Models::Lockable
# assumes that the User model has a `superadmin` relation
# that has a `maximum_attempts` attribute
def attempts_exceeded?
self.failed_attempts >= self.superadmin.maximum_attempts
end
def last_attempt?
self.failed_attempts == self.superadmin.maximum_attempts - 1
end
end
这应该可行,但这意味着无论何时更新 Devise,都存在相关代码被破坏的风险,后果未知。因此,您必须在每次更新之前查看对 Devise 的更改。如果您因此而不愿更新 Devise,如果您更新到具有固定安全问题的 Devise 版本太慢,最终可能会导致安全问题。所以要小心那些可能的问题。
一种需要更多前期工作的更安全的方法是从您自己的代码中手动锁定用户。 Devise::Models::Lockable
的文档提到了一个 public 方法 lock_access!
that locks the user when you call it. You can set the global config.maximum_attempts
to some really high value such as 25. Then, in some callback on the model (I’m not sure which callback), call a method lock_access_based_on_superadmin_limit!
that calls lock_access!
if necessary according to your custom rules. The following definition is adapted from part of Devise’s valid_for_authentication?
:
class User
# …
def lock_access_based_on_superadmin_limit!
if failed_attempts >= superadmin.maximum_attempts
lock_access! unless access_locked?
end
end
end