如何使用RESTclient Addon Firefox为restful api身份验证设置标头

时间:2012-05-04 18:05:42

标签: php rest yii restful-authentication

我有来自Yii Example

的代码
private function _checkAuth()
{
    // Check if we have the USERNAME and PASSWORD HTTP headers set?
    if(!(isset($_SERVER['HTTP_X_USERNAME']) and isset($_SERVER['HTTP_X_PASSWORD']))) {
        // Error: Unauthorized

        $this->_sendResponse(401);
    }
    $username = $_SERVER['HTTP_X_USERNAME'];
    $password = $_SERVER['HTTP_X_PASSWORD'];
    // Find the user
    $user=User::model()->find('LOWER(username)=?',array(strtolower($username)));
    $this->_sendResponse('200','$username');
    if($user===null) {
        // Error: Unauthorized
        $this->_sendResponse(401, 'Error: User Name is invalid');
    } 

    else if(!$user->validatePassword($password)) {
        // Error: Unauthorized
        $this->_sendResponse(401, 'Error: User Password is invalid');
    }
}

如何设置标头信息以进行身份​​验证。 如何在请求中设置HTTP_X_USERNAME和HTTP_X_PASSWORD;

有关RESTClient插件的名称,值和正文吗? 感谢提前

2 个答案:

答案 0 :(得分:6)

您好我已经通过以下服务器端代码解决了这个问题,设置了HTTP请求标头,就像您在客户端已经做的那样。

private function _checkAuth()
    {
        // Check if we have the USERNAME and PASSWORD HTTP headers set?

        $headers = apache_request_headers();

        if(!(isset($headers['X_'.self::APPLICATION_ID.'_USERNAME']) and isset($headers['X_'.self::APPLICATION_ID.'_PASSWORD']))) {
            // Error: Unauthorized
            $this->_sendResponse(401);
        }

        $username = $headers['X_'.self::APPLICATION_ID.'_USERNAME'];
        $password = $headers['X_'.self::APPLICATION_ID.'_PASSWORD'];
}

答案 1 :(得分:4)

我理解您的问题(与this post in yii forum相同)与RESTClient插件有关。要在RESTClient插件中设置标头:使用“标题”功能:

  • 列表项
  • 菜单'标题'
  • '自定义标题'
  • '姓名':X_PASSWORD
  • '价值':emo

与X_USERNAME相同。

HTH,

JM。