Devise允许对单个控制器动作进行令牌验证 [英] Devise allow token authentication on single controller action

查看:92
本文介绍了Devise允许对单个控制器动作进行令牌验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个web应用程序,它为所有控制器使用devise数据库身份验证,但是我想要一个控制器操作,其中也使用令牌进行身份验证。我可以使用devise吗?

解决方案

设计策略有一个有效?方法被调用以确定是否应该启用该策略。这使您可以根据每个控制器/操作来控制可用的认证策略。



将其放在初始化器中:

  require'devise / strategies / base'
require'devise / strategies / token_authenticatable'
module Devise
module Strategies
class TokenAuthenticatable< ;验证
def有效?
超级&& params [:controller] ==你的控制器& amp;& params [:action] ==your action
end
end
end
end

让我知道如果它有效。


I have an webapplication that uses devise database authentication for all the controllers, however I want to have one controller action where authentication is also done using a token. Can i use devise for this?

解决方案

Devise strategies have a valid? method that is called to determine if the strategy should be enabled. This allows you to control available auth strategies on a per controller/action basis.

Put this in an initializer:

require 'devise/strategies/base'
require 'devise/strategies/token_authenticatable'
module Devise
  module Strategies
    class TokenAuthenticatable < Authenticatable
      def valid?
        super && params[:controller] == "your controller" && params[:action] == "your action"
      end
    end
  end
end

let me know if it works.

这篇关于Devise允许对单个控制器动作进行令牌验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