`
boyjunqiang
  • 浏览: 32355 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

怎样在你的 ruby on rails 网站中加入 OpenID 登录功能

阅读更多
my OS is ubuntu7.10:
1.cd to the project directory

2.
sudo gem install ruby-openid


3.
sudo ruby script/plugin install open_id_authentication


4.rake -T open_id (to see the rake command)

5.
rake open_id_authentication:db:create


6.in the open-id migrate add these lines:
   
add_column :users, :identity_url, :string 
(in self.up)
  
remove_column :users, :identity_url
(in self.down)

7.run the migrate

8.add a line in routes.rb:
    map.open_id_complete 'session', :controller => "session", :action => "create",:requirements => { :method => :get }


VERY VERY IMPORTENT!!!
make sure the map.open_id_complete line above the map.resource :session line;(IT waste lots of my time!!!)

9:add a :identity_url in user.rb and it will look like:
    attr_accessible :login, :email, :password, :password_confirmation, :identity_url


10.add some line in session/new.rhtml:
  <p>
    <label for="openid_url">OpenID URL</label><br/>
    <%=text_field_tag "openid_url"%>
  </p>


11.:
# This controller handles the login/logout function of the site.
class SessionController < ApplicationController
  # Be sure to include AuthenticationSystem in Application Controller instead
  # include AuthenticatedSystem

  # render new.rhtml
  layout "welcome"
  
  def new
  end

  def create
    if using_open_id?
      open_id_authentication(params[:openid_url])
    else
      password_authentication(params[:login], params[:password])
    end
  end
  
  def destroy
    self.current_user.forget_me if logged_in?
    cookies.delete :auth_token
    reset_session
    flash[:notice] = "你已经登录了."
    redirect_back_or_default('/')
  end
  
  protected
  
  def open_id_authentication(openid_url)
    authenticate_with_open_id(openid_url, :required => [:nickname, :email]) do |result, identity_url, registration|
      if result.successful?
        @user = User.find_or_initialize_by_identity_url(identity_url)
        if @user.new_record?
          @user.login = registration['nickname']
          @user.email = registration['email']
          @user.save(false)
        end
        self.current_user = @user
        successful_login
      else
        failed_login result.message
      end
    end
  end
  
  def password_authentication(login, password)
    self.current_user = User.authenticate(login, password)
    if logged_in?
      successful_login
    else
      failed_login
    end
  end
  
  def failed_login(message = "Authentication failed.")
    flash.now[:error] = message
    render :action => 'new'
  end

  def successful_login
    if params[:remember_me] == "1"
      self.current_user.remember_me
      cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
    end
    redirect_back_or_default('/tools')
    flash[:notice] = "登录成功"
  end

  
end


12.add some css:
/* embeds the openid image in the text field */
input#openid_url {
   background: url(http://openid.net/login-bg.gif) no-repeat;
   background-color: #fff;
   background-position: 0 50%;
   color: #000;
   padding-left: 18px;
} 
分享到:
评论
1 楼 nonocast 2007-12-20  
sfct
好文,自从beast看到openID也看了一些介绍
能看到哥们具体step by step,呵呵,嗲

相关推荐

    使用devise_openid_authenticatable的devise_example版本_Ruby_.zip

    在描述中,"使用devise_openid_authenticatable的devise_example版本_Ruby_"进一步确认了这是一份示例代码,展示了如何在Ruby on Rails应用中集成`devise_openid_authenticatable`。开发者可能希望通过查看这个例子...

    ruby-openid:Ruby的OpenID库

    基于Ruby on Rails的使用者和服务器 OpenIDLoginGenerator用于快速创建使用OpenID进行身份验证的Rails应用程序 ActiveRecordOpenIDStore插件 全面的测试套件 透明地支持OpenID 1和OpenID 2 正在安装 在运行示例或...

    Alumni on Rails-开源

    在"Alumni on Rails"中,"信任的环境"可能通过用户认证机制实现,比如OAuth或OpenID集成,确保只有真实的校友才能加入平台。"可持续的联系"可能通过强大的社交功能如好友系统、消息通知和活动提醒来实现。"职业发展...

    OAuth gem for rails3

    OAuth gem for Rails3 是一个针对Ruby on Rails框架的授权库,它实现了OAuth协议,以便于在Rails应用中安全地处理第三方服务的授权流程。OAuth是一个开放标准,允许用户让第三方应用在无需分享用户名和密码的情况下...

    GTBAd-Advertisers:我们 GTBAd 系统广告商的 Web 前端

    Ruby on Rails OpenID 连接 如何构建 要求 节点 新产品经理 哟 凉亭 grunt-cli Ruby 程序 git 克隆 cd GTBAd-广告商 安装 凉亭安装 捆绑安装 --path 供应商/捆绑 咕噜咕噜的服务 是的! 您将在浏览器上看到 ...

    Cloud-Computing

    Ruby on Rails(简称Rails)是基于MVC(模型-视图-控制器)架构模式的开源Web开发框架,它简化了数据库集成、路由、模板和脚本等任务,大大提高了开发效率。 云计算中的Ruby应用通常涉及以下几个方面: 1. **开发...

    碰巧

    Ruby,Ruby On Rails Docker /组成 OpenID连接 WebSocket AWS S3 など 主な机能 ユーザ登录,ログイン(ユーザー登录,ロイン共にLineロ実インを使いグ装),ログアウト继续はパスワードとユーを名名

    intercode:常规Web应用程序的未来

    后端:Ruby on Rails应用程序公开了GraphQL API和启用了OpenID Connect的OAuth2服务器 前端:基于React和Apollo的单页JavaScript应用 数据库引擎:PostgreSQL 后台队列系统:Amazon SQS + Shoryuken(将来可能会...

    OmniAuth是一个使用Rack中间件的灵活的身份验证系统。.zip

    OmniAuth是一个强大的身份验证解决方案,它利用了Rack中间件的灵活性,为Ruby on Rails和其他使用Rack的应用程序提供了一种标准化的方式处理多种身份验证服务。Rack是Ruby web服务器接口,允许开发者构建自己的web...

    仿豆瓣社区圈子

    10. **后端架构**:Node.js、Django、Ruby on Rails等后端技术,实现服务器端逻辑;RESTful API设计,提供前后端分离的开发模式。 通过以上知识点的深入理解和实践,开发者可以打造出一个类似"仿豆瓣社区圈子"的...

    Laravel开发-df-core

    Laravel 的设计思想深受 Ruby on Rails 启发,强调代码的简洁性和可读性,使得开发者能够快速开发出高质量的应用。而 "df-core" 在这里指的是 DreamFactory 的核心组件,DreamFactory 是一个基于云的 API 平台,它...

    SpringOne-Using Spring Security 2

    - **Grails and Trails**:与Groovy和Ruby on Rails框架集成; - **Mule**:与企业集成平台Mule ESB集成; - **DWR**:与Direct Web Remoting集成; - **Appfuse**:与快速开发框架集成; - **AndroMDA**:与元数据...

    Dancer-Plugin-Auth-Extensible:基于Dancer的Web应用程序的身份验证框架

    Dancer 是一个轻量级、灵活的Perl Web应用框架,它的设计灵感来源于Ruby on Rails。Dancer强调简洁的语法,使得开发人员可以快速构建小型到中型的Web应用程序。它提供了路由、模板引擎、中间件等功能,同时支持多种...

    frameworktest

    3. **捆绑(Bundler)**:虽然通常与Ruby on Rails关联,但这里可能是用在了某种Java项目中,或者是开发者将它用于Java生态系统的依赖管理。Bundler确保在一个项目中使用的所有gem(Ruby库)版本都保持一致,避免因...

    LETS-Talk-开源

    6. **Web应用程序框架**:项目可能基于某种流行的Web开发框架,如Spring Boot、Django或Ruby on Rails,这些框架提供了快速开发、路由处理、模板引擎和数据库连接等功能。 7. **前端技术**:用户界面的构建可能利用...

    聚集旅行者

    - **Ruby on Rails**: 如果是基于Ruby的系统,可能使用RoR框架。 - **RESTful API**: 设计用于前后端分离的接口,遵循REST架构风格。 3. **数据库**: - **MySQL/MongoDB**: 存储用户信息、旅行计划、帖子等数据...

    hello-minnow

    1. **MVC架构**:Sails.js遵循类似于Ruby on Rails的模式,使得开发者可以更高效地组织代码,通过模型操作数据库,视图处理用户界面,控制器协调两者交互。 2. **Waterline ORM**:Sails.js内置了Waterline对象关系...

Global site tag (gtag.js) - Google Analytics