从 Rails 控制器发送 POST 请求进行身份验证

发布于 2024-11-03 20:40:53 字数 1199 浏览 0 评论 0原文

我对 Ruby 和 RoR 还很陌生,一直在努力解决一个问题,但没有取得任何进展。

基本上我正在构建一个“代理”网络服务来处理某些请求,并将它们传递给第三方网站。从第三方网站收到的响应采用 HTML 格式,然后将对其进行解析并给出适当的 XML 响应。

我现在需要通过 Rails 中的控制器发送 POST 请求来验证用户身份,我正在使用以下代码执行此操作:

require "net/http"
require "uri"

uri = URI.parse("http://myurl.com/members.cgi")

http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({"email_login" => "[email protected]", "password_login" => "password"})
response = http.request(request)

我的问题在于我收到的响应。此特定 POST 请求在成功时(即用户已成功通过身份验证)将返回 302 重定向。这本身不是问题,因为我可以通过获取“位置”标头值来跟踪重定向:

redirectLocation = response['location']

我遇到的问题是在使用以下行进行重定向时保持自己的身份验证:

redirectResponse = Net::HTTP.get_response(URI.parse(redirectLocation))

这遵循重定向但返回的响应显示我没有经过身份验证??

我真的不明白为什么会发生这种情况。我可以看到我的原始响应返回了一个身份验证 cookie:

response['cookie']

所以最后我的问题是我需要做什么才能让服务器识别我的身份验证状态?以某种方式通过我的第二个请求传递 cookie?如果是这样,我该怎么做?

非常感谢您抽出时间!

罗格

I am quite new to Ruby and RoR, and have been struggling with a problem and am not getting anywhere.

Basically I am building a "proxy" webservice that will handle certain requests, passing them to a third party website. The response received from the third party website is in HTML, which will then be parsed and an appropriate XML response will be given.

I'm at a point where I need to send POST requests via my controller in Rails to authenticate a user, which I'm doing using this code:

require "net/http"
require "uri"

uri = URI.parse("http://myurl.com/members.cgi")

http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({"email_login" => "[email protected]", "password_login" => "password"})
response = http.request(request)

My problem lies on the response I am receiving. This specific POST request, when successful (i.e. user has successfully authenticated), is returning a 302 redirect. That in itself is not an issue, as I can follow the redirect by getting the 'location' header value:

redirectLocation = response['location']

Where I'm getting stuck is in keeping myself authenticated when following the redirect with the following line:

redirectResponse = Net::HTTP.get_response(URI.parse(redirectLocation))

This follows the redirect but return a response showing I am not authenticated??

I don't really understanding why this is happening. I can see there is an authentication cookie being returned with my original response by reading:

response['cookie']

So finally my question is what do I need to do to get the server to recognise my authentication status? Pass the cookie with my second request somehow? If so, how do I do it?

Many thanks in advance for your time!

Rog

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眼泪 2024-11-10 20:40:53

是的,您需要设置 cookie。我认为它可能会给你某种会话 ID。您需要在每个请求中传递它。

查看 this 代码片段,了解如何传递您获得的 cookie对您的新请求的答复。

Yes you need to set the cookie. I think it probably give you some kind of a session id. Which you need to pass with every request.

look at this code snippet for a example on how to pass on a cookie that you get as a response with your new requests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文