Rails使用authenticate_or_request_with_http_basic完成401未授权代码

时间:2018-06-11 14:55:07

标签: ruby-on-rails devise http-status-code-401

在我的Rails 4.2应用程序中,我使用的是Devise 3.5.10。管理员登录后,我试图用第二个密码保护仅使用authenticate_or_request_with_http_basic访问特定视图的管理员用户。

这是控制器:

class ScreensController < ApplicationController
   skip_before_action :authenticate_user!

   layout 'chat'

   def chat
     if current_user.admin?
       if params["full_chat"] == "true"
         authenticate_or_request_with_http_basic('Administration') do |username, password|
           username == "admin" && password == "your_password"
         end
      end
    end
  end
 end

所以在ActiveAdmin视图中我有这个link_to:

link_to(I18n.t('admin.full_conversation'), root_url+'chat?full_chat=true#/conversations/'+conversation.id.to_s)

以下是路线:

get  '/chat' , to: 'screens#chat'

它在本地按预期工作,如果它们是正确的,在显示视图之前显示一个模态窗口询问用户和密码,但是一旦将它部署到服务器,我在loggs中得到一个401代码:

Completed 401 Unauthorized in 11ms (ActiveRecord: 1.2ms)

服务器卡在循环中。 任何关于为什么行为在本地不同于服务器的想法都将非常感激。

1 个答案:

答案 0 :(得分:0)

首先,您可以使用正确的路径清理视图,该路径类似于screens_chat_path,其中包含params全屏和对话。

get ':chat', to: 'screens#chat', as: 'chat'

查看

link_to(I18n.t('admin.full_conversation'), screen_chat_path(full_chat: true, path: /conversations/'+conversation.id.to_s))

接下来,似乎这个逻辑存在一个问题:

md5_password = Digest::MD5.hexdigest(password)
       username == "admin" && md5_password == 
User.where(role:2).first.randword

如果在这里随机输入密码怎么办?它似乎回归401,因为你正在创建的摘要和随机词不相等。

您可以构建一个主密钥并将其保存为环境变量,并说ENV['MASTER_KEY'] == password逻辑由您决定。

相关问题