Yii2-Restful-API authorization verification 401 exploration solution,

Source: Internet
Author: User

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

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.