set authentication token in http header

前端 未结 3 1749
梦毁少年i
梦毁少年i 2021-01-19 02:07

I have been following the railscast on how to set authentication tokens http://railscasts.com/episodes/352-securing-an-api?view=asciicast

I have setup my app very we

相关标签:
3条回答
  • 2021-01-19 02:40

    The accepted answer does not work.

    In Rails 4 it should be request.authorization not request['authorization']

    0 讨论(0)
  • 2021-01-19 02:44

    The header name isn't HTTP_AUTHORIZATION and you have to set it like this set it as:

    request['authorization'] = "Token token=#{token}"
    

    To be able to use the authenticate_or_request_with_http_token method.

    0 讨论(0)
  • 2021-01-19 02:48

    Look at the ActionController::HttpAuthentication module, e.g.

    user = 'whatever'
    pass = 'you-like'
    auth = ActionController::HttpAuthentication::Basic.encode_credentials(user, pass)
    request.headers['Authorization'] = auth
    

    Similarly for a token, e.g.

    token = 'whatever-it-is'
    auth = ActionController::HttpAuthentication::Token.encode_credentials(token)
    request.headers['Authorization'] = auth
    
    0 讨论(0)
提交回复
热议问题