当前位置: 首页 > 知识库问答 >
问题:

Laravel多重身份验证md5和主键字符串

黄弘盛
2023-03-14

目前我有一个项目需要使用扩展mysql数据库,它有很多数据和密码是md5加密的,MEMBER_ID(主键)是这样的字符串

5e3bb0bdb70e4f139c132cc32daace0e

密码字段名password和主键名MEMBER\u 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('成员')-

Auth::guard('成员')-

Auth::guard('成员')-

我不能更改密码加密方法,我不能向成员表添加自动递增字段或编辑主键,因为另一个网站维护人员说这可能会导致他的JAVA应用程序错误,所以在不更改任何表结构的情况下有什么想法?

更新:

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

我做了定制认证,最后认证::警卫(“成员”)-

但一旦页面刷新或转到另一个页面/路由,Auth::guard('member')就会-

更新2:

我尝试在索引处转储dd(\Session::all()),发现会话被清除,但当我尝试在Auth::guard('member')之后转储dd(\Session::all())时-

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

为什么会议被清除了?

共有1个答案

巫马磊
2023-03-14

我找到了解决方案,因为会话未启动

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

App/Http/routes.php

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

app/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,
    ];
}

app/Http/Middleware/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);
    }
}
 类似资料:
  • 我的管理员控制器扩展了控制器类,在那里我使用方法加载视图页面并传递保护。config/auth.php也通过添加管理员保护和提供程序进行了修改。在app/Actions/Fortify文件夹中,我添加了AttemptToAuthenticate和ReDirectIfTwoFactorAuthenticate类,并更改了命名空间。我的管理员控制器还扩展了Guard。用户的重定向中间件和管理员的重定向

  • 在我的LaravelV5.4项目中,我有两个模型:用户和管理员。 在config/auth.php中,我向警卫和提供者添加了admin,如下所示: 现在在AdminController类中,我想使用Auth::true函数,但默认情况下它使用用户表。我可以在config/auth.php中更改默认值,如下所示,它可以工作,但在这种情况下,我不能为用户使用auth::trunt。 我想将用户设置为默

  • 我正在laravel中创建一个多身份验证系统,其中有两种类型的用户:管理员(由我创建)和用户(使用本地laravel身份验证系统)。 如果我以用户身份登录,当我尝试访问登录页面时,当我已经登录,它会将我重定向回仪表板,但如果我以管理员身份登录,当我再次访问管理员登录页面时,它会让我再次登录,尽管已经作为管理员登录。 以下是我的重定向身份验证类代码: 有人能解释一下发生了什么事吗?

  • 我正在创建一个laravel应用程序与自定义多重身份验证。我正在跟随这篇文章进行多重身份验证。https://pusher.com/tutorials/multiple-authentication-guards-laravel 我已经创建了登录和注册控制器定义的警卫和提供者,一切正常,我能够注册用户并登录他们。我写了一页(http://127.0.0.1:8000/admin)只有当管理员登录时

  • 遵循这个指南:https://www.youtube.com/watch?v=bqkt6eSsRZs 添加了从laravel docs的auth路由 创建登录/注册视图 创建 /home路线 自定义AuthController: 尝试访问身份验证/登录时,我遇到以下错误: 请求中出现错误异常。php第775行:。。未根据请求设置会话存储。 已将身份验证路由移动到中间件组。 成功注册,在数据库和会话

  • 我是新的(护照)在laravel。是否有可能使用 即使使用了laravel内置身份验证?