Digest
headers["WWW-Authenticate"] = %(Digest realm="#{realm}", qop="auth", algorithm=MD5,
nonce="#{nonce}", opaque="#{opaque}")authenticate_with_http_digest
request_http_digest_authentication
authenticate_or_request_with_http_digestrequire 'digest/md5'
class PostsController < ApplicationController
REALM = "SuperSecret"
USERS = {"dhh" => "secret",
# plain text password
# ha1 digest password
"dap" => Digest::MD5.hexdigest(["dap", REALM, "secret"].join(":"))}
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最后更新于