2017-05-05 140 views
0

我正在开发一个使用yii2 REST Api basicauth的api。我已经在我的本地xampp服务器上成功开发了。我已经将代码部署到运行centos的vps。但我得到了以下无法回应的回复:yii2 rest api basic auth throws 401未授权

{ 
"name": "Unauthorized", 
"message": "Your request was made with invalid credentials.", 
"code": 0, 
"status": 401, 
"type": "yii\\web\\UnauthorizedHttpException" 
} 

我已正确设置标题。

响应头:

Connection →Keep-Alive 
Content-Type →application/json; charset=UTF-8 
Date →Fri, 05 May 2017 08:40:49 GMT 
Keep-Alive →timeout=5, max=100 
Server →Apache 
Transfer-Encoding →chunked 
Www-Authenticate →Basic realm="api" 
X-Powered-By →PHP/5.6.30 


my headers: 
Authorization : Basic BVrVm3Ay0jg2XBr3TeITaClg 
Content-Type : application/x-www-form-urlencoded 

当我转储头我没有看到授权头:

object(yii\web\HeaderCollection)#81 (1) { 
    ["_headers":"yii\web\HeaderCollection":private]=> 
    array(10) { 
    ["content-type"]=> 
    array(1) { 
     [0]=> 
     string(33) "application/x-www-form-urlencoded" 
    } 
    ["accept"]=> 
    array(1) { 
     [0]=> 
     string(3) "*/*" 
    } 
    ["accept-encoding"]=> 
    array(1) { 
     [0]=> 
     string(19) "gzip, deflate, sdch" 
    } 
    ["accept-language"]=> 
    array(1) { 
     [0]=> 
     string(14) "en-US,en;q=0.8" 
    } 
    ["cache-control"]=> 
    array(1) { 
     [0]=> 
     string(8) "no-cache" 
    } 
    ["connection"]=> 
    array(1) { 
     [0]=> 
     string(10) "keep-alive" 
    } 
    ["cookie"]=> 
    array(1) { 
     [0]=> 
     string(172) "_csrf=ed46321a4d6f68c815e71201a9d47fa4594bc7dc66346fe10111269e612fa24ea%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22afHivmMVT9TeQ24W-jwdIBu5sx4nA_hY%22%3B%7D" 
    } 
    ["host"]=> 
    array(1) { 
     [0]=> 
     string(14) "104.238.73.161" 
    } 
    ["postman-token"]=> 
    array(1) { 
     [0]=> 
     string(36) "b2123a9e-1585-5a44-4af1-dab9f12951c6" 
    } 
    ["user-agent"]=> 
    array(1) { 
     [0]=> 
     string(113) "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36" 
    } 
    } 
} 

任何想法,为什么这是在远程服务器上发生的事情?请帮忙。

回答

1

嗨任何一个过这个问题我已经找到了解决办法来:

在这里,如果你想进一步挖掘你有更多的信息。

我在我的.htaccess文件中添加了以下代码,问题已修复。

SetEnvIf Authorization .+ HTTP_AUTHORIZATION=$0 

,我发现从下面的线程解决方案: https://github.com/yiisoft/yii2/issues/6631

0

这可能是CRSF验证。您可以在您的控制器中使用

Public $enableCsrfValidation = false; 

这将禁用该控制器的每个操作中的验证。 https://yii2-cookbook.readthedocs.io/csrf/

+0

嗨Chux,但如果它是一个CRSF问题,那么它不应该在我的本地合作?我对么 ? – Chamindar2002

+0

是的,你是对的,我的错。你可以看看app.log文件吗? – Chux

+0

嗨chux我已经更新了我的问题。当我转储$ request-> getHeaders()时,它没有显示数组中的'authorization'元素。 – Chamindar2002