Tag: Rails/認証
class AdminController < ApplicationController before_action :authenticate def authenticate # BASIC認証 authenticate_or_request_with_http_basic do |name, password| name == "testuser2" && password == "testpass2" end end def index end end
class AdminsController < ApplicationController USERS = { "lifo" => "world" } before_action :authenticate private def authenticate authenticate_or_request_with_http_digest do |username| USERS[username] end end end
require 'digest/md5' class PostsController < ApplicationController REALM = "SuperSecret" USERS = {"dhh" => "secret", #plain text password "dap" => Digest::MD5.hexdigest(["dap",REALM,"secret"].join(":"))} #ha1 digest password before_action :authenticate, except: [:index] def index render plain: "Everyone can see me!" end def edit render plain: "I'm only accessible if you know the password" end private def authenticate authenticate_or_request_with_http_digest(REALM) do |username| USERS[username] end end end
class BooksController < ApplicationController end