Laravel 5 Adldap2 Laravel,我的逻辑在哪里

Laravel 5 Adldap2 Laravel,我的逻辑在哪里,laravel-5,vendor,adldap,Laravel 5,Vendor,Adldap,我需要使用Adldap2 Laravel。我的laravel应用程序基于laravel样板文件5 我看到该应用程序使用的唯一login()方法是:vendor\laravel\framework\src\illustration\Foundation\Auth\AuthenticatesUsers.php我知道它是一个供应商,但出于测试目的,我对公共函数login(Request$Request)方法进行了如下编辑: public function login(Request $request)

我需要使用Adldap2 Laravel。我的laravel应用程序基于laravel样板文件5

我看到该应用程序使用的唯一login()方法是:
vendor\laravel\framework\src\illustration\Foundation\Auth\AuthenticatesUsers.php
我知道它是一个供应商,但出于测试目的,我对
公共函数login(Request$Request)
方法进行了如下编辑:

public function login(Request $request){
    if (\Adldap::auth()->attempt(str_replace('@example.com', '', $request->email), $request->password)) { //THIS IS THE ONLY LINE THAT IA HAVE ADDED, THE REST OF THE CODE IS THE ORIGINAL ONE
       $this->validateLogin($request);
        if ($this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        if ($this->attemptLogin($request)) {
            return $this->sendLoginResponse($request);
        }
    }
    else {
        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }
}

这一切都很好,但我怎么能不碰这个供应商文件呢?我应该在哪里使用Adlap验证来实现if-else。

您可以使用默认的登录页面表单

此处介绍了所有要执行的步骤:

我会解释一切直到第一步,以防你遗漏了什么

1。安装Adldap2 Laravel

cd yourLaravalProjectFolder
composer require adldap2/adldap2-laravel
2。在
config/app.php

'providers' => [
    ...
    Adldap\Laravel\AdldapServiceProvider::class,
    Adldap\Laravel\AdldapAuthServiceProvider::class,
],

'aliases' => [
    ...
    'Adldap' => Adldap\Laravel\Facades\Adldap::class,
],
'providers' => [
    'users' => [
        'driver' => 'adldap', // was 'eloquent'
        'model'  => App\User::class,
    ],
],
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('username')->unique(); // was 'email'
        $table->string('password');
        $table->string('name'); // to be read from LDAP
        $table->string('phone'); // extra field to read from LDAP
        $table->rememberToken();
        $table->timestamps();
    });
}
protected $fillable = [
    // replace 'email' with 'username' and add 'phone'
    'name', 'username', 'password', 'phone',
];
发布Adldap:

php artisan vendor:publish --tag="adldap"
3。在
config/auth.php
中更改用户提供程序的驱动程序

'providers' => [
    ...
    Adldap\Laravel\AdldapServiceProvider::class,
    Adldap\Laravel\AdldapAuthServiceProvider::class,
],

'aliases' => [
    ...
    'Adldap' => Adldap\Laravel\Facades\Adldap::class,
],
'providers' => [
    'users' => [
        'driver' => 'adldap', // was 'eloquent'
        'model'  => App\User::class,
    ],
],
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('username')->unique(); // was 'email'
        $table->string('password');
        $table->string('name'); // to be read from LDAP
        $table->string('phone'); // extra field to read from LDAP
        $table->rememberToken();
        $table->timestamps();
    });
}
protected $fillable = [
    // replace 'email' with 'username' and add 'phone'
    'name', 'username', 'password', 'phone',
];
4。在
config/adldap.php

试图添加一个新连接,并保持默认值不变,但无效。Adldap2一直试图使用默认设置以管理员身份连接,因此我必须直接修改默认设置:

