• [ruby on rails]rack-cors, rack-attack


    gem 'rack-attack'
    gem 'rack-cors'
    
    • 1
    • 2

    1. rack-attack 可以根据ip、域名等设置黑名单、设置访问频率

    • 设置黑名单
    # 新增 config/initializers/rack_attack.rb
    # 请求referer如果匹配不上设置的allowed_origins,返回403 forbidden
    Rack::Attack.blocklist('block bad domains') do |req|
      next if !req.path.start_with?('/admin_api/') || Rails.env.test?
    
      Rails.application.credentials.allowed_origins.none? { |r| Regexp.new(r) =~ req.referer }
    end
    
    # EDITOR="vim" bin/rails credentials:edit
    allowed_origins:
      - api.xxx.net
      - localhost
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 设置访问频率
    class Rack::Attack
      # Rack::Attack.cache.store = ActiveSupport::Cache::RedisCacheStore.new(url: "...")
      Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
      # key: "rack::attack:#{Time.now.to_i/:period}:public_data/ip:#{req.ip}"
      throttle('public_data/ip', limit: 2, period: 1.minutes) do |req|
        req.ip if req.path.start_with?('/pc/v1/public_data')
      end
    
      self.throttled_responder = lambda do |_env|
        [429, # status
         {}, # headers
         ['throttling, retry later']] # body
      end
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2. rack-cors 可以根据域名、访问方法、资源设置跨域请求cors

    # config/initializers/cors.rb
    
    Rails.application.config.middleware.insert_before 0, Rack::Cors do
      allow do
        origins '*'
        resource '*', headers: :any, methods: [:get, :post, :put, :patch, :delete, :options, :head],
      end
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 复杂一些
    Rails.application.config.middleware.insert_before 0, Rack::Cors do
      allow do
        origins 'localhost:3000', '127.0.0.1:3000',
                /\Ahttp:\/\/192\.168\.0\.\d{1,3}(:\d+)?\z/
                # regular expressions can be used here
    
        resource '/file/list_all/', :headers => 'x-domain-token'
        resource '/file/at/*',
            methods: [:get, :post, :delete, :put, :patch, :options, :head],
            headers: 'x-domain-token',
            expose: ['Some-Custom-Response-Header'],
            max_age: 600
            # headers to expose
      end
    
      allow do
        origins '*'
        resource '/public/*', headers: :any, methods: :get
    
        # Only allow a request for a specific host
        resource '/api/v1/*',
            headers: :any,
            methods: :get,
            if: proc { |env| env['HTTP_HOST'] == 'api.example.com' }
      end
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
  • 相关阅读:
    什么是物联网(IoT),解释物联网的架构和应用场景
    [Shell详解-7]:循环语句
    HCL Domino LEAP与新的软件下载门户站点
    RHCSA认证考试---10.配置用户账户
    linux常用命令
    QT day1
    栈的概念|动态顺序栈的详细解析|静态顺序栈&链式栈的代码参考
    自学数据库-redis
    PHP的学习入门建议
    人工神经网络的算法原理,最简单的神经网络算法
  • 原文地址:https://blog.csdn.net/qq_41037744/article/details/134519179