在 Rails 5 API 中检查来源 [英] Check origin in Rails 5 API
本文介绍了在 Rails 5 API 中检查来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Rails 5 API 构建应用程序.目前,任何向我的 rails 服务器发送请求的人都可以收到响应.
I'm building an app with Rails 5 API. Currently, anyone sending request to my rails server can receive response.
我只想处理那些来自 mydomain.com 的请求
I want to process only those requests whose origin is mydomain.com
我该怎么做?
推荐答案
我相信您会希望在您的 API 上实现 CORS.
I believe you'll want to implement CORS on your API.
只需添加 rack-cors
gem.
并添加:
#config/application.rb
config.middleware.insert_before 0, Rack::Cors do
allow do
origins 'mydomain.com' resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
请仔细阅读其文档
您会在那里找到非常有用的信息.
You'll find very useful information there.
这篇关于在 Rails 5 API 中检查来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文