dongling2038 2017-02-23 03:44
浏览 105
已采纳

JSON登录认证Zend框架

I am creating a form login with ExtJS, and sending JSON data to do authentification within Zend Framework. The problem is, no matter what username and password I fill, login always succeed. Here's the related code :

Submit Function for Ext JS Form, where we send JSON data contained username and password.

var doLogin = function () {
    if (formPanel.getForm().isValid()) {
        formPanel.getForm().submit({
            method: 'POST',
            url: '/zend/public/auth/valid',
            waitMsg: 'Processing Request',
            success: function (form, action) {
                document.location = '/zend/public/guestbook';
            },
            failure: function (form, action) {
                if (action.failureType = 'server') {
                    obj = Ext.util.JSON.decode(action.response.responseText);
                    Ext.Msg.alert('Login Failed', obj.errors.reason);
                } else {
                    Ext.Msg.alert('Warning!', 'Authentification server is uneachable : ' + action.response.responseText);
                }
                formPanel.getForm().reset
            }
        })
    }
}

The Controller, we have ValidAction function to receive and send JSON data, and process to do the authentification.

public function validAction()
{
    if(!isset($this->session->isLogin)){
        $username = mysql_escape_string($_POST['username']);
        $password = mysql_escape_string($_POST['password']);

        $formdata = array('username'=>$username, 'password'=>$password); 
        if ($this->_process($formdata)) {
            $this->session->setExpirationSeconds(3600);
            $msg = '{success:true, result:{message:\'Welcome, '.$username.'!\'}}';
        } else {
            $msg = '{success:false, errors:{reason:\'Login failed, try again.\'}}';
        }
    }

protected function _process($values) {
    // Get our authentication adapter and check credentials
    $adapter = $this->_getAuthAdapter();
    $adapter->setIdentity($values['username']);
    $adapter->setCredential($values['password']);

    $auth = Zend_Auth::getInstance();
    $result = $auth->authenticate($adapter);
    if ($result->isValid()) {
        $user = $adapter->getResultRowObject();
        $auth->getStorage()->write($user);
        return true;
    }
    return false;
}

The problem lies in validAction, and weirdly I do var_dump to $this->process($formdata) and returns false, yet it always go to if function, message Success. Any ideas? Appreciated fellas.

UPDATE :

The var_dump :

Uncaught Error: You're trying to decode an invalid JSON String: 
array(2) {
   ["username"]=>
   string(2) "ad"
   ["password"]=>
   string(4) "pass"
}
bool(false)
string(59) "{success:false, errors:{reason:'Login failed, try again.'}}"
  • 写回答

1条回答 默认 最新

  • duanpu2272 2017-02-23 08:56
    关注

    Backend problem

    You are outputting invalid JSON.

    PHP provides json_encode to save you having to manually create json:

    $response=array();
    $response['success']=false;
    $response['result']=array();
    $response['message']='Welcome '.$username;
    $msg = json_encode($response);
    

    If you really don't want to use this you should add double quotes to your keys, and change to double quotes for your string properties too:

    $msg = '{"success":true, "result":{"message":"Welcome, '.$username.'!"}}';
    

    Front end problem

    You are using success and failure methods, but I can't see anything in your back end code to send status headers.

    The failure method will only get called when a response returns with a non 200 status code. So you may need to either add this to your back end code, and/or also decode the response inside your success method to make sure that you have sent success:true as part of your json before redirecting.

    To send the header in PHP 5.4 or newer:

    http_response_code(401); 
    

    in 5.3 or older you have to use header method instead - but if you are running this version you should upgrade immediately so I wont include an example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序