Yii2-Restful-API authorization verification 401 exploration solution,
Do Yii2-Restful-API authorization verification, token normal acquisition, header request has been 401, trouble ah!
Check whether there is any problem with the Code. Otherwise, I am helpless.
Environment: WAMP
First, I can get the token normally.
When you access http: // localhost/advanced/api/web/v1/users/user-profile, 401 is displayed.
What's the matter? Trace back. Where can I verify the header? Where can I start? Authenticator corresponds to class.
public function behaviors() { return ArrayHelper::merge (parent::behaviors(), [ 'authenticator' => [ 'class' => HttpBearerAuth::className(), 'optional' => [ 'login', 'signup-test', ], ] ] ); }
Tracing HttpBearerAuth
Find the authenticate Method and find $ authHeader = $ request-> getHeaders ()-> get ('authorization ');
Make a log to see if $ authHeader has obtained the Header.
Yii::info('authHeader=' . json_encode($authHeader), 'test');
If yes, check whether the token is written incorrectly, check the authenticate Method, and check the code.
If all the headers have been uploaded, let's take a look at your logic.
My results: authHeader = NULL
Then test the hard code.
Delete Authorization and access http: // localhost/advanced/api/web/v1/users/user-profile again.
This shows that our code logic is correct.
$authHeader = $request->getHeaders()->get('Authorization');
$ Request-> getHeaders (), the header does not get.
Then ask du Niang.
Reference: github: https://github.com/yiisoft/yii2/issues/6631
Probably Apache.
Add SetEnvIf Authorization. + HTTP_AUTHORIZATION = $0 to. htaccess
Comment out the hardcoded re-access http: // localhost/advanced/api/web/v1/users/user-profile
Source code
Link: https://pan.baidu.com/s/1pMhPfsB password: 9vsq