'connections' => [
    'default' => [
        'auto_connect' => false,
        'connection' => Adldap\Connections\Ldap::class,
        'schema' => Adldap\Schemas\OpenLDAP::class, // was Adldap\Schemas\ActiveDirectory::class
        'connection_settings' => [
            'account_prefix' => env('ADLDAP_ACCOUNT_PREFIX', ''),
            'account_suffix' => env('ADLDAP_ACCOUNT_SUFFIX', ''),
            'domain_controllers' => explode(' ', env('ADLDAP_CONTROLLERS', 'corp-dc1.corp.acme.org corp-dc2.corp.acme.org')),
            'port' => env('ADLDAP_PORT', 389),
            'timeout' => env('ADLDAP_TIMEOUT', 5),
            'base_dn' => env('ADLDAP_BASEDN', 'dc=corp,dc=acme,dc=org'),
            'admin_account_suffix' => env('ADLDAP_ADMIN_ACCOUNT_SUFFIX', ''),
            'admin_username' => env('ADLDAP_ADMIN_USERNAME', ''),
            'admin_password' => env('ADLDAP_ADMIN_PASSWORD', ''),
            'follow_referrals' => true,
            'use_ssl' => false,
            'use_tls' => false,
        ],
    ],
],
5。在
config/adldap\u auth.php中更改要同步的用户名和属性

此配置指定将每个登录用户的哪些字段从LDAP服务器复制到本地数据库

要同步的额外属性的一些示例可能是“角色”以控制对某些区域的访问,或“会话到期时间”以在一段时间后强制注销。我相信你能想到许多其他用途

测试LDAP服务器中可用的字段数量有限,因此我们将添加“phone”作为示例

'usernames' => [
    'ldap' => env('ADLDAP_USER_ATTRIBUTE', 'userprincipalname'), // was just 'userprincipalname'
    'eloquent' => 'username', // was 'email'
],

'sync_attributes' => [
    // 'field_in_local_db' => 'attribute_in_ldap_server',
    'username' => 'uid', // was 'email' => 'userprincipalname',
    'name' => 'cn',
    'phone' => 'telephonenumber',
],
6。在
.env
中配置LDAP和数据库连接

'providers' => [
    ...
    Adldap\Laravel\AdldapServiceProvider::class,
    Adldap\Laravel\AdldapAuthServiceProvider::class,
],

'aliases' => [
    ...
    'Adldap' => Adldap\Laravel\Facades\Adldap::class,
],
'providers' => [
    'users' => [
        'driver' => 'adldap', // was 'eloquent'
        'model'  => App\User::class,
    ],
],
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('username')->unique(); // was 'email'
        $table->string('password');
        $table->string('name'); // to be read from LDAP
        $table->string('phone'); // extra field to read from LDAP
        $table->rememberToken();
        $table->timestamps();
    });
}
protected $fillable = [
    // replace 'email' with 'username' and add 'phone'
    'name', 'username', 'password', 'phone',
];
仅供参考,机密配置(即API令牌或数据库密码)应存储在此文件中,Laravel默认将其包含在.gitignore中

ADLDAP_CONNECTION=default
ADLDAP_CONTROLLERS=ldap.forumsys.com 
ADLDAP_BASEDN=dc=example,dc=com
ADLDAP_USER_ATTRIBUTE=uid
ADLDAP_USER_FORMAT=uid=%s,dc=example,dc=com

DB_CONNECTION=sqlite  # was 'mysql'
DB_HOST=127.0.0.1     # remove this line
DB_PORT=3306          # remove this line
DB_DATABASE=homestead # remove this line
DB_USERNAME=homestead # remove this line
DB_PASSWORD=secret    # remove this line
7。更改
数据库/迁移/2014\u 10\u 12\u000000\u创建用户\u table.php

'providers' => [
    ...
    Adldap\Laravel\AdldapServiceProvider::class,
    Adldap\Laravel\AdldapAuthServiceProvider::class,
],

