设计:可锁定 - last_attempt_warning 不显示

2024-02-10

我正在尝试实施:lockable模块到我的设备如下这个维基 https://github.com/plataformatec/devise/wiki/How-To:-Add-:lockable-to-Users但遇到了一些问题。在开发过程中,当登录尝试超过maximum_attempts次,failed_attempts属性得到正确更新并且用户帐户被锁定,但是:

1)尽管config.last_attempt_warning = true不显示警告消息

2)我得到一个unlock_instructions电子邮件,但是当我将链接复制粘贴到浏览器中时,我收到一个invalid authorisation token error.

配置/初始化器/devise.rb

# ==> Configuration for :lockable
config.lock_strategy = :failed_attempts
config.unlock_keys = [:email]
config.unlock_strategy = :email
config.maximum_attempts = 3
config.last_attempt_warning = true

模型/用户.rb

class User < ApplicationRecord
  devise :database_authenticatable, :confirmable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :lockable
end

视图/设计/会话/新

= flash[:alert] if flash[:alert]
= flash[:notice] if flash[:notice]

= simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f|
  .form-inputs
    = f.input :email, required: false, autofocus: true
    = f.input :password, required: false, autocomplete: "off"
    = f.input :remember_me, as: :boolean if devise_mapping.rememberable?
  .form-actions
    = f.button :submit, "Log in"

db/migrate/YYYYMMDDxxx_add_lockable_to_devise.rb

class AddLockableToUsers < ActiveRecord::Migration[5.2]
  def change
    add_column :users, :failed_attempts, :integer, default: 0, null: false
    add_column :users, :unlock_token, :string
    add_column :users, :locked_at, :datetime
    add_index :users, :unlock_token, unique: true
  end
end

我没有任何待处理的迁移,也尝试重置数据库并重新启动服务器,但没有成功。 关于我做错了什么有什么想法吗?提前致谢。


经过大量挖掘,我成功解决了这两个问题:

警告消息不显示

第一个问题是由配置引起的:

配置/初始化器/devise.rb

config.paranoid = true

如果你看一下设计模块 https://github.com/plataformatec/devise/blob/master/lib/devise/models/lockable.rb#L121:

如果设置为偏执模式,则不显示锁定消息,因为它 泄露帐户的存在。

根据您的安全要求,您可以将此值更改为false或者对电子邮件保密并且不提供反馈。 如果您有兴趣在登录尝试失败时自定义消息,我强烈建议您阅读this https://stackoverflow.com/questions/44004275/custom-message-on-devise-lockable-n-th-attempt.


授权令牌无效

第二个问题是由我直接从电子邮件的源代码复制链接引起的 - 因为=被编码为3D=,链接显然已损坏。进一步说明here https://stackoverflow.com/questions/31895282/escaping-of-rails-generated-url.

希望这对遇到类似问题的人有所帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

设计:可锁定 - last_attempt_warning 不显示 的相关文章

随机推荐