簡體   English   中英

使用Cakephp和PhoneGap進行身份驗證

[英]Authentication using Cakephp and PhoneGap

我正在使用在服務器端使用Cakephp在客戶端使用PhoneGap的應用程序,並且使用JSON作為訪問服務器端的中介。

現在,我正在專門處理一個登錄表單,用戶需要在其中輸入他/她的用戶名和密碼。 我將以下內容放入controller中:

public function api_login() {

    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Headers: Authorization");

    if ($this->request->data && isset($this->request->data['username']) && isset($this->request->data['password'])) {
        $arrUser = $this->User->find('all',array(
            'conditions'=>array(
                'username'=> $this->request->data['username'],
                'password' => $this->request->data['password']
            )
        ));

        if (count($arrUser) > 0 ) {
            $this->Session->write('Auth.User',$arrUser[0]['User']);
            $arrReturn['status'] = 'SUCCESS';
            $arrReturn['data'] = array('loginSuccess' => 1,'user_id' => $arrUser[0]['User']['id'] );

        }
        else {
            $arrReturn['status'] = 'NOTLOGGEDIN';
            $arrReturn['data'] = array( 'loginSuccess' => 0 );
        }
    } else {
        $arrReturn['status'] = 'NOTLOGGEDIN';
        $arrReturn['data'] = array( 'loginSuccess' => 0 );
    }
    echo json_encode($arrReturn);
}

在客戶端,我正在檢索JSON編碼如下:

<script>
    $(document).ready(function(){
        $('form').on('submit',function(e){
            e.preventDefault();
            $username = $("#form-username").val();
            $password = $("#form-password").val();
            $.ajax({
                url : "http://localhost/teslaphonegap_cakephp/" + 'login.json',
                cache : false,
                data : {
                    'username' : $username,
                    'password' : $password },
                dataType : 'json',
                type : 'POST',
                success : function(result) {

                    if(result.status=="SUCCESS"){
                        alert("success");
                        console.log(result);
                    }else{

                        alert("username or pass are wrong");
                        console.log(result);
                    } },
                error : function(xhr, status, err) {
                    alert("ERROR");
                }
            });
        });
    });
</script>

在我的Model我使用beforeSave()來哈希密碼,然后才在數據庫的最開始添加密碼:

public function beforeSave($options = array()) {

    $value=$this->data['User']['password'];
    $encrypted = Security::encrypt($value, Configure::read('Security.cipherCriptKey'));
    $this->data['User']['password'] = $encrypted;
    return true;
} 

現在,當我嘗試登錄時,它總是返回錯誤消息,因為它會將未散列的值與數據庫中已經散列的其他值進行比較。 我該如何解決這個問題? 我使用了afterFind()但是沒有用:

public function afterFind($results, $primary = false) {

    foreach ($results as $key => $val) {            
        if(isset($val['User']['password'])){
            $results['User']['password'] = Security::decrypt($val['User']['password'], Configure::read('Security.cipherCriptKey'));
        }
        return $results;
    }        
}

-編輯

在我的core.php我使用了以下內容:

    Configure::write('Security.cipherCriptKey','su0HKssPmdbwgK6LdQLqzp0Y7zOmyaTI');

首先,您的afterFind()回調無法按預期工作。

        $results['User']['password'] = Security::decrypt($val['User']['password'], Configure::read('Security.cipherCriptKey'));

應該寫成

        $results[$key]['User']['password'] = Security::decrypt($val['User']['password'], Configure::read('Security.cipherCriptKey'));

但是,更改此設置將無法解決您的問題。 如果您在數據庫中搜索密碼與$this->request->data['password']匹配的記錄,則不會返回任何結果。 請注意,數據庫中的密碼是散列的。

您必須從與$this->request->data['username']匹配的表users中獲取記錄,解密字段password的值,並將其與$this->request->data['password']

afterFind()已經處理了解密,因此您的代碼可以編寫如下:

if ($this->request->data && isset($this->request->data['username']) && isset($this->request->data['password'])) {
    $arrUser = $this->User->find('first',array(
        'conditions'=>array(
            'username'=> $this->request->data['username'],
        )
    ));

    if ($this->request->data['password'] == $arrUser['User']['password']) { 
        $this->Session->write('Auth.User',$arrUser['User']);
        $arrReturn['status'] = 'SUCCESS';
        $arrReturn['data'] = array('loginSuccess' => 1,'user_id' => $arrUser['User']['id'] );
        //rest of your code

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM