Merblog

merbでBASIC認証

merb-authとかいろいろプラグインはありますが、簡単なBASIC認証ならMerb本体(merb-core)だけでもできます。

まずはコントローラのサンプルコード。

class Users < Application
  before :auth,:exclude => [:show,:index]

  def index
    # ユーザー一覧とか
  end
  
  def new
    # 新規ユーザー追加
    # これが呼ばれる前にauthが呼ばれる
  end

  def show
    # 既存ユーザーの表示
  end

  protected
    def auth
      basic_authentication("your realm") do |username, password| 
        user == "user" && password == "password"
      end 
    end
end

/users/以外でも使うならapp/controllers/application.rbに書いておけば全コントローラから呼び出せる。

参考資料

タグ

コメント

名前
本文
2+5=