如何解决我无法将我的新中间件放置在机架堆栈中我想放置的位置
我编写了一个名为 RescueAuthTokenFromOmniauth
的新中间件,它可以挽救由 OmniAuth::Strategies::GoogleOauth2
或 OmniAuth::Strategies::Facebook
创建的异常。理想情况下,我想将 RescueAuthTokenFromOmniauth
放在堆栈中的 Omniauth 中间件之前。
我可以使用
将RescueAuthTokenFromOmniauth
添加到堆栈中
# application.rb
config.middleware.use RescueAuthTokenFromOmniauth
但是当我尝试做,例如,
# application.rb
config.middleware.insert_before(OmniAuth::Strategies::GoogleOauth2,RescueAuthTokenFromOmniauth)
运行 rails middleware
时出现以下错误:
No such middleware to insert before: OmniAuth::Strategies::GoogleOauth2
Omniauth::Strategies
中间件由 gem omniauth-google-oauth2 和 omniauth-facebook 添加。
有没有办法在 OmniAuth::Strategies::GoogleOauth2
中引用 application.rb
以便我可以将我的新中间件放在它之前?
我使用的是 Rails (6.0.3.6)。
这是运行 rails 中间件的结果。它已经通过使用 RescueAuthTokenFromOmniauth
config.middleware.use RescueAuthTokenFromOmniauth
use Webpacker::DevServerProxy
use Actiondispatch::HostAuthorization
use Rack::Sendfile
use Actiondispatch::Static
use Actiondispatch::Executor
use Rack::Rewrite
use ActiveSupport::Cache::Strategy::LocalCache::Middleware
use Rack::Runtime
use Rack::Methodoverride
use Actiondispatch::RequestId
use RequestStore::Middleware
use Actiondispatch::RemoteIp
use Sprockets::Rails::QuietAssets
use Rails::Rack::Logger
use Actiondispatch::ShowExceptions
use WebConsole::Middleware
use Actiondispatch::DebugExceptions
use BetterErrors::Middleware
use Rollbar::Middleware::Rails::RollbarMiddleware
use Actiondispatch::ActionableExceptions
use Actiondispatch::Reloader
use Actiondispatch::Callbacks
use ActiveRecord::Migration::CheckPending
use Actiondispatch::Cookies
use Actiondispatch::Session::CookieStore
use Actiondispatch::Flash
use Actiondispatch::ContentSecurityPolicy::Middleware
use Rack::Head
use Rack::ConditionalGet
use Rack::ETag
use Rack::TempfileReaper
use Warden::Manager
use RescueAuthTokenFromOmniauth
use Rack::Attack
use Bullet::Rack
use OmniAuth::Strategies::GoogleOauth2
use OmniAuth::Strategies::Facebook
run SaverLife::Application.routes
这些是application.rb
中关于中间件的所有设置
require_relative '../lib/rescue_auth_token_from_omniauth'
...
config.middleware.use RescueAuthTokenFromOmniauth
我在 lib/rescue_auth_token_from_omniauth.rb
中定义的新中间件
class RescueAuthTokenFromOmniauth
def initialize(app)
@app = app
end
def call(env)
begin
@app.call env
rescue ActionController::InvalidAuthenticityToken
[302,{'Location' => "/users/sign_up",'Content-Type' => 'text/html'},['Invalid Authenticity Token']]
end
end
end
我在 devise.rb
中添加了 OmniAuth 中间件
# ==> OmniAuth
# Add a new OmniAuth provider. Check the wiki for more information on setting
# up on your models and hooks.
# config.omniauth :github,'APP_ID','APP_SECRET',scope: 'user,public_repo'
config.omniauth :google_oauth2,Rails.application.credentials.dig(Rails.env.to_sym,:google_client_id),:google_client_secret)
config.omniauth :facebook,:facebook_key),:facebook_secret),scope: "email",info_fields: 'name,email,first_name,last_name'
这就是我的 OmniAuth 初始化程序的样子
# omniauth.rb
OmniAuth.config.logger = Rails.logger
OmniAuth.config.allowed_request_methods = [:post]
注意:我现在知道 RescueAuthTokenFromOmniauth
在 Omniauth::Strategies
中间件之前,所以我的功能有效。但我想用 insert_before
显式设置它,以便很明显它应该放在 Omniauth::Strategies
中间件之前。
解决方法
您可以在 omniauth 初始化时执行此操作
return
注意,如果使用上面的代码,你不应该在 application.rb 上添加你的中间件,否则它会重复。
,我同意 Lam Phan 的方法。如果您确实想将初始值设定项保存在单独的初始值设定项文件中,请确保使用 require_relative './omniauth.rb'
之类的内容引用依赖项初始值设定项,否则它将对文件名的字母顺序敏感。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。