ActiveResource可以使用HTTP Basic验证方式,如果ActiveResource可以使用Digest验证方式,那么就不需要https安全连接访问资源了。这样一方面服务器配置简单,另一方面,速度上更有优势。
在服务器端,我们使用了
httpauth这个库实现了服务器端的HTTP Digest验证,具体实现请gem install httpauth后查看examples中的两个文件。客户端如果不需要ActiveResource的功能,直接参考httpauth例子就可以实现。
ActiveResource对于HTTP Digest的验证的支持我参考了日本人
taslam的
Basic認証以外に対応させる里面对于WSSE支持的实现。同样重新构造connection,并对于401的第一次失败作重新处理。这里不能使用ruby的http库,而需要使用mongrel作者Zed Shaw编写的全功能http client库
rfuzz。
taslam对于
ActiveResource的功底非常深厚,还有一篇文章,讲到如何提高ActiveResource处理数据的速度。
ActiveResourceが遅い→JSONならパースが速いよ
下面是activerecord 支持http digest authentication需要的代码和model示例。
AuthenticationCache代码来自于httpauth的例子程序,为节省篇幅就不再附上。
#lib/active_resource/digest.rb
require 'httpauth'
require 'rfuzz/client'
require 'authentication_cache'
module ActiveResource
class Connection::Digest < Connection
include HTTPAuth::Digest
def initialize(site, format = ActiveResource::Formats[:xml])
super
@cache = AuthenticationCache.new
end
def site=(site)
super
@client = RFuzz::HttpClient.new @site.host, @site.port
end
private
def authorization_header
{}
end
def request(method, path, *arguments)
RFuzz::HttpResponse.send(:define_method, :code, Proc.new(){ self.http_status })
RFuzz::HttpResponse.send(:define_method, :body, Proc.new(){ self.http_body })
args = {}
args[:head] = arguments.pop
args[:body] = arguments.pop
uri = URI.parse path
# If credentials were stored, use them. Otherwise do a normal get
credentials = @cache.get_credentials
if credentials
args[:head].update({"Authorization" => credentials.to_header})
end
result = nil
time = Benchmark.realtime { result = @client.send(method, path, args) }
if result.http_status == '401' and !result['WWW_AUTHENTICATE'].nil?
challenge = Challenge.from_header(result['WWW_AUTHENTICATE'])
(stale = challenge.stale) rescue NoMethodError
unless stale
username, password = site.user, site.password
else
username = credentials.username
password = credentials.password
end
credentials = Credentials.from_challenge(
challenge,
{:uri => path, :username => username, :password => password, :method => method.to_s.upcase }
)
args[:head].update({"Authorization" => credentials.to_header})
@cache.set_credentials_for uri.path, credentials
time = Benchmark.realtime { result = @client.send(method, path, args) }
end
if @cache.get_credentials
if result['AUTHENTICATION_INFO']
auth_info = AuthenticationInfo.from_header(result['AUTHENTICATION_INFO'])
@cache.update_usage_for uri.path, auth_info.h[:nextnonce]
else
@cache.update_usage_for uri.path
end
end
handle_response(result)
rescue Timeout::Error => e
raise TimeoutError.new(e.message)
end
end
class Digest < Base
def self.connection(refresh = false)
@connection = Connection::Digest.new(site, format) if refresh || @connection.nil?
@connection
end
end
end
class UserResource < ActiveResource::Digest
self.site = "http://user:pass@localhost:3000"
self.element_name = "user"
self.format = :ex_json
end
同时我发现rails 2.1新生成的new_default需要修改,否则json格式解析不会正确生成ActiveResource对象,我在这里也花了好长时间,因为我以为是json lib解析的问题。
if defined?(ActiveRecord)
# 原先为true,要改成false。
ActiveRecord::Base.include_root_in_json = false
end
分享到:
- 2008-10-24 00:04
- 浏览 1481
- 评论(1)
- 论坛回复 / 浏览 (1 / 4230)
- 查看更多
相关推荐
“摘要”式认证( Digest authentication)是一个简单的认证机制,最初是为HTTP协议开发的,因而也常叫做HTTP摘要,在RFC2671中描述。其身份验证机制很简单,它采用杂凑式(hash)加密方法,以避免用明文传输用户的...
RFC2617描述了HTTP Digest Authentication,这个是中文版,根据原英文版进行了校正,并重新排版。
digest 用户认证 response生成算法.适用于大部分网络安全认证。
HTTP Digest Auth库创建与HTTP Digest身份验证一起使用的auth标头 安装 您可以通过以下方式从十六进制安装此软件包: def deps do [{ :http_digex , " ~> 0.0.1 " }] end 或者,您可以选择从github安装,如下所...
Go中的HTTP身份验证实现这是Go语言中HTTP Basic和HTTP Digest身份验证的实现。 它被设计为http.RequestHandler函数的简单包装。特征支持HTTP基本和HTTP摘要身份验证。 支持htpasswd和htdigest格式的文件。 自动重新...
okhttp-digest okhttp的摘要身份验证器。 大多数代码是从Apache Http Client移植的。重要的该工件已从jcenter转移到了Maven Central! 坐标已从com.burgstaller:okhttp-digest:<version>到io.github.rburgst:okhttp-...
一个模拟http 401 Digest认证的登录海康NVR抓取摄像头列表的小demo
gsoap编译,onvif示例,包含发现设备,获取设备能力,ptz控制,wsse鉴权和digest鉴权。
用户输入需要通过MD5 digest的内容,程序给出digest输出结果
本示例演示如何使用 3.x和通过HTTP Digest方案对用户进行身份验证。 使用此示例作为您自己的Web应用程序的起点。 指示 要在您的计算机上安装此示例,请克隆存储库并安装依赖项。 $ git clone git@github....
本示例演示如何使用 3.x和通过HTTP Basic或HTTP Digest方案对用户进行身份验证。 使用此示例作为您自己的Web应用程序的起点。 指示 要在您的计算机上安装此示例,请克隆存储库并安装依赖项。 $ git clone git@...
赠送jar包:t-digest-3.2.jar; 赠送原API文档:t-digest-3.2-javadoc.jar; 赠送源代码:t-digest-3.2-sources.jar; 赠送Maven依赖信息文件:t-digest-3.2.pom; 包含翻译后的API文档:t-digest-3.2-javadoc-API...
AngularJS摘要式身份验证拦截器一步步客户端请求(无身份...去做允许自定义身份验证标头允许配置默认的用户名和密码(对于自动API身份验证很有趣) 为不成功的身份验证创建回调($ location.path('/ login'),$ state
NULL 博文链接:https://fengjianrong.iteye.com/blog/2379764
MK-QUERY-DIGEST也是一个用Perl脚本写的转存储日志分析程序。和MYSQLDUMPSLOW一样,也可以...在使用MK-QUERY-DIGEST工具之前必须要确保已经开启了慢日志,如何开启慢日志请参考《MYSQL慢查询日志功能的开启方法.doc》。
SM3国密算法第二部分,SM3DIGEST
t-digest 最新源码分享,t-digest 最新源码分享t-digest 最新源码分享t-digest 最新源码分享t-digest 最新源码分享t-digest 最新源码分享t-digest 最新源码分享t-digest 最新源码分享t-digest 最新源码分享t-digest ...
2、DIGEST(摘要验证):通过HTTP验证,需要提供摘要编码字符串的用户口令 3、FORM(表单验证):在网页的表单上要求提供密码 4、CLIENT-CERT(客户端证书验证):以客户端证书来确认用户的身份 基本验证 当web.xml文件中...
Digest-Perl-MD5
Laravel开发-digest-auth 内腔的摘要式身份验证RFC 2617