간단하게 Rails에서 HTTP Auth 적용하는 방법 메모할겸 포스팅합니다.
(자꾸 까먹네요..)
Step 1. concern 모듈 만들어주기.
app/controllers/concerns/http_auth_concern.rb
(path는 개인 취향입니다. 그냥 controller 아래 만들어도.. 상관은 없어요)
module HttpAuthConcern
extend ActiveSupport::Concern
included do
before_action :http_authenticate
end
def http_authenticate
authenticate_or_request_with_http_basic do |username, password|
username == 'username' && password == 'password'
end
end
end
Step 2. Basic auth가 필요한 컨트롤러에 적용하기
include로 필요한 부분에 만들어둔 모듈을 로드해주면 끝납니다.include HttpAuthConcern
e.g
class YourPrivateController < ApplicationController
before_action :set_zzz, only: [:show, :edit, :update, :destroy]
skip_before_action :verify_authenticity_token, :only => [:testzzsfdfsd]
include HttpAuthConcern
Step 3. There’s no third one.
https://i.giphy.com/d8YBM0IgPszle.gif |
HAHWULSecurity engineer, Gopher and H4cker! |
0 개의 댓글:
Post a Comment