自定义登陆
<h1>自定义登陆</h1>
<h3>重写登陆页面和登陆逻辑</h3>
<p>方式一,重写登陆控制器方法:</p>
<p>默认的登陆控制器用的是<code>App\Admin\AuthController</code>这个类,可以通过配置参数<code>admin.auth.controller</code>进行修改</p>
<pre><code class="language-php">&lt;?php
namespace App\Admin\Controllers;
use Dcat\Admin\Controllers\AuthController as BaseAuthController;
class AuthController extends BaseAuthController
{
// 自定义登陆view模板
protected $view = 'admin.login';
// 重写你的登陆页面逻辑
public function getLogin(Content $content)
{
...
}
...
}
</code></pre>
<p>方式二,覆写路由:</p>
<p>在路由文件<code>app/Admin/routes.php</code>中,覆盖掉登陆页面和登陆逻辑的路由,即可实现自定义的功能</p>
<pre><code class="language-php">Route::group([
'prefix' =&gt; config('admin.prefix'),
'namespace' =&gt; Admin::controllerNamespace(),
'middleware' =&gt; ['web', 'admin'],
], function (Router $router) {
$router-&gt;get('auth/login', 'AuthController@getLogin');
$router-&gt;post('auth/login', 'AuthController@postLogin');
});</code></pre>
<p>在自定义的路由器AuthController中的<code>getLogin</code>、<code>postLogin</code>方法里分别实现自己的登陆页面和登陆逻辑。</p>
<h3>重写laravel认证</h3>
<p>如果不使用<code>Dcat Admin</code>内置的认证登陆逻辑,可以参考下面的方式自定义登陆认证逻辑</p>
<p>首先要先定义一个<code>user provider</code>,用来获取用户身份, 比如<code>app/Providers/CustomUserProvider.php</code>:</p>
<pre><code class="language-php">&lt;?php
namespace App\Providers;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;
class CustomUserProvider implements UserProvider
{
public function retrieveById($identifier)
{}
public function retrieveByToken($identifier, $token)
{}
public function updateRememberToken(Authenticatable $user, $token)
{}
public function retrieveByCredentials(array $credentials)
{
// 用$credentials里面的用户名密码去获取用户信息,然后返回Illuminate\Contracts\Auth\Authenticatable对象
}
public function validateCredentials(Authenticatable $user, array $credentials)
{
// 用$credentials里面的用户名密码校验用户,返回true或false
}
}
</code></pre>
<p>在方法<code>retrieveByCredentials</code>和<code>validateCredentials</code>中, 传入的<code>$credentials</code>就是登陆页面提交的用户名和密码数组,然后你可以使用<code>$credentials</code>去实现自己的登陆逻辑</p>
<p>Interface <code>Illuminate\Contracts\Auth\Authenticatable</code>的定义如下:</p>
<pre><code class="language-php">&lt;?php
namespace Illuminate\Contracts\Auth;
interface Authenticatable {
public function getAuthIdentifierName();
public function getAuthIdentifier();
public function getAuthPassword();
public function getRememberToken();
public function setRememberToken($value);
public function getRememberTokenName();
}</code></pre>
<p>上面interface每个方法的解释参考<a href="https://laravel.com/docs/5.5/authentication#adding-custom-user-providers">adding-custom-user-providers</a></p>
<p>定义好了<code>User provider</code>之后,打开<code>app/Providers/AuthServiceProvider.php</code>注册它:</p>
<pre><code class="language-php">&lt;?php
namespace App\Providers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* Register any application authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this-&gt;registerPolicies();
Auth::provider('custom', function ($app, array $config) {
// Return an instance of Illuminate\Contracts\Auth\UserProvider...
return new CustomUserProvider();
});
}
}</code></pre>
<p>最后修改一下配置,打开<code>config/admin.php</code>,找到<code>auth</code>部分修改:</p>
<pre><code class="language-php"> 'auth' =&gt; [
'guards' =&gt; [
'admin' =&gt; [
'driver' =&gt; 'session',
'provider' =&gt; 'admin',
]
],
// 修改下面
'providers' =&gt; [
'admin' =&gt; [
'driver' =&gt; 'custom',
]
],
],</code></pre>
<p>这样就完成了自定义登陆认证的逻辑,自定义登陆算是laravel中比较复杂的部分,需要开发者有耐心的一步步调试完成。</p>