我有来自 Yii 示例的代码
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 插件的名称、值和正文?感谢提前
嗨,
我已经通过以下服务器端代码解决了这个问题,像您在客户端所做的那样设置 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'];
}
我知道你的问题(与 yii 论坛中的这篇文章相同)与 RESTClient 插件有关。要在 RESTClient 插件中设置标头:使用"标头"功能:
- 列表项
- 菜单"标题"
- "自定义标头"
- "姓名" : X_PASSWORD
- "价值":情绪化
X_USERNAME也是如此。
呵呵,
JM.