HTTP 헤더에 포함 된 인증 토큰이 사용자 테이블에 저장된 토큰과 일치하는 경우에만 리소스에 대한 액세스를 허용하고 싶습니다. 리소스에 액세스하기 위해 아래와 같이 curl을 사용하고 있습니다.
$ curl http://localhost:3000/api/v1/tasks.json -H 'Authorization: Token token="S8S4MPqFNdDz3G1jLsC9"'
tasks#index
방법 에서 위의 토큰이 데이터베이스에 저장된 현재 사용자의 인증 토큰과 일치하는지 확인하고 싶습니다. 토큰 값을 인스턴스 변수로 가져 와서 아래와 같이 사용할 수 있도록하려면 어떻게해야합니까?
def index
@token = ???
if User.exists?(:authentication_token => @token)
### code to access the resource
else
authenticate_user!
end
end
이 railscast 에서 가장 좋은 해결책은 authenticate_or_request_with_http_token
다음과 같은 rails 방법 을 사용 하는 것입니다.
class Api::V1::TasksController < ApplicationController
before_filter :restrict_access
respond_to :json
def index
@user = User.where("users.authentication_token IS NOT NULL").first
@tasks = @user.tasks
end
private
def restrict_access
authenticate_or_request_with_http_token do |token, options|
User.exists?(authentication_token: token)
end
end
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다