通过 ruby 脚本设计身份验证
我有一个小型应用程序,并且我会让一些外部应用程序通过 http 将数据放置到此服务中。我已经让它工作了,但没有身份验证。在门户中,我使用 devise,我的问题是:如何(所需的示例)从 ruby 脚本级别对门户进行身份验证?要首先在以下脚本中添加什么来进行身份验证?我想用设计保护这个控制器,然后我需要以下脚本的身份验证部分。
require "net/http"
require "json"
@host = "localhost"
@port = 3000
@post_ws = "/external/rd"
@req_body = {
"device" => {
"name" => "device_json",
"operating_system_id" => "7",
"hash_string" => "jfsg3k4ovj0j02jv",
"user_id" => "1"
}
}.to_json
req = Net::HTTP::Post.new(@post_ws, initheader = {'Content-Type' =>'application/json'})
req.body = @req_body
response = Net::HTTP.new(@host, @port).start {|http| http.request(req) }
问候, 马特乌什
I have small application, and I will have some external applications put data to this service over http with rest. I already have it working but without authentication. In portal I use devise, and my question is: how to (example desired) authenticate to portal from ruby script level? What to add to following script to authenticate first? I want to protect this controller with devise and then I need authentication part to following script.
require "net/http"
require "json"
@host = "localhost"
@port = 3000
@post_ws = "/external/rd"
@req_body = {
"device" => {
"name" => "device_json",
"operating_system_id" => "7",
"hash_string" => "jfsg3k4ovj0j02jv",
"user_id" => "1"
}
}.to_json
req = Net::HTTP::Post.new(@post_ws, initheader = {'Content-Type' =>'application/json'})
req.body = @req_body
response = Net::HTTP.new(@host, @port).start {|http| http.request(req) }
Regards,
Mateusz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是解决方案:
我使用了 devise 中的 token_authenticatable 。
这里是一个很棒的回答如何用json实现。我遇到了一些麻烦,并在此问题中描述了它们。也有答案。
这是示例代码:
问候,
马特乌什
Here is solution:
I used token_authenticatable from devise.
Here is one great answer how to implement it with json. I had some trouble and described them in this question. There is also answer.
Here goes example code:
Regards,
Mateusz