💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
> Quality, Speed or Cheap. Pick two. - Unknown 使用者認證Authentication用以識別使用者身分,而授權Authorization則用來處理使用者有沒有權限可以作哪些事情。 ## Authentication: 使用 Devise [devise](http://github.com/plataformatec/devise)是一套使用者認證(Authentication)套件,是Rails社群中最廣為使用的一套。 * 編輯 Gemfile 加上 ~~~ gem 'devise' ~~~ * 輸入`bundle install`安裝此套件 * 輸入`rails g devise:install`產生devise設定檔 * 編輯 config/environments/development.rb 和 production.rb 加入寄信時預設的網站網址: ~~~ config.action_mailer.default_url_options = { :host => 'localhost:3000' } ~~~ * 確認 app/views/layouts/application.html.erb layout 中可以顯示 flash 訊息,例如 ~~~ <p class="notice"><%= notice %></p> <p class="alert"><%= alert %></p> ~~~ * 確認 routes.rb 中有設定網站首頁位置,例如 ~~~ root :to => "welcome#index" ~~~ * 輸入`rails g devise user`產生 User model 及 Migration * 如果需要E-mail驗證功能,可以編輯`app/models/user.rb`和`migration`將confirmable功能打開 * 輸入`rails generate devise:views`產生樣板,這會包括有註冊、登入、忘記密碼、Email等等頁面,放在app/views/devise目錄下。 * 輸入`bin/rake db:migrate`建立資料表 ### 用法 * 在需要登入的 controller 加上`before_action :authenticate_user!` * 可以在 Layout 中加上登入登出選單 ~~~ <% if current_user %> <%= link_to('登出', destroy_user_session_path, :method => :delete) %> | <%= link_to('修改密碼', edit_registration_path(:user)) %> <% else %> <%= link_to('註冊', new_registration_path(:user)) %> | <%= link_to('登入', new_session_path(:user)) %> <% end %> ~~~ ### 加上自訂欄位 Devise預設沒有產生出first_name、last_name等等欄位,我們可以加一些欄位到User Model: * `rails g migration add_username_to_users`,加上 ~~~ add_column :users, :username, :string ~~~ * `rake db:migrate` 新增這個欄位 * 編輯application_controller.rb補上configure_permitted_parameters方法: ~~~ class ApplicationController < ActionController::Base before_action :configure_permitted_parameters, if: :devise_controller? # ... protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) << :username devise_parameter_sanitizer.for(:account_update) << :username end end ~~~ * 編輯views/devise/registrations/edit.html.erb和views/devise/registrations/new.html.erb,加上username欄位 ~~~ <div><%= f.label :username %><br /> <%= f.text_field :username %></div> ~~~ ## Authentication: 使用 Omniauth 除了使用上述的Devise自行處理使用者帳號密碼之外,現在也非常流行直接使用外部的使用者認證系統,例如Google、Facebook、Yahoo、GitHub等等,一來絕大部分的使用者都已經有了這些大網站的帳號,不需要再註冊一次。二來你也不需要擔心儲存密碼的安全性問題。 這方面利用的套件是[Omniauth](https://github.com/intridea/omniauth),他可以搭配各種不同的Provider廠商: * [omniauth-facebook](https://github.com/mkdynamic/omniauth-facebook) * [omniauth-google-oauth2](http://www.rubydoc.info/gems/omniauth-google-oauth2/0.2.5/frames) * [omniauth-yahoo](https://github.com/timbreitkreutz/omniauth-yahoo) * [omniauth-github](https://github.com/intridea/omniauth-github) ## Authentication: SSO 如果你有多個網站需要實作SSO(single sign-on),可以考慮採用[CAS](http://en.wikipedia.org/wiki/Central_Authentication_Service)的解決方案。推薦以下的Sinatra實作: * [rubycas-server](http://rubycas.github.io/) ## Authorization 在讓使用者登入之後,如果需要進一步設計使用者權限,除了自行實作之外,也有一些函式庫可以幫助你設計,例如: * [pundit](https://github.com/elabs/pundit) * [cancancan](https://github.com/CanCanCommunity/cancancan) ## OAuth OAuth 是一個開放的標準,允許用戶讓第三方應用訪問該用戶在某一網站上存儲的私密的資源(如照片,影片,聯繫人列表),而無需將用戶名和密碼提供給第三方應用。 * OAuth Client: [oauth2](https://github.com/intridea/oauth2) * OAuth Server: [doorkeeper](https://github.com/doorkeeper-gem/doorkeeper) 進一步關於OAuth的介紹,推薦[鴨七的OAuth 2.0 筆記](http://blog.yorkxin.org/tags/OAuth)