百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
D

devise-security

> 后端框架
开源

一个用于设备的安全扩展,满足 Web 应用程序的行业标准安全需求。

639 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

一个用于设备的安全扩展,满足 Web 应用程序的行业标准安全需求。

Devise Security

A Devise extension to add additional security features required by modern web applications. Forked from Devise Security Extension

It is composed of 7 additional Devise modules:

  • :password_expirable - passwords will expire after a configured time (and will need to be changed by the user). You will most likely want to use :password_expirable together with the :password_archivable module to prevent the current expired password from being reused immediately as the new password.
  • :secure_validatable - better way to validate a model (email, stronger password validation). Don't use with Devise :validatable module!
  • :password_archivable - save used passwords in an old_passwords table for history checks (prevent reusing passwords)
  • :session_limitable - ensures, that there is only one session usable per account at once
  • :expirable - expires a user account after x days of inactivity (default 90 days)
  • :security_questionable - as accessible substitution for captchas (security question with captcha fallback)
  • :paranoid_verification - admin can generate verification code that user needs to fill in otherwise he won't be able to use the application.

Configuration and database schema for each module below.

Additional features

captcha support for sign_up, sign_in, recover and unlock (to make automated mass creation and brute forcing of accounts harder)

Getting started

Devise Security works with Devise on Rails >= 7.0. You can add it to your Gemfile after you successfully set up Devise (see Devise documentation) with:

ruby
gem 'devise-security'

Run the bundle command to install it.

After you installed Devise Security you need to run the generator:

bash
rails generate devise_security:install

The generator adds optional configurations to config/initializers/devise_security.rb. Enable the modules you wish to use in the initializer you are ready to add Devise Security modules on top of Devise modules to any of your Devise models:

ruby
devise :password_expirable, :secure_validatable, :password_archivable, :session_limitable, :expirable

E-mail Validation

For :secure_validatable you need to have a way to validate an e-mail. There are multiple libraries that support this, and even a way built into Ruby!

  • (Recommended) Ruby built-in URI::MailTo::EMAIL_REGEXP constant

    Note: This method would require a email_validation method to be defined in order to hook into the validates method defined here.

  • email_address gem
  • valid_email2 gem
  • rails_email_validator gem (deprecated)

Configuration

…

Other ORMs

Devise-security supports Mongoid as an alternative ORM to active_record. To use this ORM, add this to your Gemfile.

ruby
gem 'mongoid'

And then ensure that the environment variable DEVISE_ORM=mongoid is set.

For local development you will need to have MongoDB installed locally.

bash
brew install mongodb

Rails App setup example with Mongoid

ruby
# inside config/application.rb
    require File.expand_path('../boot', __FILE__)
    #...
    DEVISE_ORM=:mongoid

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
    Bundler.require(*Rails.groups)

    module MyApp
  class Application 

   

Schema

Password expirable

ruby
create_table :the_resources do |t|
  # other devise fields

  t.datetime :password_changed_at
end
add_index :the_resources, :password_changed_at

Note: setting password_changed_at to nil will require the user to change their password.

Password archivable

ruby
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.string :password_salt # Optional. bcrypt stores the salt in the encrypted password field so this column may not be necessary.
  t.datetime :created_at
end
add_index :old_passwords, [:password_archivable_type, :password_archivable_id], name: 'index_password_archivable'

Session limitable

ruby
create_table :the_resources do |t|
  # other devise fields

  t.string :unique_session_id
end

Bypassing session limitable

Sometimes it's useful to impersonate a user without authentication (e.g. administrator impersonating a user), in this case the session_limitable strategy will log out the user, and if the user logs in while the administrator is still logged in, the administrator will be logged out.

For such cases the following can be used:

ruby
sign_in(User.find(params[:id]), scope: :user, skip_session_limitable: true)

Expirable

ruby
create_table :the_resources do |t|
  # other devise fields

  t.datetime :last_activity_at
  t.datetime :expired_at
end
add_index :the_resources, :last_activity_at
add_index :the_resources, :expired_at

Paranoid verifiable

ruby
create_table :the_resources do |t|
  # other devise fields

  t.string   :paranoid_verification_code
  t.integer  :paranoid_verification_attempt, default: 0
  t.datetime :paranoid_verified_at
end
add_index :the_resources, :paranoid_verification_code
add_index :the_resources, :paranoid_verified_at

Documentation for Paranoid Verifiable module

Security questionable

…

sh export DEVISE_ORM=active_record # or mongoid. Default is active_record bundle exec appraisal # installs dependencies for each Rails version bundle exec appraisal update # if updating dependencies is necessary bundle exec appraisal generate # to regenerate the appraisal gemfiles (if necessary) bundle exec appraisal rake # run the tests for each Rails version


## Maintenance Policy

We are committed to maintaining support for `devise-security` for all normal or
security maintenance versions of the Ruby language
[as listed here](https://www.ruby-lang.org/en/downloads/branches/), and for the
Ruby on Rails framework
[as per their maintenance policy](https://rubyonrails.org/maintenance/).

To avoid introducing bugs caused by backwardly incompatible Ruby language features, it is highly recommended that all
development work be done using the oldest supported Ruby version. In order for the devcontainer to build properly the
ruby version in `.ruby-version` should be the oldest version that supports installing dependencies for all of the
supported Rails versions.

## Copyright

Copyright (c) 2017-2025 Dillon Welch & Kevin Olbrich.

Copyright (c) 2011-2017 Marco Scholl as the project [`devise_security_extension`](https://github.com/phatworx/devise_security_extension).

This repo was created as a fork from [b2ee978a](https://github.com/phatworx/devise_security_extension/commit/b2ee978af7d49f0fb0e7271c6ac074dfb4d39353).

See LICENSE.txt for further details.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyactiverecorddevisedevise-moduleshacktoberfest

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架