Token
对应头部字段及内容:
headers["WWW-Authenticate"] = %(Token realm="#{realm.gsub(/"/, "")}")Controller 方法:
提供方法:
authenticate_with_http_token
request_http_token_authentication
authenticate_or_request_with_http_tokenauthenticate_or_request_with_http_token 简单的封装了其余两个方法。
使用举例:
class PostsController < ApplicationController
TOKEN = "secret"
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_token do |token, options|
token == TOKEN
end
end
end再次举例:
其它方法:
传递 token 时,可以传递额外的数据,如:
然后使用 token_and_options 获取这些数据。
使用举例:
Token 验证的部分特点:
不能直接明文出现在 url 里。
通过 curl -H 'Authorization: Token token="x"' 传递数据。
通常用于制作程序接口。
不提供弹窗输入,其余两种验证方式提供。
最后更新于
这有帮助吗?