首頁 > 後端開發 > php教程 > 20150720-Laravel登入驗證碰到的坑

20150720-Laravel登入驗證碰到的坑

WBOY
發布: 2016-08-08 09:20:37
原創
1718 人瀏覽過

記錄踩過的坑,以後的坑就會越來越少…

首先建表:

<code><span>php</span><span>artisan</span><span>migrate</span><span>:make_admin_table</span></code>
登入後複製

然後在新建的檔案裡面寫上表的各列設定

<code><span><?</span>php

use Illuminate<span>\</span>Database<span>\</span>Schema<span>\</span>Blueprint;
use Illuminate<span>\</span>Database<span>\</span>Migrations<span>\</span>Migration;

class CreateAdminTable extends Migration {

    <span>/**
     * Run the migrations.
     *
     * @return void
     */</span><span>public</span> function up()
    {
        Schema<span>::create</span>(<span>'admin'</span>, function(<span>$table</span>)
        {
            <span>$table</span><span>-></span>increments(<span>'id'</span>);
            <span>$table</span><span>-></span><span>string</span>(<span>'staff_code'</span>, <span>32</span>)<span>-></span>nullable();           <span>//员工号</span><span>$table</span><span>-></span><span>string</span>(<span>'login_name'</span>, <span>32</span>)<span>-></span>nullable();           <span>//登录名</span><span>$table</span><span>-></span><span>string</span>(<span>'password'</span>, <span>32</span>)<span>-></span>nullabele();            <span>//登录密码</span><span>$table</span><span>-></span><span>string</span>(<span>'mail'</span>, <span>512</span>)<span>-></span>nullable();                <span>//电子邮箱</span><span>$table</span><span>-></span><span>string</span>(<span>'staff_name'</span>, <span>32</span>)<span>-></span>nullable();           <span>//员工姓名</span><span>$table</span><span>-></span><span>string</span>(<span>'sex'</span>, <span>10</span>)<span>-></span>nullable();                  <span>//性别</span><span>$table</span><span>-></span><span>string</span>(<span>'belong_to'</span>, <span>512</span>)<span>-></span>nullable();           <span>//所属部门</span><span>$table</span><span>-></span><span>string</span>(<span>'jobs'</span>, <span>512</span>)<span>-></span>nullable();                <span>//岗位</span><span>$table</span><span>-></span><span>string</span>(<span>'telephone'</span>, <span>32</span>)<span>-></span>nullable();            <span>//固定电话</span><span>$table</span><span>-></span><span>string</span>(<span>'mobile'</span>, <span>32</span>)<span>-></span>nullable();               <span>//手机号</span>
        });
    }

    <span>/**
     * Reverse the migrations.
     *
     * @return void
     */</span><span>public</span> function down()
    {
        Schema<span>::dropIfExists</span>(<span>'admin'</span>);
    }

}</code>
登入後複製

建立Model模型Admin:

<code>php ratisan <span>generate</span> modle Admin</code>
登入後複製

再在生成的文件裡添加

<code><span><?php</span><span>use</span><span>Illuminate</span>\<span>Auth</span>\<span>UserTrait</span>;
<span>use</span><span>Illuminate</span>\<span>Auth</span>\<span>UserInterface</span>;
<span>use</span><span>Illuminate</span>\<span>Auth</span>\<span>Reminders</span>\<span>RemindableTrait</span>;
<span>use</span><span>Illuminate</span>\<span>Auth</span>\<span>Reminders</span>\<span>RemindableInterface</span>;

