Rails Rack Attack Gem Throttle

I don't know why I can not use rack-attack gem Here what I did

Gemfile

gem 'rack-attack' 

I ve installed the gem

config/application.rb

config.middleware.use Rack::Attack

initializers/rack-attack.rb

class Rack::Attack

throttle('logins/ip', :limit => 5, :period => 60.seconds) do |req|
  if req.path == '/login' && req.post?
  Rails.logger.error("Rack::Attack Too many login attempts from IP: #{req.ip}")
  req.ip 
  end
end

end

routes.rb

post   'login'   => 'index#create'
root 'index#new'
get 'login' => 'index#new'

I am using Rails 4.2.3 and the rack-attack gem 4.3.0

I wonder what I miss

1

2 Answers

make sure you configure cache.store in your initializers/rack-attack.rb file you can configure it like that:

class Rack::Attack
  ...
  cache.store = ActiveSupport::Cache::MemoryStore.new
  ...
end
1

You may need enable the cache in your development environment

please set config.cache_classes = true in config/environments/development.rb.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest