我试图让Laravel 5.4使用LDAP而不是传统的数据库方法对用户进行身份验证。
已经有了一个库,Adldap2/Adldap2-Laravel。问题是这个库:
这在我的情况下不起作用,因为LDAP服务器中没有管理员,我不需要连接;只允许用户名使用该应用程序,没有任何角色。
如already existing issue所示,我使用以下代码修改了LoginController
类(出于测试目的,我尝试连接到此网页中提供的服务器:Online LDAP Test Server)
protected function attemptLogin(Request $request) {
$conn_settings = config('adldap.connections')[config('adldap_auth.connection')]['connection_settings'];
$credentials = $request->only(config('adldap_auth.usernames.eloquent'), 'password');
$user_format = env('ADLDAP_USER_FORMAT', 'uid=%s,' . $conn_settings['base_dn']);
$userdn = sprintf($user_format, $credentials[config('adldap_auth.usernames.eloquent')]);
$pass = $credentials['password'];
if(Adldap::auth()->attempt($userdn, $pass, $bindAsUser = true)) {
return true;
}
return false;
}
我现在遇到的问题是,一旦使用正确的凭据登录,我就会被发送回登录表单。凭据错误时,我收到相应的错误消息。
我的配置文件,以防万一:
.ENV:
ADLDAP_CONTROLLERS=ldap.forumsys.com
ADLDAP_BASEDN=dc=example,dc=com
ADLDAP_USER_FORMAT=uid=%s,dc=example,dc=com
adldap_auth.php:
[
'connection' => env('ADLDAP_CONNECTION', 'default'),
'provider' => Adldap\Laravel\Auth\DatabaseUserProvider::class,
'resolver' => Adldap\Laravel\Auth\Resolver::class,
'importer' => Adldap\Laravel\Auth\Importer::class,
'rules' => [
Adldap\Laravel\Validation\Rules\DenyTrashed::class,
// Adldap\Laravel\Validation\Rules\OnlyImported::class,
],
'scopes' => [
Adldap\Laravel\Scopes\UpnScope::class,
],
'usernames' => [
'ldap' => 'uid',
'eloquent' => 'username',
],
'login_fallback' => env('ADLDAP_LOGIN_FALLBACK', false),
'password_sync' => env('ADLDAP_PASSWORD_SYNC', true),
'windows_auth_attribute' => ['samaccountname' => 'AUTH_USER'],
'sync_attributes' => [
'username' => 'uid',
'name' => 'cn',
],
];
adldap.php:
return [
'connections' => [
'default' => [
'auto_connect' => false,
'connection' => Adldap\Connections\Ldap::class,
'schema' => 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,
],
],
],
]
答案 0 :(得分:2)
如果有人偶然发现这个问题,我找到了实现我想要的方法。分步指南如下:Howto: adminless LDAP authentification in Laravel。
基本上,在true
中返回LoginController::attemptLogin()
之前,必须使用Guard
在数据库中保存App\User
。