'aliases' => [
    ...
    'Adldap' => Adldap\Laravel\Facades\Adldap::class,
],
'providers' => [
    'users' => [
        'driver' => 'adldap', // was 'eloquent'
        'model'  => App\User::class,
    ],
],
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('username')->unique(); // was 'email'
        $table->string('password');
        $table->string('name'); // to be read from LDAP
        $table->string('phone'); // extra field to read from LDAP
        $table->rememberToken();
        $table->timestamps();
    });
}
protected $fillable = [
    // replace 'email' with 'username' and add 'phone'
    'name', 'username', 'password', 'phone',
];
8。删除文件
database/migrations/2014\u 10\u 12\u 100000\u create\u password\u resets\u table.php

'providers' => [
    ...
    Adldap\Laravel\AdldapServiceProvider::class,
    Adldap\Laravel\AdldapAuthServiceProvider::class,
],

'aliases' => [
    ...
    'Adldap' => Adldap\Laravel\Facades\Adldap::class,
],
'providers' => [
    'users' => [
        'driver' => 'adldap', // was 'eloquent'
        'model'  => App\User::class,
    ],
],
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('username')->unique(); // was 'email'
        $table->string('password');
        $table->string('name'); // to be read from LDAP
        $table->string('phone'); // extra field to read from LDAP
        $table->rememberToken();
        $table->timestamps();
    });
}
protected $fillable = [
    // replace 'email' with 'username' and add 'phone'
    'name', 'username', 'password', 'phone',
];
9。更改
app/User.php

'providers' => [
    ...
    Adldap\Laravel\AdldapServiceProvider::class,
    Adldap\Laravel\AdldapAuthServiceProvider::class,
],

'aliases' => [
    ...
    'Adldap' => Adldap\Laravel\Facades\Adldap::class,
],
'providers' => [
    'users' => [
        'driver' => 'adldap', // was 'eloquent'
        'model'  => App\User::class,
    ],
],
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('username')->unique(); // was 'email'
        $table->string('password');
        $table->string('name'); // to be read from LDAP
        $table->string('phone'); // extra field to read from LDAP
        $table->rememberToken();
        $table->timestamps();
    });
}
protected $fillable = [
    // replace 'email' with 'username' and add 'phone'
    'name', 'username', 'password', 'phone',
];
10运行迁移以创建用户表和验证脚手架

迁移之前,请确保数据库已配置且工作正常

touch database/database.sqlite
php artisan migrate
php artisan make:auth
最后一个命令安装了许多我们不需要的控制器和视图,所以让我们删除它们

11。删除这些文件和文件夹

 - app/Http/Controllers/Auth/ForgotPasswordController.php
 - app/Http/Controllers/Auth/RegisterController.php
 - app/Http/Controllers/Auth/ResetPasswordController.php
 - resources/views/auth/register.blade.php
 - resources/views/auth/passwords --> remove folder and all files inside
12。从resources/views/layouts/app.blade.php中删除此行

<li><a href="{{ route('register') }}">Register</a></li>
<a href="{{ url('/register') }}">Register</a>
<div class="form-group{{ $errors->has('username') ? ' has-error' : '' }}">
    <label for="username" class="col-md-4 control-label">Username</label>
    <div class="col-md-6">
        <input id="username" type="text" class="form-control" name="username" value="{{ old('username') }}" required autofocus>
        @if ($errors->has('username'))
            <span class="help-block">
                <strong>{{ $errors->first('username') }}</strong>
            </span>
        @endif
    </div>
</div>
运行网站

我们完了

不要忘记在本地测试
.env
文件中将web服务器端口设置为8000:

APP_URL=http://localhost:8000
让我们运行该网站并尝试登录

php artisan serve
访问
http://localhost:8000
在您喜爱的浏览器中

尝试访问
http://localhost:8000/home
在登录之前


希望它能帮助您。

Mm是的,答案是告诉我运行
php artisan Service
?我问过如何修改一些Adldap配置,但无论如何,它有很好的信息,谢谢。最后,我没有从AdLdap中接触任何内容,这是一个配置问题。