1

Yii のログインフォームでユーザー入力からパスワードをハッシュして保存する必要があります。次のような POST パラメーターを介してそれらを取得した場合:

$model->username=$_POST['User']['username'];
$model->password=crypt($_POST['User']['username']);// salt might be added
if($model->save())
  $this->redirect(array('view','id'=>$model->id));

このようにして、暗号化されていないパスワードを POST リクエストで公開します。その他の方法は、次のようなログイン フォームから直接アクセスすることです。

public function actionCreate2()
{
    $model=new User;
    $model->username = $form->username;
    $model->password = crypt($form->password);
    if($model->save())
            $this->redirect(array('view','id'=>$model->id));

    $this->render('create',array(
        'model'=>$model,
    ));
}

しかし、これは、保存されたユーザーを認証する私の場合には機能しません。認証機能:

public function authenticate()
{
    $users = User::model()->findByAttributes(array('username'=>$this->username));

    if($users == null)
        $this->errorCode=self::ERROR_USERNAME_INVALID;
    elseif ($users->password !== crypt($this->password, $users->password))
    //elseif($users->password !== $this->password)
        $this->errorCode=self::ERROR_PASSWORD_INVALID;
    else
        $this->errorCode=self::ERROR_NONE;
    return !$this->errorCode;
}

適切な方法でそれを行う方法は?

サミュエルの提案に従うと、さらに多くの問題が発生しました-入力フィールドにハッシュ化されたパスワードとともに、何かを入力する前でもアラームメッセージを検証します(写真を参照): もっとトラブル

「提案」の代わりにユーザー名とパスワードを入力して「作成」を押すと、暗号化されていない値でフォームが送信されます(POSTリクエストのスニッフィングから):

Form Data   view source   view URL   encoded
YII_CSRF_TOKEN:9758c50299b9d4b96b6ac6a2e5f0c939eae46abe
User[username]:igor23
User[password]:igor23
yt0:Create

しかし、実際には何もデータベースに保存されておらず、暗号化も暗号化もされていません...

4

1 に答える 1

1

create メソッドを次のように変更します。

/**
 * Creates a new model.
 * If creation is successful, the browser will be redirected to the 'view' page.
 */
public function actionCreate() {
    $model = new User;

    if (isset($_POST['User'])) {
        $model->attributes = $_POST['User'];
        $model->password = crypt($model->password, 'mysalt123');

        if ($model->save())
            $this->redirect(array('view', 'id' => $model->primaryKey));
    }

    // Reset password field
    $model->password = "";

    $this->render('create', array(
        'model' => $model,
    ));
}

そのelseifを次から変更します。

elseif ($users->password !== crypt($this->password, $users->password))

これに:

elseif (strcmp(crypt($this->password, 'mysalt123'), $users->password))
于 2013-08-19T05:31:51.303 に答える