【问题标题】:How to authenticate Token with devise如何使用设计验证令牌
【发布时间】:2012-03-10 20:36:58
【问题描述】:

我想使用 devise 的 token_authenticable 助手来针对系统对用户进行身份验证。

我发现了一些旧文档,其中使用了名为 valid_authentication_token?(...) 的方法,但在较新的设计版本中找不到相同的方法。

那么验证用户的正确方法是什么? 我应该为具有命名令牌的用户请求模型并检查电子邮件地址是否匹配?

非常感谢您的帮助。

帕斯卡涡轮

【问题讨论】:

    标签: ruby-on-rails authentication devise token


    【解决方案1】:

    如果你添加

    t.token_authenticatable
    

    给你的用户 ActionRecord,并添加

    devise  :token_authenticatable
    

    到你的用户模型

    并在 config/initializer/devise 中指定哪个参数是您的令牌密钥,如下所示:

    config.token_authentication_key = :auth_token
    

    然后是使用

    的控制器
    before_filter :authenticate_user!  # Tell devise to use :user map 
    

    进行身份验证。在 authenticate_user! 之后,可以使用个别方法进行测试

    user_signed_in?
    

    将通过登录会话或通过查询字符串传递或使用 HTTP 基本身份验证传递的设计授权令牌来授权用户。详情请参阅设计助手code

    【讨论】:

    • 您确定“load_and_authorize_resource”是设计的一部分吗?我找不到任何关于此的文档 - 但有很多关于“cancan”的条目
    • 抱歉,把我的设计和 cancan 调用搞混了。我们两者都用。我已经用 before_filter authenticate_user 更新了我上面的答案!来自设计。
    • @SteveWilhelm 你写了“..设计在查询字符串上传递的授权令牌......”。你能告诉我当我向 JSON API 发送 POST 请求时我会怎么做吗?
    • 只需将身份验证令牌作为 URL 参数传递。我相信它默认为 auth_token 并且可以使用 config.token_authentication_key 进行配置
    猜你喜欢
    • 2015-02-11
    • 2011-11-06
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    相关资源
    最近更新 更多