dsideal2015 2014-12-10 03:19
浏览 103
已采纳

如何使Auth :: Attempt在DB中使用未加密的密码

I am having similar problem to most people with Auth:Attempt. However, I could not find one documentation where I could use existing table with using migration tool to create a new users table.

My application details

  1. I am using existing table to keep track of users and not using migration tool to generate users table.
  2. passwords in database is not hashed. I have separate page to add new users and store them in database. It uses User model class.
  3. I don't know how to make passwords hashed while storing them in database using user model for existing table.
  4. I am using Auth::attempt($user) to authenticate.

My code

routes.php

Route::post('/login',array('as' => 'login', function () {


        $user = array(
            'username' => Input::get('username'),
            'password' => Input::get('password')
        );

        if (Auth::attempt($user)) {
             return "Hello World! :)";
            /*return Redirect::route('home')
                ->with('flash_notice', 'You are successfully logged in.');*/
        }
         //return "Hello World! :o";
        // authentication failure! lets go back to the login page
        return Redirect::route('login')
            ->with('flash_error', 'Your username/password combination was incorrect.')
            ->withInput();
}));

login page code:

@extends('layouts.login_registration_master')

@section('content')

<div class="row centered-form">
  <div class="col-xs-12 col-sm-8 col-md-4 col-sm-offset-2 col-md-offset-4">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Please Login</h3>
      </div>
      <div class="panel-body">
         @if (Session::has('flash_error'))
        <div id="flash_error">{{ Session::get('flash_error') }}</div>
        @endif
        {{Form::open(array('route' => 'login', 'method'=>'POST')) }}
          <div class="row">
          <div class="form-group">
            {{ Form::text('username', null, array('class'=>'form-control input-sm','placeholder'=>'User Name')) }}
          </div>
          </div>
          <div class="row">            
              <div class="form-group">
                 {{ Form::password('password', array('class'=>'form-control input-sm','placeholder'=>'Password')) }}
              </div>            
          </div>
        <div class="row">            
            <div class="col-xs-6">
                {{ Form::checkbox('remember', 'Remember Me'); echo ' Remember Me'}}     

            </div>            
            <div class="col-xs-6 pull-right" align="right">
                {{ HTML::linkAction('RegistrationController@showForgotPasswordPage', 'Forgot Password?') }}
            </div>
          </div>
          <div class="row"> 
          {{ Form::submit('Login', array('class'=>'btn btn-info btn-block')) }}
          </div>
          <div class="row" align="center"> 
              {{ HTML::linkAction('RegistrationController@showMainPage', 'Sign Up for new account.') }}
          </div>

        {{Form::close()}}
      </div>
    </div>
  </div>
</div>

@stop

User.php

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'rdm_user';


    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');


        /**
         * Validation
         */

        protected $guarded = array('id');
        protected $fillable = array('first_name', 'last_name','email','password','username', 'role');

        public static $rules = array(
            'first_name' => 'required|min:3',
            'last_name' => 'required|min:3',
            'role' => 'required',
            'username' => 'unique:rdm_user',
            'email' => 'required|email',
            'password' =>'same:password_confirmation'
        );

          public static $rulesUpdate = array(
            'first_name' => 'required|min:3',
            'last_name' => 'required|min:3',
            'role' => 'required',            
            'email' => 'required|email',
            'password' =>'required|same:password_confirmation'
        );

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

        public function getAuthPassword() {
            return $this->password;
        }


}

auth.php

<?php

return array(

    /*
    |--------------------------------------------------------------------------
    | Default Authentication Driver
    |--------------------------------------------------------------------------
    |
    | This option controls the authentication driver that will be utilized.
    | This driver manages the retrieval and authentication of the users
    | attempting to get access to protected areas of your application.
    |
    | Supported: "database", "eloquent"
    |
    */

    'driver' => 'eloquent',

    /*
    |--------------------------------------------------------------------------
    | Authentication Model
    |--------------------------------------------------------------------------
    |
    | When using the "Eloquent" authentication driver, we need to know which
    | Eloquent model should be used to retrieve your users. Of course, it
    | is often just the "User" model but you may use whatever you like.
    |
    */

    'model' => 'User',

    /*
    |--------------------------------------------------------------------------
    | Authentication Table
    |--------------------------------------------------------------------------
    |
    | When using the "Database" authentication driver, we need to know which
    | table should be used to retrieve your users. We have chosen a basic
    | default value but you may easily change it to any table you like.
    |
    */

    'table' => 'rdm_user',

    /*
    |--------------------------------------------------------------------------
    | Password Reminder Settings
    |--------------------------------------------------------------------------
    |
    | Here you may set the settings for password reminders, including a view
    | that should be used as your password reminder e-mail. You will also
    | be able to set the name of the table that holds the reset tokens.
    |
    | The "expire" time is the number of minutes that the reminder should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'reminder' => array(

        'email' => 'emails.auth.reminder',

        'table' => 'password_reminders',

        'expire' => 60,

    ),

);

If you need more information please tell..

Thank you in advance :)

Vinay

  • 写回答

1条回答 默认 最新

  • dongzi5062 2014-12-10 03:49
    关注

    There is rarely any valid reason to not just hash the password. You just hash all the passwords - then your problem is solved.

    $users = Users::all();
    foreach ($users as $user)
    {
        $user->password = Hash::make($user->password);
        $user->save();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败