Rails 3.2
我正在使用Rails进行用户管理,正在开发Rails应用程序。新要求是向
添加一些规则我在Gemfile中添加了以下内容:
gem 'devise_security_extension', '0.10.0'
在我的config / initializers / devise.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
我修改了我的模型/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
根据StackOverflow的建议,我还执行了以下操作:
rails g migration AddPasswordChangedAtToUser password_changed_at:datetime:index
产生了以下迁移:
class AddPasswordChangedAtToUser < ActiveRecord::Migration
def change
add_column :users, :password_changed_at, :datetime
add_index :users, :password_changed_at
end
end
然后,我执行了以下操作:
rails g migration CreateOldPasswordsTable
创建了一个迁移,我对其进行了如下编辑:
class CreateOldPasswordsTable < ActiveRecord::Migration
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
end
我运行了迁移,并确保修改了表用户,并创建了表old_passwords
运行该应用程序时,出现登录屏幕,并尝试使用现有凭据登录时(从日志文件中)获得以下信息:
Started POST "/users/sign_in" for 63.251.170.1 at 2018-10-24 00:08:34 +0000
Processing by SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Mi9uscJh/b6qusoV3kclz7FXWj952AnYV9mNwURG8tQ=", "user"=>{"email"=>"xxxxx@gmail.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
[1m[35mUser Load (0.4ms)[0m SELECT `users`.* FROM `users` WHERE `users`.`email` = 'xxxxx@gmail.com' LIMIT 1
Completed 500 Internal Server Error in 81.7ms
NoMethodError (undefined method `expired_at' for #<User:0x0000000a91b360>):
app/middleware/catch_json_parse_errors.rb:8:in `call'
有什么想法吗?
我缺少对users表的以下修改:
add_column :users, :password_changed_at, :datetime
add_index :users, :password_changed_at
add_column :users, :unique_session_id, :string, :limit => 20
add_column :users, :last_activity_at, :datetime
add_index :users, :last_activity_at
add_column :users, :expired_at, :datetime
add_index :users, :expired_at