Check origin in Rails 5 API

孤者浪人 提交于 2019-12-08 14:09:06

问题


I'm building an app with Rails 5 API. Currently, anyone sending request to my rails server can receive response.

I want to process only those requests whose origin is mydomain.com

How can I do so?


回答1:


I believe you'll want to implement CORS on your API.

Simply add rack-cors gem.

And add:

 #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

Please read carefully its documentation

You'll find very useful information there.



来源:https://stackoverflow.com/questions/39185925/check-origin-in-rails-5-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!