<span><span>class</span><span>Admin</span><span>extends</span> \<span>Eloquent</span><span>implements</span><span>UserInterface</span>, <span>RemindableInterface</span> {</span><span>use</span><span>UserTrait</span>, <span>RemindableTrait</span>;

    <span>protected</span><span>$fillable</span> = [];

    <span>protected</span><span>$table</span> = <span>'admin'</span>; <span>// 指定表名</span><span>protected</span><span>$primaryKey</span> = <span>'id'</span>; <span>// 指定主键名</span><span>protected</span><span>$hidden</span> = <span>array</span>(<span>'password'</span>);  <span>//密码字段</span><span>public</span><span>$timestamps</span> = <span>false</span>; <span>// 关闭 创建时间 与 更新时间 的自动维护</span><span>public</span><span><span>function</span><span>getRememberToken</span><span>()</span>{</span><span>return</span><span>$this</span>->rememberToken ;
    }

    <span>public</span><span><span>function</span><span>setRememberToken</span><span>(<span>$value</span>)</span>{</span><span>$this</span>->rememberToken = <span>$value</span> ;
    }

    <span>public</span><span><span>function</span><span>getRememberTokenName</span><span>()</span>{</span><span>return</span><span>$this</span>->reminder ;
    }
}</span></code>
登入後複製

解釋一下,因為需要做登錄驗證,用的是laravel自帶的Auth所以需要添加use和繼承UserInterface以及RemindableInterface接口並重寫一些方法
具體的就是這幾句

<code><span>use</span><span>Illuminate</span>\<span>Auth</span>\<span>UserTrait</span>;
<span>use</span><span>Illuminate</span>\<span>Auth</span>\<span>UserInterface</span>;
<span>use</span><span>Illuminate</span>\<span>Auth</span>\<span>Reminders</span>\<span>RemindableTrait</span>;
<span>use</span><span>Illuminate</span>\<span>Auth</span>\<span>Reminders</span>\<span>RemindableInterface</span>;

<span><span>class</span><span>Admin</span><span>extends</span> \<span>Eloquent</span><span>implements</span><span>UserInterface</span>, <span>RemindableInterface</span> {</span><span>use</span><span>UserTrait</span>, <span>RemindableTrait</span>;
    <span>/*******
    以下代码省略
    *******/</span><span>public</span><span><span>function</span><span>getRememberToken</span><span>()</span>{</span><span>return</span><span>$this</span>->rememberToken ;
    }

    <span>public</span><span><span>function</span><span>setRememberToken</span><span>(<span>$value</span>)</span>{</span><span>$this</span>->rememberToken = <span>$value</span> ;
    }

    <span>public</span><span><span>function</span><span>getRememberTokenName</span><span>()</span>{</span><span>return</span><span>$this</span>->reminder ;
    }
    }</code>
登入後複製

然後我繼續找到Auth檔案的設定 修改一下需要用到的表
app/config/auth.php
找到以下欄位並修改成自己所指定的表

<code><span><?php</span><span>return</span><span>array</span>(

<span>'driver'</span> => <span>'eloquent'</span>, <span>//验证方式,有database和eloquent两种</span><span>'model'</span> => <span>'Admin'</span>, <span>//所使用的model名</span><span>'table'</span> => <span>'admin'</span>, <span>//对应的表名</span><span>'reminder'</span> => <span>array</span>(

        <span>'email'</span> => <span>'emails.auth.reminder'</span>,

        <span>'table'</span> => <span>'password_reminders'</span>,

        <span>'expire'</span> => <span>60</span>,

    ),

);</span></code>
登入後複製

然後再加入controller方法:

<code><span>//获取登录页面</span><span>public</span> function get_web_login(){

        <span>return</span> View<span>::make</span>(<span>'web.web_login'</span>);

    }

    <span>//登录验证</span><span>public</span> function post_login(){
        <span>if</span> (Auth<span>::attempt</span>(<span>array</span>(<span>'login_name'</span><span>=></span>Input<span>::get</span>(<span>'login_name'</span>), <span>'password'</span><span>=></span>Input<span>::get</span>(<span>'password'</span>)))) {

            Notification<span>::success</span>(<span>'登录成功'</span>);

            <span>return</span> Redirect<span>::to</span>(<span>'/web/index'</span>)
            <span>-></span><span>with</span>(<span>'message'</span>, <span>'成功登录'</span>);
        } <span>else</span> {

            Notification<span>::warning</span>(<span>'用户名密码不正确'</span>);

            <span>return</span> Redirect<span>::to</span>(<span>'/web/login'</span>)
            <span>-></span><span>with</span>(<span>'message'</span>, <span>'用户名密码不正确'</span>)
                <span>-></span>withInput();
        }

    }</code>
