首页 > 解决方案 > 为什么 ldap 绑定在服务器上失败?

问题描述

我在我的 Rails 应用程序中对 LDAP 服务器进行身份验证,下面的代码在本地工作,但不在服务器上工作。

Net::LDAP::BindingInformationInvalidError (Invalid binding information)在尝试登录应用程序时抛出的服务器上,但通过控制台工作

我对 Ruby 很陌生,无法找出调试它的正确方法......我知道 LDAP 配置是正确的,因为我可以从控制台或本地开发环境中进行身份验证和绑定.. 我试图通过:verbose => true到 LDAP 构造函数,但没有效果......

require 'net/ldap'
require 'devise/strategies/authenticatable'

module Devise
  module Strategies
    class LdapAuthenticatable < Authenticatable
      def authenticate!
        if params[:user]
          ldap = Net::LDAP.new :host => 'XX.XX.XX.XX',
            :port => 636,
            :connect_timeout => 5,
            :base => 'CN=Configuration,DC=internal,DC=XX,DC=XX',
            :encryption => {
                :method => :simple_tls
            },
            :auth => {
              :method => :simple,
              :username => ENV['LDAP_USER'],
              :password => ENV['LDAP_PASSWORD']
            }

          result = ldap.bind_as(:base => "OU=Users,OU=XX,DC=XX,DC=XX,DC=XX",
                              :filter => "(userPrincipalName=#{email})",
                              :password => password,
          )

          if result
            user = User.find_by(email: email)
            success!(user)
          else
            return fail(:invalid_login)
          end
        end
      end

      def email
        params[:user][:email]
      end

      def password
        params[:user][:password]
      end

    end
  end
end

Warden::Strategies.add(:ldap_authenticatable, Devise::Strategies::LdapAuthenticatable)

解决了

原来是 ENV 变量没有被读取。

标签: ruby-on-railsrubyldap

解决方案


也许该帐户未授权?听起来问题出在绑定配置中:base => "OU=Users,OU=XX,DC=XX,DC=XX,DC=XX"

来自遇到此错误的其他用户的更多信息:


推荐阅读