---
title: "rails登陆认证鉴权"
author: "wenbin151"
date: "2010-03-01T23:15:52Z"
tags: ["Rails", "Ruby", "SVN", "Security", "UP"]
categories: ["编程语言"]
slug: "rails"
draft: false
lastmod: "2010-03-01T23:15:52Z"
id: 605298
url: "https://wenbin151.iteye.com/blog/605298"
---

ruby script/plugin install http://elitists.textdriven.com/svn/plugins/acts\_as\_state\_machine/trunk/

ruby script/plugin install http://svn.techno-weenie.net/projects/plugins/restful\_authentication/

 

 

＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝

Restful Authentication Generator  
\====

This is a basic restful authentication generator for rails, taken  
from acts as authenticated.  Currently it requires Rails 1.2.6 or above.

To use:

  ./script/generate authenticated user sessions \\  
        --include-activation \\  
        --stateful

The first parameter specifies the model that gets created in signup  
(typically a user or account model).  A model with migration is  
created, as well as a basic controller with the create method.

The second parameter specifies the sessions controller name.  This is  
the controller that handles the actual login/logout function on the  
site.

The third parameter (--include-activation) generates the code for a  
ActionMailer and its respective Activation Code through email.

The fourth (--stateful) builds in support for acts\_as\_state\_machine  
and generates activation code.  This was taken from:

http://www.vaporbase.com/postings/stateful\_authentication

You can pass --skip-migration to skip the user migration.

If you're using acts\_as\_state\_machine, define your users resource like this:

    map.resources :users, :member =&gt; { :suspend   =&gt; :put,  
                                     :unsuspend =&gt; :put,  
                                     :purge     =&gt; :delete }

Also, add an observer to config/environment.rb if you chose the  
\--include-activation option

  config.active\_record.observers = :user\_observer # or whatever you  
                                                        # named your model

Security Alert  
\====

I introduced a change to the model controller that's been tripping  
folks up on Rails 2.0.  The change was added as a suggestion to help  
combat session fixation attacks.  However, this resets the Form  
Authentication token used by Request Forgery Protection.  I've left  
it out now, since Rails 1.2.6 and Rails 2.0 will both stop session  
fixation attacks anyway.

＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝

ruby script/generate authenticated user sessions --include-activation --stateful

 

 ruby script/generate  controller home index

 

 

＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝

  map.root :controller =&gt; "home"

  map.home '/', :controller =&gt; 'home', :action =&gt; 'index'

  map.resources :users

  map.resource :session  
  map.signup '/signup', :controller =&gt; 'users', :action =&gt; 'new'  
  map.login  '/login', :controller =&gt; 'sessions', :action =&gt; 'new'  
  map.logout '/logout', :controller =&gt; 'sessions', :action =&gt; 'destroy'

  map.activate '/activate/:activation\_code', :controller =&gt; 'users', :action =&gt; 'activate', :activation\_code =&gt; nil

＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝

 

 

&lt;!-- home/index.rhtml --&gt;  
&lt;h1&gt;Welcome&lt;/h1&gt;

&lt;% if logged\_in? %&gt;  
   &lt;p&gt;&lt;strong&gt;You are logged in as &lt;%=h current\_user.login %&gt;&lt;/strong&gt;&lt;/p&gt;  
   &lt;p&gt;&lt;%= link\_to 'Logout', logout\_path %&gt;&lt;/p&gt;  
&lt;% else %&gt;  
   &lt;p&gt;&lt;strong&gt;You are currently not logged in.&lt;/strong&gt;&lt;/p&gt;  
   &lt;p&gt;  
     &lt;%= link\_to 'Login', login\_path %&gt; or  
     &lt;%= link\_to 'Sign Up', signup\_path %&gt;  
   &lt;/p&gt;  
&lt;% end %&gt;

 

更详细的文章

http://www.360doc.com/content/09/0407/22/4131\_3053944.shtml