【问题标题】:Devise works different in two machines with exactly the same code and configuration设计在具有完全相同代码和配置的两台机器上工作不同
【发布时间】:2019-12-29 18:55:12
【问题描述】:

我正在使用 Ruby 2.6.5 和 Rails 5.2.2。

我对设计行为感到非常困惑。首先,我的项目在我的本地主机中工作。当我尝试将项目放入 VPS 时,错误就开始了。

具有相同 gem 版本和相同操作系统的相同代码在 VPS 和我的 localhost 两台不同机器上的工作方式不同。

VPS:

    devise (4.7.1)
      bcrypt (~> 3.0)
      orm_adapter (~> 0.1)
      railties (>= 4.1.0)
      responders
      warden (~> 1.2.3)
    devise-i18n (1.8.2)
      devise (>= 4.6)

进入 /users/sign_in:

登录后:

还有本地主机:

    devise (4.7.1)
      bcrypt (~> 3.0)
      orm_adapter (~> 0.1)
      railties (>= 4.1.0)
      responders
      warden (~> 1.2.3)
    devise-i18n (1.8.2)
      devise (>= 4.6)

进入 /users/sign_in:

登录后:

代码:

class Admin::HomeController < ApplicationController
  layout 'admin'

  before_action :authenticate_user!
  before_action :set_title

  def index
  end

  private

    def set_title
      @title = 'Home'
    end
end
class Admin::User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable,
         :recoverable, :rememberable, :validatable
end
# frozen_string_literal: true

class Admin::User::SessionsController < ::Devise::SessionsController
  layout 'login'

  # before_action :configure_sign_in_params, only: [:create]

  # GET /resource/sign_in
  # def new
  #   super
  # end

  # POST /resource/sign_in
  # def create
  #   super
  # end

  # DELETE /resource/sign_out
  # def destroy
  #   super
  # end

  # protected

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_sign_in_params
  #   devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
  # end
end
Rails.application.routes.draw do
  devise_for :clients, controllers: {
    sessions: 'clients/sessions',
    confirmations: 'clients/confirmations',
    omniauth: 'clients/omniauth',
    registrations: 'clients/registrations',
    passwords: 'clients/passwords',
    unlocks: 'clients/unlocks'
  }

  #
  # Área do cliente
  #
  devise_scope :client do
    get 'area-do-cliente/entrar', to: 'clients/sessions#new'
    get 'area-do-cliente/meus-dados', to: 'clients/registrations#edit'
  end

  get 'area-do-cliente/pedidos', to: 'clients/orders#index', as: 'client_orders'

  devise_for :users,
             class_name: "Admin::User",
             controllers: {
    sessions: 'admin/user/sessions',
    confirmations: 'admin/user/confirmations',
    omniauth: 'admin/user/omniauth',
    registrations: 'admin/user/registrations',
    passwords: 'admin/user/passwords',
    unlocks: 'admin/user/unlocks'
  }

  namespace :admin do
    root 'home#index'

# Some other routes ...

我解决了 VPS 真实性问题。现在它不会抛出错误,但是当我登录时它会重定向到 错误路径(不是管理员根目录),并使用 current_user nil

Devise 真实性问题有很多问题,他们说要把它放在控制器中:

protect_from_forgery

我做到了,但现在登录后current_usernil

这两种环境的不同之处在于,在 VPS 中,我使用了 Nginx 配置:

server {
    listen 80;
    listen [::]:80;

    server_name vps12441.publiccloud.com.br;

    location / {
        proxy_pass http://127.0.0.1:3000;
    }
}

为什么要设计在两台不同的机器上使用完全相同的代码(和应用程序配置)获得不同的行为?

我怎样才能让它工作?

【问题讨论】:

  • 欢迎来到 SO!谨慎使用强调 您可以将内容以粗体或斜体表示,但仅在真正需要时才这样做。强调用于引起对某事的额外注意。如果几乎所有事情都需要额外的关注,那么没有什么会得到额外的关注。根据经验,在用粗体或斜体写一些东西之前,问问自己:如果我只能在这篇文章中用粗体或斜体写一件事,会是这个吗?如果答案是否定的,那就不要。 meta.stackoverflow.com/q/303219/128421

标签: ruby-on-rails ruby authentication devise


【解决方案1】:

听起来您使用的是 Rails 6?默认情况下,Rails 6 在生产模式下尝试验证真实性令牌。我猜想在 VPS 上,您的应用程序正在生产模式下运行,并试图保护您免受 CSRF 攻击,而在开发模式下它不会这样做,这就是它在 localhost 上运行的方式。您可以通过几种方式确认我是否正确,但如果我将skip_before_action :verify_authenticity_token 添加到您的ApplicationController 应该可以解决您的VPS 解决方案中的问题。

在 VPS 上进行开发的另一种选择是在开发模式下启动 Rails 服务器:RAILS_ENV=development bundle exec rails s

【讨论】:

  • 我按照您所说的将 skip_before_action :verify_authenticity_token 添加到我的 ApplicationController 中。它有效,但现在我得出结论:这不是设计问题,而是 Rails 问题。我在设计控制器中遇到了同样的问题。并非全部,现在在某些控制器中我收到此错误:在 process_action 回调之前:verify_authenticity_token 尚未定义我的 RAILS_ENV 正在所有机器上开发
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
  • 2016-08-21
相关资源
最近更新 更多