Ruby on Rails-排除机架中间件-有可能吗? [英] Ruby on rails - rack middleware exclude - is it possible?

查看:85
本文介绍了Ruby on Rails-排除机架中间件-有可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试机架中间件,以在单独的层中进行身份验证. 问题是,所有请求都通过此层.我不希望CSS,JavaScript之类的资产请求通过身份验证中间件. 我也不想退出流程.

I am trying a rack middleware, to do authentication in a separate layer. The problem is, all request goes through this layer. I don't want the asset request like css, javascript to go through authentication middleware., I also don't want logout flow to go through this.,

在application.rb

In application.rb

config.middleware.use AuthClient::MyFilterClass

我期望类似

config.middleware.use AuthClient::MyFilterClass, :exclude => [:logout,'*/assets/*']

是否可以从中间件中排除自定义路径/操作?

Is there any way to exclude custom path / actions from middleware ?

推荐答案

机架中间件形成向上和向下的堆栈,如以下示例所示:

Rack middlewares form a up and down stack, like the following example:

这意味着无论您提出什么请求,它都会通过所有中间件传递.
因此,您不能排除这样的中间件.

This means that whatever the request your making, it will pass through all middlewares no matter what.
So you can't exclude middlewares like this.

但是,您可以做的是将自己的中间件注入到堆栈中,该中间件将检查请求路径并调用或不调用其他一些中间件.

What you can do though, is inject your own middleware to the stack, which will check the request path and call some other middlewares or not.

类似这样的东西:

class MyMiddleware
  def initialize(app)
    @app = app
  end

  def call(env)
    if env['REQUEST_PATH'].match(/^\/assets\//)
      middleware = AuthClient::MyFilterClass.new(@app)
      env = middleware.call(env)
    end

    @app.call(env)
  end
end

这将根据请求路径有条件地调用AuthClient::MyFilterClass中间件.

This will call the AuthClient::MyFilterClass middleware conditionnally depending of the request path.

这篇关于Ruby on Rails-排除机架中间件-有可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