Ruby on Rails 中身份验证的最佳解决方案

我正在寻找一个预先构建的解决方案,我可以在我的 RoR 应用程序中使用。理想情况下,我正在寻找类似于 ASP.NET Forms 身份验证的东西,它提供电子邮件验证、注册控件和允许用户重置密码。哦,是的,并且很容易地允许我拉出当前登录到应用程序中的用户。

我已经开始研究已经写好的文章,但是我发现它真的很令人困惑。我看过 LoginGenerator,RestfulAuthentication,SaltedLoginGenerator,但似乎没有一个地方有很好的教程或提供它们的比较。如果有一个网站我还没有发现,或者有一个事实上的标准,大多数人使用,我会感谢帮助的手。

56462 次浏览

I would really recommend Restful Authentication. I think it's pretty much the de-facto standard.

There's also RestfulOpenIDAuthentication if you want OpenID support in addition to password support.

Just a note, LoginGenerator and SaltedLoginGenerator have been superseded by Restful Authentication and are unsupported on newer Rails releases -- dont waste any time on them, though they were great at the time.

I'd also like to point out an excellent tutorial/discussion on extending the core functionality of Restful Authentication, in case you're looking for something a bit more robust.

restful_authentication is a powerful tool which is very flexible and provides most of what you are looking for out of the box. However, a couple of caveats:

  1. Don't think in terms of 'controls'. In Rails the Model, View and Controller are much more independent than in 'Webforms-style' ASP.NET. Work out what you want from each layer independently, write tests/specs to match and make sure each layer is doing what you expect.
  2. Even if you are using a plugin there is no substitute for reading (at least some) of the code generated. If you have a big-picture idea of what is going on under the hood, you will find debugging and customising much easier.

The plugin restful_authentication and other plugins that extend it, answer your needs perfectly. A quick search on github.com will reveal a lot of tutorials, examples, and extensitons. Just go here:
- http://github.com/search?q=restful_authentication

There are several projects that use restful_authentication just to provide examples of a bare-bones Rails app with just the authentication parts.

  1. http://github.com/fudgestudios/bort -- A base rails app featuring: RESTful Authentication
  2. http://github.com/mrflip/restful_authentication_example -- Another project with a great examlpe of how to use restful_authentication
  3. http://github.com/activefx/restful_authentication_tutorial -- Same as above, with some other plugins bundled.
  4. http://railscasts.com/episodes/67-restful-authentication -- a great screencast explaining restful_authentication

This information should be enough to get you started finding heads and tails ... good luck.

AuthLogic appears to be the new kid on the block and seems to be the next evolution of restful_authentication, easier to use, etc

http://github.com/binarylogic/authlogic/tree/master

Edit: now that Rails 3 is out, Devise seems to be the new, new kid on the block

https://github.com/plataformatec/devise or I have been rolling my own authentication now with the has_secure_password built in to Rails http://railscasts.com/episodes/250-authentication-from-scratch-revised

Side note: Ruby Toolbox is a great site for finding the current best solution in various categories (based on the number of GitHub watchers):

http://ruby-toolbox.com/categories/rails_authentication.html

AuthLogic seems to be what you want for this. It's very configurable, and although it doesn't generate the code for you, it's quite easy to use. For email validation and password recovery you probably want to use the :perishable_token column. AuthLogic takes care of it, you only need to reset it when it's used. For information on how to set up a basic app, you can take a look at Ryan Bates' Railscast on AuthLogic, and the "official" example app. Ben Johnson, the creator of AuthLogic has also written a blog post on how to RESTfully reset passwords.

Unfortunately I can't post more than one link, but the links to the railscast, the password reset blog post and the example app are all in the README (see the AuthLogic repo for the README)

Update: Now I can post more links, so I linked some more. Thank you marinatime for adding the link in the meanwhile

I'm really liking thoughtbot's clearance. Very simple and has a few good hooks and is testable.

Another vote for Clearance - perhaps not as customisable or as 'in' as authlogic, but in terms of just being able to drop it in place and go, it's definitely worth having a look at.

For a really simple solution go with Clearance.

If you are looking for more options Devise is a great solution. It uses Warden which is a rack based authentication system.

Just updating this: Ryan Bates' Railscast #250 shows building an authentication system from scratch....