【问题标题】:how to implement second level authentication in rails?如何在 Rails 中实现二级身份验证?
【发布时间】:2017-01-03 09:39:35
【问题描述】:

我的application 中已经设置了devise authentication。现在我想在rails 中实现second level authentication。我想hard codeemailpasswordcontrollers 中编码。我想将此emailpassworduser 提供的emailpassword 进行比较。而不是保存在database 中并选择second level authentication。因为这5 到6 个links 在任何时候都只会被person 中的一个person 使用。我想在我的控制器应用程序中为 5 到 6 actions 实现此功能。我该如何实施?我检查了一个 gem gem 'devise-authy',但它似乎向移动设备发送了password 以进行otp 身份验证。我不想要 otp 身份验证。我想要使​​用与设计电子邮件相同的电子邮件进行二级身份验证,但密码应该与设计原始密码不同。(就像在设计表中,我想多取一列 second_level_password。)这种类型的有没有gem要求或这是manually 编码的。如果是manually 编码,那我该怎么做呢?

【问题讨论】:

    标签: ruby-on-rails authentication devise


    【解决方案1】:

    您可以在 rails 控制器中使用 before_action。

    在您的控制器中添加以下代码行。

    before_action :second_level_access
    
    private
    
    def second_level_access
      #You can use your logic to get password
      unless current_user.second_level_password == params[:second_level_password]
       # signout to user because user is already logged in with devise.
       redirect_to session_path
      end
    end
    

    编辑

    你可以使用

    attr_accessor_with_default :second_level_logged_in, false

    将以下行添加到application controller:

    before_action :logged_in_with_second_level, if: 'current_user and !second_level_logged_in'
    
    def logged_in_with_second_level
      redirect_to 'second_level_logged_in_form_page'
    end  
    

    create action 中的second_level_logged_in

    添加代码current_user.second_level_logged_in=true

    【讨论】:

    • 感谢您的回答。但是,如果用户尚未使用 second_level_password 登录,则单击这些链接后,我想提供与设计相同的 second_level_authentication 表单。如果他已经使用第二个密码登录,则不会出现第二级表单。如果他从第二级注销,我只想注销第二级身份验证,第一级身份验证或会话应该保持不变。我该怎么做?
    • 您可以使用 attr_accessor_with_default :second_level_logged_in, false 将以下行添加到应用程序控制器: before_action :logged_in_with_second_level, if: 'current_user and !second_level_logged_in' def logged_in_with_second_level redirect_to 'second_level_logged_in_form_page' end in create action of second_level_logged_in assign current_user second_level_logged_in=true
    • @John,我更新了答案中的代码,请看一下
    • 谢谢。我会试试的
    猜你喜欢
    • 1970-01-01
    • 2018-09-29
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多