Am का एक त्रुटि देता है, लेकिन यहyii2 बाकी हिस्सों में वाहक टोकन पासिंग angular2 और yii2 शोकहारा सेवाओं का उपयोग कर 401
विफल रहता है यह मेरा angular2 हिस्सा है
In the userservice
constructor(private http:HttpClientService, private _domaindetails:Domaindetails) { }
profile(val:string="profile"):Observable<User>{
return this.http.get(this.getUserUrl(val))
.map(this.extractData)
}
यह httpclientService
createAuthorizationHeader(headers: Headers) {
let token = JSON.parse(localStorage.getItem("currentUser")).token;
if (token) {
headers.append('Authorization', 'Bearer ' + token);
headers.append('Content-Type', 'application/x-www-form-urlencoded');
headers.append('Content-Type', 'application/json');
}
}
post(url, data) {
let headers = new Headers();
this.createAuthorizationHeader(headers);
return this.http.post(url, data, {
headers: headers
});
}
है
यह मेरा बैकएंड है Yii2
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['corsFilter'] = [
'class' => \yii\filters\Cors::className(),
'cors' => [
// restrict access to
'Origin' => ['http://localhost:4200'],
'Access-Control-Request-Method' => ['POST', 'GET','PUT'],
// Allow only POST and PUT methods
'Access-Control-Request-Headers' => ['*'],
// Allow only headers 'X-Wsse'
// 'Access-Control-Allow-Credentials' => true,
// Allow OPTIONS caching
'Access-Control-Max-Age' => 3600,
// Allow the X-Pagination-Current-Page header to be exposed to the browser.
'Access-Control-Expose-Headers' => ['X-Pagination-Current-Page'],
],
];
$auth = $behaviors['authenticator'] = [
'class' => HttpBearerAuth::className(),
'only' => ['can-access','profile'], //access controller
];
$behaviors['authenticator']['except'] = ['options'];
return $behaviors;
}
और yii2 कार्रवाई अनाधिकृत उपयोग
public function actionProfile()
{
$user = Yii::$app->user->identity;
return ['data' => $user];
}
की एक त्रुटि लौटने जब मैं जाँच Yii::$app->request->headers;
आप देख सकते हैं वाहक टोकन
सेट किया गया है यह मेरा usermodel है विफल रहता है
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['auth_key' => $token]); //auth_key is the value used in bearer token
}
क्यों यह एक त्रुटि 401 अनधिकृत पहुँच लौटने और कुछ अनुसंधान के बाद
अभी तक जब मैं डाकिया में हेडर वाहक authkey का एक ही मूल्य और प्राधिकरण सेट का उपयोग यह काम करता है पर रखता है मैं पता चला है कि जब भी angular2 एक अनुरोध करता है यह जो
क्या आपने "प्रामाणिक" के बजाय व्यवहार को "bearerAuth" पर सेट करने का प्रयास किया है? – kyle
यह भी असफल हो जाता है और अज्ञात bearerAuth –