【问题标题】:Devise and Devise token auth gem routing errors设计和设计令牌身份验证 gem 路由错误
【发布时间】:2019-07-24 02:51:28
【问题描述】:

我有一个带有 API 的 Rails 应用程序。我可以使用来自gem devise_authaccess-token 拨打电话和进行身份验证。

我现在想为 Web 请求添加身份验证。我决定创建两个application_controllers 来处理webapi 这两种类型的请求。

两个控制器如下所示:

class ApplicationController < ActionController::Base
  before_action :authenticate_user!
  protect_from_forgery with: :exception

end

对于api

class ApiApplicationController < ActionController::Base
  skip_before_action :verify_authenticity_token
  include DeviseTokenAuth::Concerns::SetUserByToken
end

路线如下所示:

Rails.application.routes.draw do
  devise_for :users, as: 'web'

  mount_devise_token_auth_for 'User', at: 'auth'
  root to: 'pages#home'
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  namespace :api, defaults: { format: :json } do
    namespace :v1 do
      resources :trainings, only: [ :index, :show, :create ]
      post 'import', to: "trainings#import"
    end
  end

  namespace :admin do
    resources :trainings, only: [:index]
    resources :users, only: [:index, :show]
  end
end

我刚刚添加了web 前缀以避免与相同的rails_path 发生冲突。

我要访问的控制器是admin/users,它继承自ApplicationController

class Admin::UsersController < ApplicationController
  before_action :authenticate_user!

当我在浏览器中输入时:

http://localhost:3000/admin/users

我希望看到正常的设计行为,即在网络中看到登录表单。

不幸的是,请求被 AuthTokenController 接收到:

Started GET "/auth/sign_in" for ::1 at 2019-07-23 15:37:46 +0200
Processing by DeviseTokenAuth::SessionsController#new as HTML
Filter chain halted as :authenticate_user! rendered or redirected
Completed 401 Unauthorized in 1ms (Views: 0.3ms | ActiveRecord: 0.0ms)

我将控制器用于 web 和 api,以便我可以使用 protect_from_forgery 方法等。

但我不知道如何在 Routes 中告诉 Rails,每当我发出 Web 请求时,它都会通过正确的控制器进行身份验证。

总而言之,我点击了以下网址:

http://localhost:3000/admin/users

对应这个控制器:

admin_users GET      /admin/users(.:format)  admin/users#index

这个控制器继承自

class Admin::UsersController < ApplicationController
  before_action :authenticate_user!

而这个ApplicationController 使用:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :authenticate_user!
end

在任何时候我都不会告诉这个请求关于DeviseAuthToken 的任何事情。我不知道为什么它会接受请求。

我的用户模型如下:

# frozen_string_literal: true

class User < ActiveRecord::Base
  has_many :trainings

  extend Devise::Models
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  include DeviseTokenAuth::Concerns::User
end

【问题讨论】:

  • 你能解决这个问题吗?我遇到了完全相同的问题。

标签: ruby-on-rails devise


【解决方案1】:

来自设计documentation

对于 Rails 5,请注意,protect_from_forgery 不再附加到 before_action 链中,因此如果您在 protect_from_forgery 之前设置了 authenticate_user,您的请求将导致“无法验证 CSRF 令牌真实性”。要解决此问题,请更改调用它们的顺序,或使用protect_from_forgery prepend: true。

【讨论】:

  • 好点,但仍然是同样的错误。 TokenAuth 正在控制而不是正常设计
猜你喜欢
  • 2014-02-04
  • 1970-01-01
  • 1970-01-01
  • 2016-03-22
  • 1970-01-01
  • 2015-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多