`

关于Rails登录和验证插件http_authentication restful-authentication

阅读更多
http://github.com/technoweenie/restful-authentication/
很热的一个插件下载

http_authentication比较轻量级
示例如下:
Simple Basic example:

class PostsController < ApplicationController
  USER_NAME, PASSWORD = "dhh", "secret"
  
  before_filter :authenticate, :except => [ :index ]
  
  def index
    render :text => "Everyone can see me!"
  end
  
  def edit
    render :text => "I'm only accessible if you know the password"
  end
  
  private
    def authenticate
      authenticate_or_request_with_http_basic do |user_name, password| 
        user_name == USER_NAME && password == PASSWORD
      end
    end
end


#Here is a more advanced Basic example where only Atom feeds and the XML API is #protected by HTTP authentication, 
#the regular HTML interface is protected by a session approach (NOTE: This example requires Rails Edge as 
#it uses Request#format, which is not available in Rails 1.2.0):

class ApplicationController < ActionController::Base
  before_filter :set_account, :authenticate

  protected
    def set_account
      @account = Account.find_by_url_name(request.subdomains.first)
    end
  
    def authenticate
      case request.format
      when Mime::XML, Mime::ATOM
        if user = authenticate_with_http_basic { |u, p| @account.users.authenticate(u, p) }
          @current_user = user
        else
          request_http_basic_authentication
        end
      else
        if session_authenticated?
          @current_user = @account.users.find(session[:authenticated][:user_id])
        else
          redirect_to(login_url) and return false
        end
      end
    end
end


#In your integration tests, you can do something like this:
  
  def test_access_granted_from_xml
    get(
      "/notes/1.xml", nil, 
      :authorization => HttpAuthentication::Basic.encode_credentials(users(:dhh).name, users(:dhh).password)
    )

    assert_equal 200, status
  end



http://github.com/binarylogic/authlogic/
本地下载

用法示例:
  class UserSession < Authlogic::Session::Base
    # specify configuration here, such as:
    # logout_on_timeout true
    # ...many more options in the documentation
  end


 
class UserSessionsController < ApplicationController
    def new
      @user_session = UserSession.new
    end

    def create
      @user_session = UserSession.new(params[:user_session])
      if @user_session.save
        redirect_to account_url
      else
        render :action => :new
      end
    end

    def destroy
      current_user_session.destroy
      redirect_to new_user_session_url
    end
  end

#As you can see, this fits nicely into the RESTful development pattern. What about the view…

  <% form_for @user_session do |f| %>
    <%= f.error_messages %>
    <%= f.label :login %><br />
    <%= f.text_field :login %><br />
    <br />
    <%= f.label :password %><br />
    <%= f.password_field :password %><br />
    <br />
    <%= f.submit "Login" %>
  <% end %>

#Or how about persisting the session…

  class ApplicationController
    helper_method :current_user_session, :current_user

    private
      def current_user_session
        return @current_user_session if defined?(@current_user_session)
        @current_user_session = UserSession.find
      end

      def current_user
        return @current_user if defined?(@current_user)
        @current_user = current_user_session && current_user_session.user
      end
  end
分享到:
评论

相关推荐

    RESTful.Rails.Development.2015.10.pdf

    根据提供的文件信息,我们可以推断出这是一本关于如何使用Ruby on Rails框架来构建RESTful应用程序和服务的专业书籍。书名为《RESTful Rails Development》,作者为Silvia Puglisi,出版时间为2015年10月。接下来,...

    Rails 3 in Action

    第六章至第八章涉及 **身份验证(Authentication)** 和 **授权(Authorization)**,这两个概念对于构建安全的 Web 应用至关重要。 - **身份验证**:确认用户的身份,通常通过用户名和密码组合完成。 - **授权**:根据...

    使用rails6开发纯后端API项目.zip

    在Rails 6中开发纯后端API项目是一个常见的任务,特别是在构建现代Web应用程序时,前端与后端分离的架构越来越流行。Rails作为一个强大的Ruby框架,提供了丰富的功能来帮助开发者高效地构建API服务。下面我们将详细...

    ruby on rails 的小型内容管理系统

    rails2.0的内容管理系统,可以发布文档、CVS库和Web资源3中资源,其中文档可以上传下载附件。支持打Tag。用户注册登录使用restful_authentication,分页使用will_paginate,Gem版本1.3.5

    Gobble:Gobble是一个受Slack启发的全栈单页消息传递应用程序。 它在后端使用Ruby on Rails生成RESTful API,在前端使用ReactRedux,并使用Pusher无缝实现WebSockets

    它在后端使用Ruby on Rails生成RESTful API,在前端使用React / Redux,并使用Pusher无缝实现WebSockets。 特征 使用Devise通过自动演示登录进行用户身份验证 即时通讯 讯息格式 通知事项当前频道通知 频道数 直接...

    laravel-token-authentication:Laravel 项目旨在创建和验证通过 RESTful API 使用的令牌

    Laravel 试图通过简化大多数 Web 项目中使用的常见任务(例如身份验证、路由、会话和缓存)来减轻开发过程中的痛苦。 Laravel 的目标是在不牺牲应用程序功能的情况下使开发过程对开发人员来说是一种愉快的过程。 ...

    Gymify-Backend:这是一个使用Ruby on Rails框架构建的REST API。 该API实现基于令牌的身份验证和授权

    GYMIFY API 使用Ruby on Rails构建的RESTFUL API。 该应用程序公开了健身房前端应用程序的API端点。 该应用程序允许用户注册,登录,查看培训师并与培训师预约约会。终点此api公开了两个端点,可以使用...

    Pragmatic.Bookshelf.Advanced.Rails.Recipes.May.2008

    书中有许多关于模型关联、查询优化、事务处理和复杂数据库设计的实例,帮助开发者更好地管理数据和数据库交互。 3. **Caching**:Rails提供了多种缓存策略,如页面缓存、动作缓存和碎片缓存,以提高应用程序性能。...

    后台管理框架

    Rails推崇敏捷开发,具有强大的社区支持和丰富的插件库。 (4) Laravel:PHP领域的热门框架,以优雅的语法和全面的特性和工具闻名。Laravel包含了艺术级别的路由、缓存、队列和任务调度,以及强大的Eloquent ORM。 ...

    meetingzero_api:MeetingZero Rails API

    例如,`Meeting`和`Participant`模型可能会包含数据验证、关联和其他业务规则。Rails的ActiveRecord库使得与数据库的交互变得简单。 4. **视图(Views)**:尽管API通常不涉及视图,因为它们主要处理数据交换,但...

    angular_practice:这是练习使用Angular和Rails

    在IT行业中,Angular和Rails是两个非常重要的框架,分别用于前端和后端开发。这篇文本将深入探讨这两个技术,以及如何将它们结合使用。 Angular,由Google维护,是一款强大的JavaScript框架,主要用于构建单页应用...

    RailsAPI

    RailsAPI 是一个专门为构建RESTful API设计的Ruby on Rails框架的子集。它专注于提供一个轻量级的、高效的环境,用于开发仅处理JSON或XML数据的后端服务,而无需传统的Web应用程序视图和路由。RailsAPI的目标是减少...

    商业编程-源码-网站后台管理系统框架(精美实用).zip

    框架可能包含了身份验证(Authentication)和授权(Authorization)机制,例如JWT(JSON Web Tokens)或OAuth2,来实现用户登录验证和权限分配。 4. **模板引擎** 框架内可能集成了模板引擎,如Thymeleaf(Java)...

    新闻发布系统源代码.........

    这涉及到身份验证(Authentication)和授权(Authorization)机制,如JWT(JSON Web Tokens)或OAuth。 6. **API接口**:如果系统需要与其他应用集成,可能包含RESTful API接口,用于数据交换。这些接口遵循HTTP...

Global site tag (gtag.js) - Google Analytics