代码之家  ›  专栏  ›  技术社区  ›  Scott Miller

这是使用api密钥进行http基本身份验证的合理方法吗?

  •  0
  • Scott Miller  · 技术社区  · 16 年前

    我刚刚开始在rails应用程序上添加rest api,由于我只想公开几个控制器/操作,所以我向applicationcontroller添加了一个方法:

      def http_basic_authentication
        if request.format == Mime::XML
          authenticate_or_request_with_http_basic do |username, api_key|
            self.current_user = User.find(:first, :from => 'users, accounts', :conditions => ["accounts.id = users.account_id AND accounts.api_key = ?", api_key])
          end
        end
      end
    

    然后,我可以在我想要暴露的个人控制器/动作上使用一个前置过滤器。是否有人有任何反馈、代码审查或更好的方法?

    2 回复  |  直到 15 年前
        1
  •  1
  •   Sophie Alpert    16 年前

    可能这会更干净:

    self.current_user = Account.find_by_api_key(api_key).user
    
        2
  •  0
  •   simianarmy    15 年前

    你可能会发现这里详述的方法很有用 http://www.compulsivoco.com/2009/05/rails-api-authentication-using-restful-authentication/

    这与常见的restful_身份验证插件集成。