繁体   English   中英

从Rails API GET请求收集客户端域

[英]Gather Client Domain from Rails API GET Request

在Rails API上捕获客户端请求的域的最简单方法是什么?

例:

如果我使用https://www.hurl.it/将GET请求发布到:

https://staging.mysite.com/api/v1/products?access_token=7b9f3cddd3914a6f45fa692997fe6dc9

如何捕获请求来自hurl.it? 我试过了:

  • request.host
  • request.url
  • request.referer
  • request.headers [ '原点']

我需要这个,因为我检查了访问令牌,并且请求来自注册了访问令牌的域。

API控制器:

    module Api
      module V1
        class ApiController < ApplicationController
          respond_to :json
          before_filter :restrict_access

          private

          def restrict_access
            api_app = ApiApp.find_by_access_token(params[:access_token])
            binding.pry
            head :unauthorized unless (api_app && (api_app.request_origin.include? request.host.split('?').first))
          end
        end
      end
    end

Rails版本:4.2

谢谢您的帮助!

我最终坚持使用IP地址:

head :unauthorized unless (api_app && (api_app.request_origin == request.env["HTTP_X_REAL_IP"] ||= request.env["REMOTE_ADDR"]))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM