有时候我们没法做用户认证,比如一个用户的收件箱的RSS feed或者一个激活注册用户的链接
我们可以通过一个access_key来做访问控制
[code]
class Inbox < ActiveRecord::Base
has_many :messages
before_create :generate_access_key
def generate_access_key
@attributes['access_key'] = MD5.hexdigest((object_id + rand(255)).to_s)
end
end
class FeedController < ApplicationController
before_filter :authenticate_access_key, :only => [:inbox]
def authenticate_access_key
inbox = Inbox.find_by_access_key(params[:access_key])
if inbox.blank? || inbox.id != params[:id].to_i
raise "Unauthorized"
end
end
def inbox
@inbox = Inbox.find(params[:id])
end
end
[/code]
其中access_key可能不是唯一的,我们最后采用[url=http://rubyforge.org/projects/uuidtools]UUID[/url]来做
这样我们可以给用户生成URL:
[code]
<%= url_for :controller => 'feed',
:action => 'inbox',
:id => @inbox,
:access_key => @inbox.access_key
%>
[/code]
这会生成类似于http://hideto.com/feed/inbox/5?access_key=43243tjhdifhf234534645gfgthy6yh546y56y46yfb的一个链接
这样用户就可以订阅该链接作为收件箱的RSS feed。
我们可以通过一个access_key来做访问控制
[code]
class Inbox < ActiveRecord::Base
has_many :messages
before_create :generate_access_key
def generate_access_key
@attributes['access_key'] = MD5.hexdigest((object_id + rand(255)).to_s)
end
end
class FeedController < ApplicationController
before_filter :authenticate_access_key, :only => [:inbox]
def authenticate_access_key
inbox = Inbox.find_by_access_key(params[:access_key])
if inbox.blank? || inbox.id != params[:id].to_i
raise "Unauthorized"
end
end
def inbox
@inbox = Inbox.find(params[:id])
end
end
[/code]
其中access_key可能不是唯一的,我们最后采用[url=http://rubyforge.org/projects/uuidtools]UUID[/url]来做
这样我们可以给用户生成URL:
[code]
<%= url_for :controller => 'feed',
:action => 'inbox',
:id => @inbox,
:access_key => @inbox.access_key
%>
[/code]
这会生成类似于http://hideto.com/feed/inbox/5?access_key=43243tjhdifhf234534645gfgthy6yh546y56y46yfb的一个链接
这样用户就可以订阅该链接作为收件箱的RSS feed。
本文介绍了一种通过生成Access Key来实现用户收件箱RSS订阅的方法,利用UUID确保唯一性,实现对用户数据的安全访问控制。
7200

被折叠的 条评论
为什么被折叠?



