4 응용 프로그램 I 컨트롤러에서 index
, create
, show
, update
및 destroy
방법에 액세스 할 수있는 API를 만드는거야.레일 필터 사용하기 전에 내 레일에서
인증을 위해 사용자 이름과 비밀번호 (access_key = username
, secret_key = password
및 master_secret_key = password
) 대신 HTTP 기본을 사용하여 특정 방법에 액세스합니다.
나는 access_key
/secret_key
쌍 만 create
이 update
, destroy
방법과 access_key
/master_secret_key
쌍 index
및 show
방법에 대한 액세스를 허용하는 액세스 좋아 (뿐만 아니라 다른 사람) 것입니다.
인증이 작동하지만 요청 방법에 따라 인증 방법을 Rails에 알려주는 데 문제가 있습니다.
제안 사항?
class Api::V1::TestsController < ApplicationController
before_filter { :authenticate! || :authenticate_with_master! }
skip_before_filter :verify_authenticity_token
respond_to :json
def index
end
def create
end
def show
end
def update
end
def destroy
end
protected
def authenticate!
authenticate_or_request_with_http_basic do |access_key, secret_key|
@credential = Credentials.find_by_access_key(access_key)
(@credential.secret_key == secret_key) ? true : false
end
end
def authenticate_with_master!
authenticate_or_request_with_http_basic do |access_key, master_secret_key|
@credential = Credentials.find_by_access_key(access_key)
(@user.master_secret_key == master_secret_key) ? true : false
end
end
end