【问题标题】:Laravel LDAP (Adldap2) Can't authenticate,username is null into guard->attemptLaravel LDAP(Adldap2)无法验证,用户名为空进入保护->尝试
【发布时间】:2020-06-16 19:31:48
【问题描述】:

早上, 我不断收到消息“必须指定用户名”。尝试登录我的应用程序时。 连接到 LDAP 没问题,也同步,在我的数据库/表用户中,我看到所有带有密码的用户名。 但是不能用任何人的用户名登录。 试图 dd($username) 进入 LoginController,“guard()->attempt”显示“null”。 感谢您的帮助!

我的版本

Laravel Version: ^7.0
Adldap2-Laravel Version: ^6.1
PHP Version: ^7.2.5
LDAP Type: OpenLDAP

我的 .env

LDAP_HOSTS=ldap.forumsys.com
LDAP_BASE_DN=dc=example,dc=com
LDAP_USERNAME=cn=read-only-admin,dc=example,dc=com
LDAP_PASSWORD=password
LDAP_PASSWORD_SYNC=true

我的 ldap.php

<?php

return [

    'logging' => env('LDAP_LOGGING', false),

     'connections' => [

        'default' => [

                  'auto_connect' => env('LDAP_AUTO_CONNECT', true),

                  'connection' => Adldap\Connections\Ldap::class,

               'settings' => [

               'schema' => Adldap\Schemas\OpenLDAP::class, 

               'account_prefix' => env('LDAP_ACCOUNT_PREFIX', ''),

               'account_suffix' => env('LDAP_ACCOUNT_SUFFIX', ''),

              'hosts' => explode(' ', env('LDAP_HOSTS', 'corp-dc1.corp.acme.org corp-dc2.corp.acme.org')),

               'port' => env('LDAP_PORT', 389),

               'timeout' => env('LDAP_TIMEOUT', 5),

                'base_dn' => env('LDAP_BASE_DN', 'dc=corp,dc=acme,dc=org'),

                'username' => env('LDAP_USERNAME', 'username'),
                'password' => env('LDAP_PASSWORD', 'secret'),

                'follow_referrals' => false,

                'use_ssl' => env('LDAP_USE_SSL', false),
                'use_tls' => env('LDAP_USE_TLS', false),

            ],

        ],

    ],

];

我的 ldap_auth

<?php

return [

     'connection' => env('LDAP_CONNECTION', 'default'),

    'provider' => Adldap\Laravel\Auth\DatabaseUserProvider::class,

    'model' => App\User::class,

    'rules' => [

        // Denys deleted users from authenticating.

        Adldap\Laravel\Validation\Rules\DenyTrashed::class,

        // Allows only manually imported users to authenticate.

        // Adldap\Laravel\Validation\Rules\OnlyImported::class,

    ],

    'scopes' => [

        // Only allows users with a user principal name to authenticate.
        // Suitable when using ActiveDirectory.
        // Adldap\Laravel\Scopes\UpnScope::class,

        // Only allows users with a uid to authenticate.
        // Suitable when using OpenLDAP.
        // Adldap\Laravel\Scopes\UidScope::class,

    ],

    'identifiers' => [

        'ldap' => [

            'locate_users_by' => 'uid',

            'bind_users_by' => 'distinguishedname',

        ],

        'database' => [

         'guid_column' => 'objectguid',

            'username_column' => 'username', //'email',

        ],

        'windows' => [

            'locate_users_by' => 'samaccountname',

            'server_key' => 'AUTH_USER',

        ],

    ],

    'passwords' => [

        'sync' => env('LDAP_PASSWORD_SYNC', false),

        'column' => 'password',

    ],

    'login_fallback' => env('LDAP_LOGIN_FALLBACK', false),

    'sync_attributes' => [

        //'email' => 'userprincipalname',

        'username' => 'uid', 

        'name' => 'cn',

    ],

    'logging' => [

        'enabled' => env('LDAP_LOGGING', true),

        'events' => [

            \Adldap\Laravel\Events\Importing::class                 => \Adldap\Laravel\Listeners\LogImport::class,
            \Adldap\Laravel\Events\Synchronized::class              => \Adldap\Laravel\Listeners\LogSynchronized::class,
            \Adldap\Laravel\Events\Synchronizing::class             => \Adldap\Laravel\Listeners\LogSynchronizing::class,
            \Adldap\Laravel\Events\Authenticated::class             => \Adldap\Laravel\Listeners\LogAuthenticated::class,
            \Adldap\Laravel\Events\Authenticating::class            => \Adldap\Laravel\Listeners\LogAuthentication::class,
            \Adldap\Laravel\Events\AuthenticationFailed::class      => \Adldap\Laravel\Listeners\LogAuthenticationFailure::class,
            \Adldap\Laravel\Events\AuthenticationRejected::class    => \Adldap\Laravel\Listeners\LogAuthenticationRejection::class,
            \Adldap\Laravel\Events\AuthenticationSuccessful::class  => \Adldap\Laravel\Listeners\LogAuthenticationSuccess::class,
            \Adldap\Laravel\Events\DiscoveredWithCredentials::class => \Adldap\Laravel\Listeners\LogDiscovery::class,
            \Adldap\Laravel\Events\AuthenticatedWithWindows::class  => \Adldap\Laravel\Listeners\LogWindowsAuth::class,
            \Adldap\Laravel\Events\AuthenticatedModelTrashed::class => \Adldap\Laravel\Listeners\LogTrashedModel::class,

        ],
    ],

];