登入後複製

然後是視圖檔login.blade.php:

<code><span>@section</span>(<span>'title'</span>)登录 - <span>@parent</span><span>@stop</span><span>@section</span>(<span>'nav_1'</span>)
    <li <span>class</span>=<span>"active"</span>><a href=<span>"#"</span>>登录</a></li>
<span>@stop</span><span>@section</span>(<span>'selection'</span>)
    <div id=<span>"login"</span><span>class</span>=<span>"login"</span>>
        <form <span>class</span>=<span>"form"</span> role=<span>"form"</span> action=<span>"{{URL::route('web.web_login.post')}}"</span> style=<span>"width: 500px"</span> method=<span>"post"</span>>
            <span>@if</span> (Session::has(<span>'message'</span>))

                <div <span>class</span>=<span>"alert alert-error"</span>>{{ Session::get(<span>'message'</span>)}}</div>

            <span>@endif</span>
            <div <span>class</span>=<span>"form-group"</span>>
                <label <span>for</span>=<span>"login_name"</span>>登录名:</label>
                <input <span>type</span>=<span>"text"</span><span>class</span>=<span>"form-control"</span> id=<span>"login_name"</span> name=<span>"login_name"</span>>
                <label <span>for</span>=<span>"password"</span>>密码:</label>
                <input <span>type</span>=<span>"password"</span><span>class</span>=<span>"form-control"</span> id=<span>"password"</span> name=<span>"password"</span>>

            </div>
            <div align=<span>"left"</span>>
                <button <span>type</span>=<span>"submit"</span><span>class</span>=<span>"btn btn-info btn-lg"</span>><span <span>class</span>=<span>"glyphicon glyphicon-user"</span> aria-hidden=<span>"true"</span>></span>  登录</button>


            </div>
        </form>
    </div>
<span>@stop</span></code>
登入後複製

最後更新路由

<code>Route::get(<span>'/web/index'</span>, <span>array</span>(<span>'as'</span> => <span>'web.web_index'</span>, <span>'uses'</span> => <span>'App\Controllers\Api\WebController@get_web_index'</span>));
<span>//登录页面</span>
Route::get(<span>'/web/login'</span>, <span>array</span>(<span>'as'</span> => <span>'web.web_login'</span>, <span>'uses'</span> => <span>'App\Controllers\Api\WebController@get_web_login'</span>));
Route::post(<span>'/web/login'</span>, <span>array</span>(<span>'as'</span> => <span>'web.web_login.post'</span>, <span>'uses'</span> => <span>'App\Controllers\Api\WebController@post_login'</span>));
</code>
登入後複製

<code><span>/**
         * Attempt to authenticate a user using the given credentials.
         *
         *<span> @param</span> array $credentials
         *<span> @param</span> bool $remember
         *<span> @param</span> bool $login
         *<span> @return</span> bool 
         *<span> @static</span>         */</span><span>public</span><span>static</span> function <span>attempt</span>($credentials = array(), $remember = <span>false</span>, $login = <span>true</span>){
            <span>return</span> \Illuminate\Auth\Guard::attempt($credentials, $remember, $login);
        }</code>
登入後複製
顛屁顛的打開資料庫隨便塞了條用戶資料進去,就嘗試登錄,然後問題就來了

不管我怎麼試,帳戶密碼就是不對


百度google了一下,然而並沒有找到什麼結果
無奈之下只能看看laravel的源碼

首先呼叫的就是attempt方法來驗證使用者名稱密碼於是我跳轉進這個函數去看了下

