【发布时间】:2015-06-15 14:22:40
【问题描述】:
在 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['origin']
我需要这个,因为我检查了访问令牌并且请求来自注册访问令牌的域。
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
感谢您的帮助!
【问题讨论】:
-
基本上,您不能指望以上任何内容都存在或准确。许多客户端不发送 HTTP_REFERRER 或 CORS 源头。欺骗标题也很简单。不要依赖它们来确保安全。
标签: ruby-on-rails api get header request