从Laravel7.x文档中,我试图为我的应用程序创建一个手动身份验证。文件显示如下:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
public function authenticate(Request $request)
{
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
// Authentication passed...
return redirect()->intended('dashboard');
}
}
}我有自己的用户桌:
sys_users
==========
user_acc character varying(20) NOT NULL, -- Primary Key
full_name character varying(300) NOT NULL,
is_suspended boolean DEFAULT FALSE,
CONSTRAINT PK_SysUsers PRIMARY KEY (user_acc),要登录,用户需要输入以下数据:
我尝试定制控制器一点(尚未完成):
class LoginController extends Controller
{
public function authenticate(Request $request)
{
$request->validate([
'username' => 'required',
'password' => 'required',
]);
$credentials = $request->only('username', 'password', 'module');
if (Auth::attempt($credentials)) {
return redirect()->route($request->module);
}
}
}我想在上面的定制代码中添加以下内容:(i)通过查询sys_users表来检查表中是否存在用户名,(ii)使用POP3检查密码(我已经准备好了phpmailer库和代码),(iii)通过查询我准备的另一个表来检查用户是否可以访问这些模块。
问题是:
Auth类的attempt方法中,但是我似乎找不到假定的方法。这些文件非常缺乏,没有提供详细的解释。Auth类中修改attempt方法,我应该如何进行身份验证过程?发布于 2020-07-24 09:37:08
您想要做的是实现您自己的用户提供者,使用外部POP3电子邮件系统验证您的用户凭据。
我不认为您需要自定义您的卫队,因为我假设您仍然希望默认SessionGuard的SessionGuard在会话中检查和存储有关身份验证状态的信息。
我认为,除了如何验证所提供的凭据之外,您可能还需要所有其他默认行为。
也许您可以在./app/Providers/文件夹中创建:
PopThreeUserProvider.php
namespace App\Providers;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
class PopThreeUserProvider extends EloquentUserProvider
{
/**
* Validate a user against the given credentials.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(UserContract $user, array $credentials)
{
// Implement your pop3 authentication here, I have left the default code
// for the Eloquent provider below
$plain = $credentials['password'];
return $this->hasher->check($plain, $user->getAuthPassword());
}
}现在在你的./app/Providers/AuthServiceProvider.php
class AuthServiceProvider extends ServiceProvider
{
...
/**
* Register any application authentication / authorization services.
*
* @return void
*/
public function boot()
{
...
Auth::provider('pop3', function ($app, array $config) {
return new PopThreeUserProvider($app->make('hash'), $config['model']);
});
...
}
...
}现在在你的config/auth.php
...
'providers' => [
'users' => [
'driver' => 'pop3',
],
],
...当然,这一切都假定您拥有:
class User extends Authenticatable
{
protected $table = 'sys_users';
protected $primaryKey = 'user_acc';
protected $incrementing = false;
...https://stackoverflow.com/questions/63068549
复制相似问题