Web ブラウザ、スマートフォン、タブレットなどからログインできるようにする Rails 用の RESTful API を設計する必要があります。ただし、REST API はステートレスである必要があります。つまり、Cookie を使用しないでください。それを取り除く方法はありますか?そのための提案はありますか?
1 に答える
1
HTML と JSON の両方で応答するアプリでこれをどのように処理したかを次に示します。信頼できるソースからの API 呼び出しである場合を除いて、CSRF チェックが必要なので、
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
# has to come after the protect_from_forgery line
skip_before_filter :verify_authenticity_token, :if => :api_request?
# but don't just accept any URL with a .json extension, you need something else to
# ensure your caller is trusted (in this case I test for a valid API key being passed)
before_filter :api_key_valid?, :if => :api_request?
def api_request?
request.format == 'application/json'
end
# ... etc
end
于 2012-11-16T02:25:45.340 に答える