Laravel multiauth md5和主键字符串

时间:2016-01-05 07:22:50

标签: php mysql laravel laravel-5 laravel-5.2

目前我有1个项目需要使用扩展mysql数据库,其中有大量数据和密码是md5加密而MEMBER_ID(主键)是这样的字符串

  

5e3bb0bdb70e4f139c132cc32daace0e

密码字段名称密码,主键名称 MEMBER_ID

所以我设置了这样的雄辩模型

<?php

namespace App\Models;

use Auth;
use Illuminate\Auth\Authenticatable;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;

class Member extends BaseModels implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable;

    protected $connection = 'mysql_db2';

    protected $table = 'tb_member';

    protected $primaryKey = 'MEMBER_ID';

    protected $fillable = [];

    public $incrementing = false;

    protected $hidden = ['PASSWORD'];

    public function getAuthIdentifier() {
        return $this->MEMBER_ID;
    }

    public function setPASSWORDAttribute($value) {
        $this->attributes['PASSWORD'] = static::encryptPassword($value);
    }

    public function setPassword($password) {dd('set');
        $this->PASSWORD = static::encryptPassword($password);
    }

    public function getAuthPassword() {dd('get');
        return $this->PASSWORD;
    }

    public static function encryptPassword($password) {
        return md5($password);
    }

    public function checkLogin($username, $password) {
        if ($this->USERNAME == $username && $this->PASSWORD == static::encryptPassword($password)) {
            return true;
        } else {
            return false;
        }
    }
}

我搜索并发现一些帖子提及添加 getAuthPassword() setPassword()方法,但是我在那里转储它也通过了,所以我认为是在laravel 5.2中弃用了吗?

这是我的控制器

<?php

namespace App\Http\Controllers\Auth;

use App\Models\Member;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;
use Auth;

class MemberController extends Controller {
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    public function login(Request $request) {
        $username = $request->get('username');
        $password = $request->get('password');

        try {
            $member = Member::where('USERNAME', '=', $username)->first();
            dd(Auth::guard('member')->attempt([
                'USERNAME' => $username,
                'PASSWORD' => $password
            ]));
            if ($member) {
                if ($member->checkLogin($username, $password)) {

                    Auth::guard('member')->login($member);
                    if (Auth::guard('member')->check()) {
//                        dd(Auth::guard('member')->check());
                        return redirect()->route('frontend.index');
                    } else {
                        return redirect()->route('frontend.index')->withErrors();
                    }
                } else {
                    return 'Incorrect password';
                }
            } else {
                return 'Username not found';
            }
            $d = new Member();
            $d->PASSWORD = 123456;

            $credentials = [
                'USERNAME' => $username,
                'PASSWORD' => $password
            ];
            if (Auth::guard('member')->attempt($credentials)) {
                return 'successfully login';
            } else {
                return 'unable to login';
            }
            return 'hehe';
//            dd($username, $password);
        } catch (\Exception $e) {
            dd('FILE: ' . $e->getfile() . 'LINE: ' . $e->getLine() . '<br />' . $e->getMessage());
        }
    }
}

Auth :: guard(&#39;成员&#39;) - &gt; attempt()始终返回false

Auth :: guard(&#39;成员&#39;) - &gt;登录($ member)始终返回null

Auth :: guard(&#39;会员&#39;) - &gt; check()在页面未刷新时返回true,但一旦刷新(或转到其他路径/页面) Auth :: guard(&#39;会员&#39;) - &gt; check()将返回false

我无法更改密码加密方法,我无法添加自动增量字段或编辑主键到成员表,因为另一个网站维护者说它可能会导致他的JAVA应用程序错误,所以任何想法都不改变任何表结构?< / p>

更新:

我找到了一些文章http://laravel.io/forum/11-04-2014-laravel-5-how-do-i-create-a-custom-auth-in-laravel-5

我做了自定义身份验证,最后 Auth :: guard(&#39;成员&#39;) - &gt;尝试返回true

但是一旦页面刷新或转到另一个页面/路线, Auth :: guard(&#39;成员&#39;) - &gt; check()将再次返回

更新2:

我尝试在索引处转储 dd(\ Session :: all()),我发现会话被清除,但是当我尝试转储 dd(\ Session :: all() ) Auth :: guard(&#39;会员&#39;) - &gt;尝试后,会话显示

array:1 [▼
  "login_member_59ba36addc2b2f9401580f014c7f58ea4e30989d" => "5e3bb0bdb70e4f139c132cc32daace0e"
]

为什么会话被清除?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,因为会话没有开始

https://laracasts.com/discuss/channels/laravel/auth-session-killed-after-redirect-laravel-52

应用程序/ HTTP / routes.php文件

//Frontend route
Route::group(array('namespace' => 'Frontend', 'middleware' => ['web']), function() {
    Route::get('/', ['as' => 'frontend.index', 'uses' => 'IndexController@index']);
});

应用程序/ HTTP / kernel.php

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'member' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],

        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        ],

        'api' => [
            'throttle:60,1',
        ],
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'web' => \App\Http\Middleware\Web::class,
    ];
}

应用程序/ HTTP /中间件/ Web.php

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class Web
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        return $next($request);
    }
}