Ruby on Rails中,如何实现路由前进行身份验证操作?

2026-04-10 07:181阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计545个文字,预计阅读时间需要3分钟。

Ruby on Rails中,如何实现路由前进行身份验证操作?

什么。我正在尝试在我的Rails API上实现基于令牌的基本身份验证。它适用于现有线路,但这里有一个问题:当未经身份验证的用户访问不存在的路由时,它会显示404 - 找不到页面,而不是‘未授权访问’。

什么.

我正在尝试在我的Rails API上实现基于令牌的基本身份验证.它适用于现有路线,但这里有一个问题:

当未经身份验证的用户访问不存在的路由时,它会显示404 – 未找到页面,而不是401 – 未授权.在验证路由之前,如何让Rails检查身份验证?

这是我的application_controller.rb:

class Api::V1::ApiController < ApplicationController # Main controller inherited by all API controllers # For APIs, you may want to use :null_session instead. protect_from_forgery with: :null_session # Enforce site-wide authentication before_action :authenticate def authenticate authenticate_token || render_unauthorized end def authenticate_token # Can we find a user with the authentication token used? authenticate_with_api.stripe.com/this/route/doesnt/exist HTTP/1.1 401 Unauthorized Server: nginx Date: Fri, 08 Aug 2014 21:21:49 GMT Content-Type: application/json;charset=utf-8 Www-Authenticate: Basic realm="Stripe" Cache-Control: no-cache, no-store Stripe-Version: 2014-08-04 Strict-Transport-Security: max-age=31556926; includeSubDomains

或者,如果您希望/需要将其保存在ruby-land中,您可以使用机架中间件,在rails加载您的路线之前进行身份验证.你可以尝试github.com/iain/rack-token_auth

$bin/rake middleware ...snip a bunch of middleware... use Rack::TokenAuth run Dummy::App::Application.routes

更新:使用HTTP基本身份验证

在这种情况下,您根本不需要另一个库,因为Rack通过Rack :: Auth :: Basic中间件内置了对http basic auth的支持,例如:

config.middleware.use Rack::Auth::Basic do |username, password| username == "bob" && password == "secret" end

更新:我可以将身份验证应用于API命名空间吗?

Ruby on Rails中,如何实现路由前进行身份验证操作?

这取决于.由于机架中间件在rails加载路由,控制器等之前运行,因此它不知道您的命名空间.尽管如此,如果您的命名空间控制器也在URL级别进行了命名空间,例如在/ api下,您可以检查request.path是否应用身份验证.

您可以使用此行为创建一个新的机架middlware来装饰Rack :: Auth :: Basic,例如:

class MyAPIAuth < Rack::Auth::Basic def call(env) request = Rack::Request.new(env) if request.path =~ /\A\/api\/?/ super else @app.call(env) end end end # change your configuration to use your middleware config.middleware.use MyAPIAuth do |username, password| username == "bob" && password == "secret" end

本文共计545个文字,预计阅读时间需要3分钟。

Ruby on Rails中,如何实现路由前进行身份验证操作?

什么。我正在尝试在我的Rails API上实现基于令牌的基本身份验证。它适用于现有线路,但这里有一个问题:当未经身份验证的用户访问不存在的路由时,它会显示404 - 找不到页面,而不是‘未授权访问’。

什么.

我正在尝试在我的Rails API上实现基于令牌的基本身份验证.它适用于现有路线,但这里有一个问题:

当未经身份验证的用户访问不存在的路由时,它会显示404 – 未找到页面,而不是401 – 未授权.在验证路由之前,如何让Rails检查身份验证?

这是我的application_controller.rb:

class Api::V1::ApiController < ApplicationController # Main controller inherited by all API controllers # For APIs, you may want to use :null_session instead. protect_from_forgery with: :null_session # Enforce site-wide authentication before_action :authenticate def authenticate authenticate_token || render_unauthorized end def authenticate_token # Can we find a user with the authentication token used? authenticate_with_api.stripe.com/this/route/doesnt/exist HTTP/1.1 401 Unauthorized Server: nginx Date: Fri, 08 Aug 2014 21:21:49 GMT Content-Type: application/json;charset=utf-8 Www-Authenticate: Basic realm="Stripe" Cache-Control: no-cache, no-store Stripe-Version: 2014-08-04 Strict-Transport-Security: max-age=31556926; includeSubDomains

或者,如果您希望/需要将其保存在ruby-land中,您可以使用机架中间件,在rails加载您的路线之前进行身份验证.你可以尝试github.com/iain/rack-token_auth

$bin/rake middleware ...snip a bunch of middleware... use Rack::TokenAuth run Dummy::App::Application.routes

更新:使用HTTP基本身份验证

在这种情况下,您根本不需要另一个库,因为Rack通过Rack :: Auth :: Basic中间件内置了对http basic auth的支持,例如:

config.middleware.use Rack::Auth::Basic do |username, password| username == "bob" && password == "secret" end

更新:我可以将身份验证应用于API命名空间吗?

Ruby on Rails中,如何实现路由前进行身份验证操作?

这取决于.由于机架中间件在rails加载路由,控制器等之前运行,因此它不知道您的命名空间.尽管如此,如果您的命名空间控制器也在URL级别进行了命名空间,例如在/ api下,您可以检查request.path是否应用身份验证.

您可以使用此行为创建一个新的机架middlware来装饰Rack :: Auth :: Basic,例如:

class MyAPIAuth < Rack::Auth::Basic def call(env) request = Rack::Request.new(env) if request.path =~ /\A\/api\/?/ super else @app.call(env) end end end # change your configuration to use your middleware config.middleware.use MyAPIAuth do |username, password| username == "bob" && password == "secret" end