devise中如何使用auth_token认证 与 RubyChina api认证的区别
来源:互联网 发布:数据迁移测试用例 编辑:程序博客网 时间:2024/04/29 08:20
1. 修改配置文件 config/initializers/devise.rb
config.token_authentication_key = :auth_token
2. 修改controller action
class Users::SessionsController < Devise::SessionsController
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
respond_to do |format|
format.html do
respond_with resource, :location => redirect_location(resource_name, resource)
end
format.json do
render :json => { :response => 'ok', :auth_token => current_user.authentication_token }.to_json, :status => :ok
end
end
end
end
3, 可以使用如下的命令测试
curl -X POST 'http://localhost:3000/users/sign_in.json' -d 'user[email]=example@example.com&user[password]=password'
-> {"response":"ok","auth_token":"ABCDE0123456789"}
curl -L 'http://localhost:3000/profile?auth_token=ABCDE0123456789'
-> got page that I wanted that needs authentication
devise 提供的auth token 方式不是很合适,需要修改devise.rb配置文件,与web认证冲突
还是RubyChina实现的比较好一点
Ruby-China的代码中如何实现tokenauthentication登录
1, 登录的时候生成private_token代码
# 用户密钥,用于客户端验证
field :private_token
# 重新生成 Private Token
def update_private_token
random_key = "#{SecureRandom.hex(10)}:#{self.id}"
self.update_attribute(:private_token, random_key)
end
2, 在客户端登录的时候,返回 private_token
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
respond_to do |format|
format.html { redirect_to after_sign_in_path_for(resource) }
format.json { render :status => '201', :json => resource.as_json(:only => [:login, :email, :private_token]) }
end
end
3, 可以使用如下命令测试
curl -X POST 'http://ruby-china.org/account/sign_in.json' -d "user[login]=xxxx&user[password]=xxxxxx"
4, 如何在下次接口定义中使用?
# file path: lib/api.rb
#
# Post a new topic
# require authentication
# params:
# title
# body
# node_id
post do
authenticate!
@topic = current_user.topics.new(:title => params[:title], :body => params[:body])
@topic.node_id = params[:node_id]
@topic.save!
#TODO error handling
end
5, authenticate!的源代码【在lib/api/helper.rb文件中】
# user helpers
def current_user
@current_user ||= User.where(:private_token => params[:token] || '').first
end
def authenticate!
error!({ "error" => "401 Unauthorized" }, 401) unless current_user
end
RubyChina没有使用Devise默认的认证码,定义了一套新的认证机制, 结合 Grape 做接口,与 web层
config.token_authentication_key = :auth_token
2. 修改controller action
class Users::SessionsController < Devise::SessionsController
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
respond_to do |format|
format.html do
respond_with resource, :location => redirect_location(resource_name, resource)
end
format.json do
render :json => { :response => 'ok', :auth_token => current_user.authentication_token }.to_json, :status => :ok
end
end
end
end
3, 可以使用如下的命令测试
curl -X POST 'http://localhost:3000/users/sign_in.json' -d 'user[email]=example@example.com&user[password]=password'
-> {"response":"ok","auth_token":"ABCDE0123456789"}
curl -L 'http://localhost:3000/profile?auth_token=ABCDE0123456789'
-> got page that I wanted that needs authentication
devise 提供的auth token 方式不是很合适,需要修改devise.rb配置文件,与web认证冲突
还是RubyChina实现的比较好一点
Ruby-China的代码中如何实现tokenauthentication登录
1, 登录的时候生成private_token代码
# 用户密钥,用于客户端验证
field :private_token
# 重新生成 Private Token
def update_private_token
random_key = "#{SecureRandom.hex(10)}:#{self.id}"
self.update_attribute(:private_token, random_key)
end
2, 在客户端登录的时候,返回 private_token
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
respond_to do |format|
format.html { redirect_to after_sign_in_path_for(resource) }
format.json { render :status => '201', :json => resource.as_json(:only => [:login, :email, :private_token]) }
end
end
3, 可以使用如下命令测试
curl -X POST 'http://ruby-china.org/account/sign_in.json' -d "user[login]=xxxx&user[password]=xxxxxx"
4, 如何在下次接口定义中使用?
# file path: lib/api.rb
#
# Post a new topic
# require authentication
# params:
# title
# body
# node_id
post do
authenticate!
@topic = current_user.topics.new(:title => params[:title], :body => params[:body])
@topic.node_id = params[:node_id]
@topic.save!
#TODO error handling
end
5, authenticate!的源代码【在lib/api/helper.rb文件中】
# user helpers
def current_user
@current_user ||= User.where(:private_token => params[:token] || '').first
end
def authenticate!
error!({ "error" => "401 Unauthorized" }, 401) unless current_user
end
RubyChina没有使用Devise默认的认证码,定义了一套新的认证机制, 结合 Grape 做接口,与 web层
- devise中如何使用auth_token认证 与 RubyChina api认证的区别
- 关于微软企业库2.0 中认证与1.0的区别
- 可信网站认证与诚信网站认证的区别?
- 华付实名认证 传统实名认证与华付实名认证的区别
- rbac与auth权限认证的区别
- Web API与JWT认证
- Laravel 5 中使用 JWT(Json Web Token) 实现基于API的用户认证
- rails中使用devise
- 如何使用HttpClient认证机制
- devise的使用
- Apple ID双重认证是什么?双重认证与两步验证的区别
- 什么是MFI认证?WWI认证是什么?苹果公司的MFI认证和WWI认证区别
- 在C#中如何与Windows认证的SQL Server进行连接?
- Android 如何开启与关闭adb 的认证机制(google adb secure) (adb RSA 指纹认证)
- [FAQ10781]如何开启与关闭adb 的认证机制(google adb secure) (adb RSA 指纹认证)
- 单向SSL与双向SSL认证的区别
- 如何理解CPU卡内部认证与外部认证
- 如何理解CPU卡内部认证与外部认证
- C语言操作符优先级
- java访问修饰符
- 关于VS2010调试时启动速度慢的解决方法
- oracle中的数据字典
- java 通过反射获取泛型的类型
- devise中如何使用auth_token认证 与 RubyChina api认证的区别
- ubuntu svn 服务器
- 使用com object 控制outlook
- 用devc++制作DLL动态链接库的方法
- 二维码
- 利用XOR运算进行简单加密(C# 3.0完全参考手册)
- 化身CIO 项目经理需要修炼的九件事
- Mac-OSX下安装Git和Mercurial,
- 由于本机的限制,该操作已被取消。请与系统管理员联系。