代码之家  ›  专栏  ›  技术社区  ›  EastsideDev

在rails 3应用程序中使用devieve_security_扩展

  •  0
  • EastsideDev  · 技术社区  · 6 年前

    我正在尝试将devieve_security_扩展用于现有的rails 3应用程序,以便可以控制以下内容:

    • 确保密码长度在8到70个字符之间,并且至少有一个小写字符、一个大写字符、一个数字和一个特殊字符。

    • 旧密码已存档,最多6个

    • 不能使用旧密码(不在存档中)

    • 密码3个月后过期

    • 超过3个月未使用的帐户已过期

    • 会话在不活动15分钟后过期

    我在gemfile中添加了以下内容:

    gem 'devise_security_extension', '0.10.0'
    

    在config/initializers/devieve.rb中,我添加了以下内容:

    config.expire_password_after = 3.months
    config.password_regex= /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,70}$/
    config.password_archiving_count = 6
    config.deny_old_passwords = true
    config.expire_after = 3.months
    config.timeout_in = 15.minutes
    config.expire_auth_token_on_timeout = true
    

    在my models/user.rb中,我有以下内容:

    devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :invitable,
         :confirmable, :password_expirable, :secure_validatable, :password_archivable,
         :session_limitable, :expirable
    

    然后,我做了以下操作:

    bundle install
    rails generate devise_security_extension:install
    

    但是,我注意到没有生成迁移文件。我有没有错过这里的台阶?

    1 回复  |  直到 6 年前
        1
  •  1
  •   SteveTurczyn    6 年前

    线路

    rails generate devise_security_extension:install
    

    不创建迁移,而是将可选配置添加到 config/initializers/devise.rb

    对于password_expirable和password_archible,您应该创建一些迁移:存储密码更改日期的字段…

    rails g migration AddPasswordChangedAtToUser password_changed_at:datetime:index
    

    …和一个跟踪旧密码的表

    create_table :old_passwords do |t|
      t.string :encrypted_password, :null => false
      t.string :password_archivable_type, :null => false
      t.integer :password_archivable_id, :null => false
      t.datetime :created_at
    end
    add_index :old_passwords, [:password_archivable_type, :password_archivable_id], :name => :index_password_archivable
    

    所有这些都在github gem自述文件中解释过…

    https://github.com/phatworx/devise_security_extension