【问题标题】:403 Forbidden when doing AJAX call to non-restful action within restful controller403 Forbidden 当在 RESTful 控制器中执行 AJAX 调用非 RESTful 操作时
【发布时间】:2016-10-30 21:30:20
【问题描述】:

我有一个 Profile 模型,我正在尝试通过 AJAX 进行更新。

这是我的routes.rb

  resources :profiles do
    member do
      patch :speed_rating
      patch :dribbling_rating
      patch :passing_rating
      patch :tackling_rating
    end
  end

生成以下路由:

speed_rating_profile_path   PATCH   /profiles/:id/speed_rating(.:format)    
profiles#speed_rating

dribbling_rating_profile_path   PATCH   /profiles/:id/dribbling_rating(.:format)    
profiles#dribbling_rating

passing_rating_profile_path PATCH   /profiles/:id/passing_rating(.:format)  
profiles#passing_rating

tackling_rating_profile_path    PATCH   /profiles/:id/tackling_rating(.:format) 
profiles#tackling_rating

这是我的ProfilesController

class ProfilesController < ApplicationController
  before_action :set_profile, only: [:show, :edit, :update, :destroy, :invite_user, :speed_rating, :tackling_rating, :dribbling_rating, :passing_rating]
  authorize_resource except: [:dashboard]
  skip_authorization_check only: :dashboard

  def speed_rating
    binding.pry
  end

  def dribbling_rating
    binding.pry
  end

  def passing_rating
    binding.pry
  end

  def tackling_rating
    binding.pry
  end

  private
    def set_profile
      @profile = Profile.friendly.find(params[:id])
    end

    def profile_params
      params.require(:profile).permit(:id, :first_name, :last_name, :dob, :height, :weight, :bib_color, videos_attributes: [:id, :url, :video, :vimeo_url, :vimeo_embed_code, :official, :video_cache, :remove_video, :_destroy], transcripts_attributes: [:id, :url, :name, :transcript, :remove_transcript, :url_cache, :_destroy])
    end

在我的profile.js 中,我正在这样做:

  var url = "/profiles/" + _profile_id + "/" + _rating_type + "_rating/?" + _rating_type + "=" + button.innerText

  $.ajax({
    type: "PATCH",
    url: url,
    success: function(result){
      console.log(_profile_id + "'s " + _rating_type + " was successfully updated.");
      console.log(result);
    },
    error: function(result){
      console.log(_profile_id + "'s " + _rating_type + " was successfully updated.");
      console.log(result);
    }
  })

这是我得到的错误:

jquery.self-bd7ddd3….js?body=1:10255 PATCH http://localhost:3000/profiles/rebecca-nitzsche-st-george-s-college/dribbling_rating/?dribbling=6 403 (Forbidden)

编辑 1

我正在使用 CanCanCan 进行授权。

我尝试过的一件事是将这些新操作添加到授权例外列表中,具体如下:

authorize_resource except: [:dashboard, :speed_rating, :dribbling_rating, :tackling_rating, :passing_rating]

到目前为止,这似乎奏效了。但是,这会使我的应用不安全吗?

如何在我的正确授权范围内使其工作,以确保任何用户在发现这些路由时都不会滥用它们?

【问题讨论】:

    标签: jquery ruby-on-rails ajax ruby-on-rails-5 cancancan


    【解决方案1】:

    我将假设这一行:

    authorize_resource except: [:dashboard]
    

    正在进行某种形式的身份验证或授权,这可能需要用户详细信息。在您的 AJAX 请求中,我看不到任何有关用户名或密码或会话 cookie 的规定。您需要确保将会话 cookie 与 AJAX 请求一起发送,以便成功授权该请求。

    您可以通过在对$.ajax 的调用中包含一个xhrFields 对象来做到这一点:

    $.ajax({
        'type': 'PATCH',
        'url': 'whatever',
        'xhrFields': {
            'withCredentials': true
        }
        // whatever other AJAX options you need
    });
    

    【讨论】:

    • 我喜欢你的思路,但那行不通:(
    • 你是对的,只是你的处方有点错误。我用我尝试过的方法和有效的方法更新了这个问题。但从安全的角度来看,这感觉不是最好的方法。如何确保这些操作仍受限于 CanCanCan 管理的授权规则?
    猜你喜欢
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多