在 http 标头中设置身份验证令牌 [英] set authentication token in http header
问题描述
我一直在关注关于如何设置身份验证令牌的 railscasthttp://railscasts.com/episodes/352-securing-an-api?view=asciicast
I have been following the railscast on how to set authentication tokens http://railscasts.com/episodes/352-securing-an-api?view=asciicast
我已经很好地设置了我的应用程序,它使用 authenticate_or_request_with_http_token 方法来获取令牌.
I have setup my app very well and it uses the authenticate_or_request_with_http_token method to get the token.
我的问题是我有一个需要在标头中设置令牌的应用程序.类似的东西:
My problem is that I have a next app that needs to set the token in the header. Something like:
uri = URI.parse(full_url)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request['HTTP_AUTHORIZATION'] = 'this_is_a_test_key'
response = http.request(request)
上面的代码被拒绝访问.我知道设置像 X-CUSTOM-TOKEN 这样的自定义项很容易,但如何设置默认项?
The above code is getting an access denied. I know it is easy to set custom ones like X-CUSTOM-TOKEN, but how do I set the default one?
推荐答案
标题名称不是 HTTP_AUTHORIZATION
你必须像这样设置它设置为:
The header name isn't HTTP_AUTHORIZATION
and you have to set it like this set it as:
request['authorization'] = "Token token=#{token}"
为了能够使用authenticate_or_request_with_http_token
方法.
这篇关于在 http 标头中设置身份验证令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!