我的登录控制器

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function username()
    {
        return 'username';
    }
}

【问题讨论】:

    标签: laravel authentication ldap openldap adldap


    【解决方案1】:

    我自己也经历过这种痛苦。

    您似乎正在使用您的服务帐户连接 LDAP,但我看不到您将经过身份验证的用户登录到 Laravel 的位置。您的用户是否必须在某处的屏幕上提供用户名和密码?如果没有,你如何获取用户并将他们登录到 Laravel?

    我在必须通过 LDAP 的企业中这样做的方式是拥有正常的 Laravel 登录页面,但是 登录控制器 中的中间方法通过服务向 LDAP 发送消息与 PW 的帐户。如果成功,则使用 Laravel 的标准方法登录。基本上只需根据 LDAP 检查 PW,然后登录,而不是检查 Laravel DB。

    示例代码 - 这会千差万别,但可以让您了解可能的工作原理:

      if(env('LOGIN', false) === 'LDAP'){
            $ldap = new \App\Http\Controllers\ClientSpecific\BaseLDAPController();
            $username = $request->input('username');
            if($ldap->authenticate($username, $request->input('password'))){
                return $this->sendLoginResponse($request);
            }
        }else {
            if ($this->guard()->attempt($credentials, $request->has('remember'))) {
                return $this->sendLoginResponse($request);
            }
        }
    

    【讨论】:

    • 感谢@watercayman 的回复。我使用 laravel 标准视图(login.blade.php)连接用户名和密码。我只是用“用户名”将“电子邮件”替换为该视图。我还使用 laravel 标准功能登录在我的数据库同步后进行身份验证。此同步的结果(php artisan adlap:import)是“成功”。
    • 早上,当我尝试登录时,请参阅下面的 larevel_log:[2020-06-17 13:09:17] local.INFO:已成功找到用户“Isaac Newton”进行身份验证。 [2020-06-17 13:09:17] local.INFO:正在同步用户“Isaac Newton”。 [2020-06-17 13:09:17] local.INFO:用户“艾萨克牛顿”已成功同步。 [2020-06-17 13:09:17] local.INFO:用户 'Isaac Newton' 正在使用用户名进行身份验证:'' [2020-06-17 13:09:17] local.ERROR:必须指定用户名。 {"exception":"[object] (Adldap\\Auth\\UsernameRequiredException(code: 0): 必须指定用户名。
    • 我手动完成了所有这些 - 我也遇到了 ldap 包的问题。对我来说,我连接然后手动绘制用户名,如您在上面的代码中看到的 - 我从$request 得到它。正如我在上面的答案中提到的,您的日志显示 ldap 正在使用正确的 AD USER,但在 Laravel USERNAME 上没有句柄。 (或者可能是相反的,我不知道日志)。无论哪种方式,您的 AD 服务帐户和您的 Laravel 用户之间都缺少一个链接。查看sendLoginResponse() 方法中输入的任何内容 - 如果这是空白,您就知道问题出在哪里。
    • 感谢@watercayman 帮助我。在我的登录控制器的许多 dd 之后,我注意到 Auth\guard->attempt 函数到 adlap2 和 Auth\guard->attempt 函数到照亮(外观)之间没有链接。这是因为 adlap2 返回空。那么如何在这些之间建立联系以发挥作用呢?
    • 是的 - 这就是我的想法。您设置 adlap2 的方式有问题。我只能告诉你问题的答案(上图)——你需要返回包说明,查看该链接的位置、安装是否正确、是否出现故障等。有很多地方你可能错过了接线。既然您知道问题出在哪里,我认为您必须牢记这一点回到说明并尝试找出缺少的内容。抱歉 - 我知道什么是痛苦的广告。
    【解决方案2】:

    我知道这是一个老问题,但即使在 2020 年我仍然面临这个问题,而且我花了很多时间。实际上,如果您还尝试使用 Adldap2-Laravel 包将 OpenLDAP 服务器与您的 Laravel 应用程序绑定,那么这个问题非常愚蠢。 Adldap2-Laravel 默认为 Microsoft 的 Active Directory 配置。但是对于 OpenLDAP,我们需要正确更改 Identifiers 数组如下..

    <?php
    
    return [
    
         // configurations settings...
    
        'identifiers' => [
    
            'ldap' => [
    
                'locate_users_by' => 'uid', // changed from userprincipalname
    
                'bind_users_by' => 'dn', // changed from distinguishedname
    
            ],
    
            'database' => [
    
             'guid_column' => 'objectguid',
    
                'username_column' => 'username', //'email',
    
            ],
    
            'windows' => [
    
                'locate_users_by' => 'samaccountname',
    
                'server_key' => 'AUTH_USER',
    
            ],
    
        ],
    
        // rest of the configurations...
    
    ];
    

    如果我的答案对于新手来说不清楚,请告诉我,因为我也是新手。我会尝试更好地解释解决方案:)

    【讨论】:

      猜你喜欢
      • 2017-02-10
      • 1970-01-01
      • 2022-10-03
      • 1970-01-01
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      相关资源
      最近更新 更多