<code><span>/**
     * Attempt to authenticate a user using the given credentials.
     *
     * @param  array  $credentials
     * @param  bool   $remember
     * @param  bool   $login
     * @return bool
     */</span><span>public</span> function attempt(<span>array</span><span>$credentials</span><span>=</span><span>array</span>(), <span>$remember</span><span>=</span><span>false</span>, <span>$login</span><span>=</span><span>true</span>)
    {
        <span>$this</span><span>-></span>fireAttemptEvent(<span>$credentials</span>, <span>$remember</span>, <span>$login</span>);

        <span>$this</span><span>-></span>lastAttempted <span>=</span><span>$user</span><span>=</span><span>$this</span><span>-></span>provider<span>-></span>retrieveByCredentials(<span>$credentials</span>);

        <span>// If an implementation of UserInterface was returned, we'll ask the provider</span><span>// to validate the user against the given credentials, and if they are in</span><span>// fact valid we'll log the users into the application and return true.</span><span>if</span> (<span>$this</span><span>-></span>hasValidCredentials(<span>$user</span>, <span>$credentials</span>))
        {
            <span>if</span> (<span>$login</span>) <span>$this</span><span>-></span>login(<span>$user</span>, <span>$remember</span>);

            <span>return</span><span>true</span>;
        }

        <span>return</span><span>false</span>;
    }</code>
登入後複製
不難看出她只是返回的Guar::attempt方法的結果,那麼我繼續進去看

<code><span>/**
     * Determine if the user matches the credentials.
     *
     *<span> @param</span>  mixed  $user
     *<span> @param</span>  array  $credentials
     *<span> @return</span> bool
     */</span><span>protected</span> function <span>hasValidCredentials</span>($user, $credentials)
    {
        <span>return</span> ! is_null($user) && $<span>this</span>->provider->validateCredentials($user, $credentials);
    }</code>
登入後複製
到了這裡,大概就可以知道,登入結果,應該就是那個hasValidCredentials方法回傳的結果來控制的,那麼它內部又是怎麼實現的呢?再進去看看

<code><span>/**
     * Validate a user against the given credentials.
     *
     *<span> @param</span>  \Illuminate\Auth\UserInterface  $user
     *<span> @param</span>  array  $credentials
     *<span> @return</span> bool
     */</span><span>public</span> function <span>validateCredentials</span>(UserInterface $user, array $credentials);</code>
登入後複製
然而這裡只是做了個簡單的判斷是否存在$user參數,所以我繼續轉進validateCredentials方法

<code><span>/**
     * Validate a user against the given credentials.
     *
     *<span> @param</span>  \Illuminate\Auth\UserInterface  $user
     *<span> @param</span>  array  $credentials
     *<span> @return</span> bool
     */</span><span>public</span> function <span>validateCredentials</span>(UserInterface $user, array $credentials)
    {
        $plain = $credentials[<span>'password'</span>];

        <span>return</span> $<span>this</span>->hasher->check($plain, $user->getAuthPassword());
    }</code>
登入後複製

到了這裡初學laravel的我並不能看懂是啥意思,所以只能繼續google,然後真的找到了一些相關的情報
擴充Auth功能
看完這篇貼文之後,加上上面程式碼的理解
順著它說的目錄
/vender/laravel/framework/src/illuminate/Auth
於是我找到了EloquentUserProvider.php這個檔案 在其內部找到了validateCredentials方法的具體實現

<code>Hash::make(<span>"<span>$passowrd</span>"</span>);</code>
登入後複製
這下就清楚了

laravel在驗證密碼的時候會把輸入的密碼用hash運算之後再與資料庫所儲存的密碼對比


然而我是直接在在資料庫中加入明文密碼的,所以顯示密碼不正確是理所當然的

所以,在儲存密碼欄位的時候,務必記得用

rrreee
來產生對應密碼的hash字串…
然後我在用這個方法寫入密碼hash字串的時候報錯了,檢查一看,原來是當初設定的密碼欄位太短導致的,於是把密碼欄位長度改為1024個字長 問題就解決了
這個坑折騰了我一上午…記錄下來讓大家參考參考,避免再次像我一樣被坑吧_ (:з”∠)_

另外還找到了一篇bolg,說是怎樣將laravel的預設的加密方法換成自訂的MD5加密方式的以後應該會用到吧,貼在這裡以供日後參考laravel更改預設的登入密碼加密方式

(完)

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。

以上就介紹了20150720-Laravel登入驗證碰到的坑,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

🎜
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板