Jen*_*nny 38 ruby-on-rails token devise
所以,我正在尝试使用带有Devise的令牌(版本1.0.3和Rails 2.3.8)让用户登录,但我不完全确定从哪里开始.
http://zyphdesignco.com/blog/simple-auth-token-example-with-devise
上面的教程帮助我打开了令牌功能,并展示了如何生成(或删除)令牌......但是令牌的整个POINT是用它们来授权用户,对吗?
当我在控制台中查看用户时,我可以说user.authentication_token,并得到一些回复:"Qm1ne93n_XkgmQTvxDmm",这一切都很好......但是我从哪里开始呢?
我尝试使用以下命令行命令命中sign_in root:
curl -d"authentication_token = Qm1ne93n_XkgmQTvxDmm"localhost:3000/users/sign_in
并且肯定没有成功登录.
在会话控制器中,我看到他们调用:
验证(RESOURCE_NAME)
我在假设的是模块中的某个地方:
包括Devise :: Controllers :: InternalHelpers
包括在内,但我不知道在哪里寻找(它绝对不在源的控制器文件夹中).如果我能看看身份验证是如何工作的,我可以看看它是否甚至可以看到令牌......
设计是否允许您实际使用令牌登录,或者它是否只有生成它们的框架?如果它确实让你登录他们...你怎么做?你能不能使用curl(即它是否必须在浏览器中?如果是这样,我会自己推出自己的解决方案,我需要非浏览器支持.).如果没有,我该怎么做自己的?
Ant*_*zzo 36
我的理解是,您可以使用令牌登录或打击需要身份验证的任意页面,即使使用cURL也是如此.如果你查看config/initializers/devise.rb,应该有一行说:
config.token_authentication_key = :auth_token
Run Code Online (Sandbox Code Playgroud)
无论名称token_authentication_key是什么,都应该与您在请求中作为查询或表单参数放置的内容相匹配.您authentication_token在示例中使用过,不确定是否更改了devise.rb以匹配.
如果你想弄清楚内部的工作方式,我会尝试git clone git://github.com/plataformatec/devise.git搜索你需要澄清的方法.
以下是一些示例cURL请求(我创建了一个自定义的Users :: SessionsController,它扩展了Devise :: SessionsController并覆盖了create方法来处理JSON.)
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
Run Code Online (Sandbox Code Playgroud)
然后是我给出的cURL请求:
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
Run Code Online (Sandbox Code Playgroud)
看到这篇文章:http://www.hyperionreactor.net/blog/token-based-authentication-rails-3-and-rails-2
基本上你只需要将令牌附加到你的请求,你就会自动进行身份验证,即localhost:3000/posts.xml?auth_token = the_token
| 归档时间: |
|
| 查看次数: |
19445 次 |
| 最近记录: |