在 rails 中进行 Http 基本身份验证 [英] Doing a Http basic authentication in rails
本文介绍了在 rails 中进行 Http 基本身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Grails 背景的,并且是 Rails 的新手.我希望在 Rails 中进行 http 基本身份验证.
Hi I'm from Grails background and new to Rails. I wish to do http basic authentication in rails.
我在 grails 中有一个代码,它执行这样的基本身份验证:
I have a code in grails which does basic authentication like this:
def authString = "${key}:".getBytes().encodeBase64().toString()
def conn = "http://something.net".toURL().openConnection()
conn.setRequestProperty("Authorization", "Basic ${authString}")
可以用 Rails 做同样的事情吗?
Can the same be done with rails?
推荐答案
写下面的代码,在你想限制使用http基本认证的控制器中
Write the below code, in the controller which you want to restrict using http basic authentication
class ApplicationController < ActionController::Base
http_basic_authenticate_with :name => "user", :password => "password"
end
使用 open-uri 发出请求将如下所示:
Making a request with open-uri would look like this:
require 'open-uri'
open("http://www.your-website.net/",
http_basic_authentication: ["user", "password"])
这篇关于在 rails 中进行 Http 基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文