Php Laravel API-对邮递员中未经授权的用户不起作用的保护
我正在学习创建Laravel api。我试图保护我的数据免受未经授权的用户的攻击,例如:不允许未经授权的用户获取数据并将其发布到我的应用程序中。在浏览器中,它可以工作:如果用户不提供登录凭据,他就不会得到任何东西。在《邮递员》中,我可以获得所有数据,而无需凭据。为什么?Php Laravel API-对邮递员中未经授权的用户不起作用的保护,php,laravel,postman,Php,Laravel,Postman,我正在学习创建Laravel api。我试图保护我的数据免受未经授权的用户的攻击,例如:不允许未经授权的用户获取数据并将其发布到我的应用程序中。在浏览器中,它可以工作:如果用户不提供登录凭据,他就不会得到任何东西。在《邮递员》中,我可以获得所有数据,而无需凭据。为什么? public function __construct() { $this->middleware('auth.basic'); //works in browser but not in postman
public function __construct()
{
$this->middleware('auth.basic');
//works in browser but not in postman
}
以JSON格式获取所有课程:
public function index()
{
$lessons = Lesson::all();
return $this->respond([
'data' => $this->transformCollection($lessons->toArray())
]);
}
Post方法:(如果我没有给出标题和正文值,我会得到422响应代码)
如何防止Postman为未经授权的用户获取和发布方法。有解决方法吗?您应该从邮递员那里传递令牌并对其进行验证。 然后
public function index()
{
$u = UserSession::where('token',$token)->first();
if(count($u) > 0) {
$lessons = Lesson::all();
return $this->respond([
'data' => $this->transformCollection($lessons->toArray())
]);
} else {
return Api::error(100, $inputs, array("Invalid Token"));
}
}
你可能忘记在postman中打开凭据了吗?我在哪里看到它?我想问题不在postman中,如果我试图以未经授权的用户身份获取数据,它不应该获取任何数据,除非你是:o
$this->validate($request, [
'token' => 'required',
]);
public function index()
{
$u = UserSession::where('token',$token)->first();
if(count($u) > 0) {
$lessons = Lesson::all();
return $this->respond([
'data' => $this->transformCollection($lessons->toArray())
]);
} else {
return Api::error(100, $inputs, array("Invalid Token"));
}
}