簡體   English   中英

我如何獲取基本授權標頭並通過使用php對api進行身份驗證

[英]How can i get Basic authorization header and authenticate it for api by using php

我如何獲取基本授權標頭並通過使用PHP對API進行身份驗證。 我創建了一個唯一的密鑰,該密鑰已編碼並存儲在數據庫中。 我正在使用Slim Framework for Rest API。

我可以使用$request->headers();獲取標題$request->headers(); 在Slim Framework中。 它返回指定的所有標頭,但是如何使用數據庫令牌檢查該鍵。

有什么適當的方法嗎?

我在Slim Framework 3中做到了這一點:

我將中間件添加到所有請求中,並檢查了某些字符串的Authorization標頭,如果未設置Authorization標頭或與我的字符串不匹配,我只返回400 HTTP代碼。

$app->add(function($request,$response,$next){
$authorization_header = $request->getHeader("Authorization");

if(empty($authorization_header) || ($authorization_header[0]!="test")){ //you can check the header for a certain string or you can check if what is in the Authorization header exists in your database

    return $response->withStatus(400);
}

$response = $next($request,$response);

return $response;

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM