新手问题 麻烦帮忙推荐一款简单用户认证的 gem,注册都不需要,只是密码限制访问即可

ted · October 09, 2014 · Last by lyfi2003 replied at October 09, 2014 · 2341 hits

devise 太重了,我的新应用只需要为页面设置一个需要密码访问的限制即可,谢谢 :)

硬编码,在 before_action 那里加上

很简单的需求,不需要任何 gem, 可参考 wblog 的后台登录代码

主控制器给一些 helper 方法 https://github.com/windy/wblog/blob/master/app/controllers/application_controller.rb#L25

登录提示 https://github.com/windy/wblog/blob/master/app/controllers/admin/sessions_controller.rb

其他控制器添加 before_action :authericate_user! 即可。

用 HTTP BaseAuth

谢谢三位!我都去了解下。

http_authentication 最轻量级的了。

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  
You need to Sign in before reply, if you don't have an account, please Sign up first.