我在我的rails 3应用程序中使用rack-throttle
作为限速引擎。我已经根据Rack::Throttle::Interval
创建了自己的类来定义自定义速率限制逻辑。我正在检查请求是否是针对确切的控制器和确切的操作。如果我请GET
请求,这工作正常。但是,如果我发送POST
请求我得到一些问题。在rails应用程序中限制POST请求
class CustomLimiter < Rack::Throttle::Interval
def allowed?(request)
path_info = Rails.application.routes.recognize_path request.url rescue path_info = {}
if path_info[:controller] == "some_controller" and path_info[:action] == "some_action"
super
else
true
end
end
end
这里是我的控制器操作
def question
#user is redirected here
end
def check_answer
#some logic to check answer
redirect_to question_path
end
我的路线
get "questions" => "application#question", :as => "question"
post "check_answer" => "application#check_answer", :as => "check_answer"
编辑:
的问题是,POST
请求来应用,因此该方法allowed?
被称为。但是,当我拨打Rails.application.routes.recognize_path
我得到一个Route set not finalized
例外。我怎样才能防止用户与rack-throttle
帮助中间件在application.rb
class Application < Rails::Application
#Set up rate limiting
config.require "ip_limiter"
config.require "ip_user_agent_limiter"
config.middleware.use IpLimiter, :min => 0.2
config.middleware.use IpUserAgentLimiter, :min => 2
end
两个IpLimiter
和IpUserAgentLimiter
增加都源于确切控制器的动作准确发送大量POST请求自定义限制器
你如何插入机架::油门::区间在您的应用程序? – shingara 2012-03-12 08:55:21
@shingara 它包含在'application.rb'中 – RomanKapitonov 2012-03-12 10:40:17
允许?方法是否在POST方法中调用?你可以在你的应用程序中添加这个中间件吗? – shingara 2012-03-12 10:48:44