1

我在我的主應用程序中使用Devise進行認證,並且使用「http_basic_authenticate_with」構建了一個API(用於PABX),並且在將before_action :authenticate_user!, except: [:method_name]添加到我的控制器時它運行良好。我這樣做是因爲except: [:method_name]將不需要在Devise上登錄的用戶會話,我只想對這些API控制器使用基本身份驗證。如何避免設計需要引擎內的身份驗證?

config/routes.rb(主要應用)

scope module: 'api' do 
    scope module: 'pabx' do 
     scope '/api/pabx' do 
      get '/accounts/phone_number/:phone_number', to: 'accounts#phone_number' 
     end 
    end 
end 

controllers/api/pabx/accounts_controller.rb(主要應用)

class Api::Pabx::AccountsController < ApplicationController 

    http_basic_authenticate_with name: 'some_name', password: 'some_password' 

    before_action :authenticate_user!, except: [:phone_number] 
    skip_before_filter :verify_authenticity_token 

    # POST api/pabx/accounts/phone_number.json 
    def phone_number 
    ... 
    end 

end 

,當我想要做類似的引擎內部的API東西的問題發生。 當我嘗試訪問路由時,即使在使用before_action :authenticate_user!, except: [:method_name]時,Devise似乎也不允許在未經身份驗證的情況下訪問它。

config/routes.rb(Myengine)

scope module: 'api' do 
    scope '/api' do 
     get '/accounts/phone_number/:phone_number', to: 'accounts#phone_number' 
    end 
end 

controllers/api/pabx/accounts_controller.rb(Myengine)

require_dependency "myengine/application_controller" 

module Myengine 
    class Api::AccountsController < ApplicationController 

    http_basic_authenticate_with name: 'some_name', password: 'some_password' 

    before_action :authenticate_user!, except: [:phone_number] 
    skip_before_filter :verify_authenticity_token 

    # POST api/pabx/accounts/phone_number.json 
    def phone_number 
     ... 
    end 
end 

這是我在主應用程序

controllers/application_controller.rb(主要應用)

class ApplicationController < ActionController::Base  # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 

    # Authentication using Devise (DO NOT REMOVE THIS LINE) 
    before_action :authenticate_user! 
end  
代碼0

config/routes.rb(主要應用)

authenticate :user do 
    mount Myengine::Engine, at: '/myengine' 
end 

當我嘗試訪問該方法在我的引擎,終端顯示我:

Started POST "/myengine/api/accounts/phone_number.json" for ::1 at 2015-09-17 21:15:31 -0300 


Started GET "https://stackoverflow.com/users/sign_in" for ::1 at 2015-09-17 21:15:31 -0300 
Processing by Devise::SessionsController#new as */* 
    Rendered users/shared/_links.html.erb (6.3ms) 
    Rendered users/sessions/new.html.erb within layouts/authentication (29.3ms) 
    Rendered layouts/elements/_flash_messages.html.erb (2.3ms) 
Completed 200 OK in 720ms (Views: 714.4ms | ActiveRecord: 0.0ms) 

(如果我做錯事或某人有) (我使用Devise 3.5.2和Rails 4.2.0)

非常感謝您閱讀我的問題。

回答

1

我解決了這個問題,這很簡單。現在我在我的引擎應用程序控制器中執行身份驗證,而不是通過主應用程序控制器上的路由。

module Myengine 
    class ApplicationController < ActionController::Base 
     before_action :authenticate_user! 
    end 
end 

我也對我的config/routes.rb中取出authenticate :user(主要應用)

mount Myengine::Engine, at: '/myengine' 

這樣,我能使用下面的代碼在任何控制器

before_action :authenticate_user!, except: [:mypublic_method]