实例:
$ git clone git://github.com/RailsApps/rails3-devise-rspec-cucumber.git
$ bundle install
$ rake db:migrate
在config/environments/production.rb文件中修改代码:
config.serve_static_assets = false
==================================
protect_from_forgery
<%= csrf_meta_tags %>
===================================
VIEW
<% if user_signed_in? %>
Controller
before_filter :authenticate_user!
before_filter :authenticate_user!, :only => [:edit]
routes
authenticated :user do
resources :users
end
Helpers
current_admin
current_user
=================== model ===============
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
## Database authenticatable
field :login_name, :type => String, :default => ""
field :encrypted_password, :type => String, :default => ""
## Recoverable
field :reset_password_token, :type => String
field :reset_password_sent_at, :type => Time
## Rememberable
field :remember_created_at, :type => Time
## Trackable
field :sign_in_count, :type => Integer, :default => 0
field :current_sign_in_at, :type => Time
field :last_sign_in_at, :type => Time
field :current_sign_in_ip, :type => String
field :last_sign_in_ip, :type => String
## Confirmable
# field :confirmation_token, :type => String
# field :confirmed_at, :type => Time
# field :confirmation_sent_at, :type => Time
# field :unconfirmed_email, :type => String # Only if using reconfirmable
## Lockable
# field :failed_attempts, :type => Integer, :default => 0 # Only if lock strategy is :failed_attempts
# field :unlock_token, :type => String # Only if unlock strategy is :email or :both
# field :locked_at, :type => Time
## Token authenticatable
# field :authentication_token, :type => String
=================================
1:Encryptable:除了内置的Bcrypt(默认),增加支持认证机制
2:Lockable:锁定一定数量的失败尝试登录。通过电子邮件或之后才能解锁
3:validatable:有效性:提供的电子邮件及密码鉴定。它是可选的,可定制的,所以你可以定义自己的代码。
4:Timeoutable:在一特定时期(expires sessions)没有活动。
5:Trackable(跟踪):追踪 登录的次数、时间戳记签字和IP地址
6:Rememberable(记忆):管理产生和清除表示来自用户保存的cookie的标记(token)
7:Registerable(注册):处理用户注册过程,也可以让他们编辑和摧毁他们的帐户。
8:recoverable(重设)重置用户密码并且发送重置指令。
9:Confirmable注册登录认证
10:Omniauthable: adds Omniauth (github.com/intridea/omniauth) support;
11:Database Authenticatable: encrypts and stores a password in the database to validate the authenticity of a user while signing in. The authentication can be done both through POST requests or HTTP Basic Authentication.
12:Token Authenticatable: signs in a user based on an authentication token (also known as “single access token”). The token can be given both through query string or HTTP Basic Authentication.
通过Divse 添加Users
其中User.rb可用属性:12个
:token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable , :omniauthable ,
:database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable,
使用:
1:Gemfile中加入: gem 'devise'
2:建立devise档案: rails g devise:install(自动在routes.rb中加入:devise_for:user)
3:预设定网站网址:在config/environmentents/development.rb与production.rb中加入
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
4:在app/views/layouts/application.html.erb layout中加入:(提示flash信息)
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
5:设定主页,在routes.rb中
root :to => ""
6:产生User model以及Migration
rails g devise user
7:如果需要E-mail验证登录功能,修改user.rb migration将confirmable打开
8:产生view模板
rails g devise:views
9:建立资料表
rake db:migration
使用:
在需要登录的control中加上:before_filter:authenticate_user!
!!
定制登录信息:(注意修改:)
devise默认是email和密码登录,那么,现在用用户名登录!配置如下:
1:添加username字段到User表单
rails generate migration add_username_to_users username:string
rake db:migration
2:修改配置文件:是devise默认用username登录/config/initializers/devise.rb
config.authentication_keys = [ :username ]
config.sign_out_via = :get
3:修改注册页面,app\views\devise\registrations\new.html.erb(类似修改其他devise的视图)
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :username %><br />//////
<%= f.text_field :username %></p>//////
<p><%= f.label :email %><br />
<%= f.email_field :email %></p>
<p><%= f.label :password %><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></p>
<p><%= f.submit "Sign up" %></p>
<% end %>
<%= render :partial => "devise/shared/links" %>
4:重启服务-效果成功
相关推荐
插件拒绝弱口令,使用这是Ruby端口。 如果默认情况下分数低于4,将拒绝用户密码。 它还将电子邮件用作zxcvbn的用户输入,以拒绝包含电子邮件部分的密码(如果在前端使用zxcvbn.js,则也应这样做以获得相同的分数)...
权威设计 这是一个扩展,用于将添加到Rails应用程序。OneTouch支持通用身份验证令牌支持Rails 5 ... $ export AUTHY_API_KEY=YOUR_AUTHY_API_KEY 接下来,将gem添加到您的Gemfile中: gem 'devise'gem 'devise-authy'
This book is for web developers who are getting started with Rails and are looking for authentication solutions, as well as for Rails developers who are looking to extend their implementation of ...
authy-devise, Authy设计插件添加两个因素认证 Authy设计这是一个设计插件扩展,向你的Rails 应用程序添加两个因素验证。要求获取 Authy API密钥: https://www.authy.com/signup演示请参见 https://github.com/aut
Devise - 一个基于Warden的Rails灵活身份验证解决方案
使用devise_openid_authenticatable的devise_example版本_Ruby_
devise-i18n, 设计 gem的翻译 devise-i18n 设计"是一种基于warden的Rails 灵活认证方案"。 国际化( aka i18n ) 是一个"计算机软件适应不同语言。区域差异和目标市场技术要求的方法"。在控制器。模型和其他领域中支持...
这是一个npm软件包,其中包含打包为插件的Ember Simple Auth devise扩展库。 安装 Ember Simple Auth Devise至少需要Ember CLI 0.0.44。 要安装只需运行 ember install ember-cli-simple-auth ember install ember...
devise-jwt是一个扩展,它使用令牌进行用户身份验证。 它遵循原则。 当无法使用Cookie时,此gem只是它们的替代品。 作为cookie,以devise-jwt过期的令牌将强制具有过期时间。 如果您需要用户永不注销,那么使用...
此devise插件尚未在同时启用DatabaseAuthenticatable的情况下进行过测试。 这意味着减少了对DatabaseAuthenticatable的替代,从而允许使用半单点登录方法。 有关示例应用程序的截屏视频,请访问: : 先决条件设计〜...
设计异步 Devise Async提供了一种简单的方法,可将Devise配置为使用ActiveJob异步发送其电子邮件。安装将此行添加到应用程序的Gemfile中: gem 'devise-async' 然后执行: $ bundle或将其自己安装为: $ gem install...
gem 'devise_sms_verifiable' 如果安装了 ,请运行生成器以将配置设置添加到初始化程序: $ rails generate devise_sms_verifiable:install否则使用这个生成器: $ rails generate devise:install生成迁移: $ ...
具有可扩展性,并包含两步备份代码,以作为如何构建插件的示例 贡献 我们欢迎请求请求,错误报告和其他贡献。 我们特别在寻求帮助,以使该gem与Rails 5+完全兼容并压缩所有弃用消息。 示例应用 demo目录中提供了...
devise_ldap_authenticatable, 为LDAP设计模块 设计 LDAP Authenticatable Devise是一种基于LDAP的认证策略,用于基于LDAP的验证框架。如果你正在构建要在组织中使用的应用程序,需要使用 LDAP,这个插件将为你提供...
gear ruby on rails项目 gear仅仅加入devise
语言:English 简单的设计转换器-没有广告-没有不需要的授权-完全开源... 这是一个简单的装置转换器。 它不需要任何许可,也不会干扰您的广告。 支持的货币有:“ EUR”,“ USD”,“ CAD”,“ GBP” ......
为Devise添加UID支持。_Ruby_下载
devise-i18n “是基于Warden的Rails的灵活身份验证解决方案”。 国际化(aka i18n)是“使计算机软件适应目标市场的不同语言,地区差异和技术要求的手段”。 Devise在控制器,模型和其他区域中支持i18n,但不支持...
具有Devise身份验证的模板RailsAngular应用程序_JavaSc