devise+ActiveDirectory(LDAP)認証
Last-modified: Tue, 23 Sep 2014 17:57:06 JST (3354d)
Top > devise+ActiveDirectory(LDAP)認証
deviseのインストールと設定
- Gemfileに以下追記
gem 'devise' gem 'devise_ldap_authenticatable'
- インストール
bundle install rails g devise:install rails g devise User rails g devise_ldap_authenticatable:install
- app/models/user.rbを編集。:registerable,:recoverable,:validatableを削除。
最終的に以下のようにする。class User < ActiveRecord::Base devise :ldap_authenticatable, :rememberable, :trackable end
- db/migrate/YYYYMMDDhhmmss_devise_create_users.rbを編集。
デフォルトではemailでログインなので、修正したり、パスワードリマインダを無効にしたりする。
最終的に以下のようにする。class DeviseCreateUsers < ActiveRecord::Migration def change create_table(:users) do |t| ## LDAP authenticatable t.string :login, null: false, default: "", unique: true ## Database authenticatable #t.string :email, null: false, default: "" #t.string :encrypted_password, null: false, default: "" ## Recoverable #t.string :reset_password_token #t.datetime :reset_password_sent_at ## Rememberable t.datetime :remember_created_at ## Trackable t.integer :sign_in_count, default: 0, null: false t.datetime :current_sign_in_at t.datetime :last_sign_in_at t.string :current_sign_in_ip t.string :last_sign_in_ip ## Confirmable # t.string :confirmation_token # t.datetime :confirmed_at # t.datetime :confirmation_sent_at # t.string :unconfirmed_email # Only if using reconfirmable ## Lockable # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts # t.string :unlock_token # Only if unlock strategy is :email or :both # t.datetime :locked_at t.timestamps end add_index :users, :login, unique: true # add_index :users, :reset_password_token, unique: true # add_index :users, :confirmation_token, unique: true # add_index :users, :unlock_token, unique: true end end
- マイグレーションする
rake db:migrate
- config/initializers/devise.rbを編集する。(修正箇所のみピックアップ)
config.ldap_logger = true config.ldap_create_user = true config.ldap_update_password = false config.ldap_use_admin_to_bind = true config.authentication_keys = [ :login ]
なお、ldap_create_userは、ユーザを新規作成するという意味ではなく、
すべての有効なLDAPユーザーがログインを許可され、適切なユーザー・レコードが作成されるオプションらしい。
falseにすると、予めDBにレコードを作成する必要がある。github - config/ldap.ymlを編集。以下は参考例です。
development: host: ad.exsample.local port: 389 attribute: sAMAccountName base: ou=myhome,dc=exsample,dc=local admin_user: ldap admin_password: ldap_pass ssl: false
ActiveDirectoryをLDAPでアクセスする際、ユーザ名はsAMAccountNameという項目に入っているので、
attribuleの値をsAMAccountNameにします。
また、ADは匿名アクセスが出来ないので、LDAP認証するために接続するアカウントが必要になります。
ここでは、ldapという名前のアカウントを作成し、それを使っています。
ここで設定するのは、LDAPのcn項目に入っているユーザ名(ADでいうフルネーム)の項目なので注意!(Notログインユーザ名)
なお、サンプルにあるcn=admin,dc=test,dc=comのような指定は試した感じ無効でした。
なお、OUだとかDCだとかを調べるのに、Apache Directory Studioが大変便利です。
- ログイン用のビューを作成します。
rails g devise:views
- app/views/devise/session/new.html.hamlを編集します。
TwitterBootstrapとか入れていると一部違いますが、
基本的には、:emailの箇所を:loginになおしたり、email_fieldをtext_fieldにしたりです。
以下は参考までに・・<h2>Sign in</h2> <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> <div><%= f.label :login %><br /> <%= f.text_field :login, autofocus: true %></div> <div><%= f.label :password %><br /> <%= f.password_field :password, autocomplete: "off" %></div> <% if devise_mapping.rememberable? -%> <div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div> <% end -%> <div><%= f.submit "Sign in" %></div> <% end %> <%= render "devise/shared/links" %>
- /config/routes.rbを編集し、deviseの機能をログイン、ログアウトのみに限定します。
(特に設定しなくてもsessionしかルーティングの設定はされていないようです。)devise_for :users, only: [:sessions]
- ルーティングを確認
rake routes
- 全ページ認証がかかるようにします。
/app/controllers/application_controller.rbの最後に以下追記before_action :authenticate_user!
- ログアウトのリンクを適当なところに貼り付ける。
<%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link' %>
- 日本語訳を入れる
config/application.rbを編集。config.i18n.default_locale = :ja
- ここ
からdevise.ja.ymlをダウンロードし、config/localesに入れる。
Counter: 4786,
today: 3,
yesterday: 1
このページの参照回数は、4786です。