nup*_*pac 5 ruby-on-rails restful-authentication ruby-on-rails-3.2
我有一个使用令牌来验证用户身份的 rails 应用程序。目前我将令牌作为参数传递。我想改变这一点。我相信可以通过 html 标头传递它。我不明白如何使用authenticate_or_request_with_http_token do |token, options|. 我的 rails 应用程序实际上是我的 iphone 客户端的服务器。我不明白的事情是:
我知道options是随机数,但是我的客户端和服务器之间如何解决?
我如何在我的服务器代码中实际使用它。
我可以authenticate_or_request_with_http_token do |token, options|用来检查标头中的令牌,但是一旦成功创建会话,如何将其插入标头中。
这是我的服务器会话控制器:
def create
if @user && @user.authenticate(params[:password])
# @token = @user.auth_token
@user.auth_token = SecureRandom.hex
@user.save
respond_to do |format|
format.json {render :json => {:status => "200", :message => "Logged in successfully", :auth_token => @user.auth_token}}
end
else
respond_to do |format|
format.json {render :json => {:status => "401", :message => "wrong credentials"}}
end
end
end
def destroy
if(@user)
@user.auth_token = ""
@user.save
respond_to do |format|
format.json {render :json => {:status => "200", :message => "logged out successfully"}}
end
else
respond_to do |format|
format.json {render :json => {:status => "401", :message => "No User"}}
end
end
end
def user
@user = User.find_by_auth_token(params[:auth_token])
end
Run Code Online (Sandbox Code Playgroud)
设置您使用的自定义标题 response.headers。
就像是
response.headers["X-AUTH-TOKEN"] = auth_token
Run Code Online (Sandbox Code Playgroud)
应该工作..阅读你使用的标题
request.headers["X-AUTH-TOKEN"]
Run Code Online (Sandbox Code Playgroud)
将X-在命名是一个很好的做法惯例,所有的自定义标题应该有一个X-在前面。
| 归档时间: |
|
| 查看次数: |
10528 次 |
| 最近记录: |