Laravel Restfull Api安全性

问题描述 投票:1回答:2

对我来说,防止局外人需要什么。我只想响应我定义的那些地方的请求。

示例:允许的应用程序和URL

com.x.app
com.y.app
--------- AND ---------
http://www.x.com

是否有简单的方法可以做到这一点?最好的问候!

php laravel security
2个回答
0
投票

在Laravel中构建RESTful API的最佳方法是通过routes/api.php

向该文件添加路由时,例如:

Route::get('/users/list', 'ApiController@userList');

这意味着当您转到yourwebsite.com/api/users.list时,它将在给定的控制器中执行给定的方法。

就对您的API用户进行身份验证而言,您可以将用户及其API密钥存储在数据库中,并在他们使用魔术构造方法到达方法之前对其进行身份验证。

public function __construct() {
  $inputKey = filter_input(INPUT_GET, 'key'); //filter input the way you want
  if(!isCustomer($inputKey) { //validate api keys in your app somehow
     die('Authentication Failure'); //kick the freebooters
  }
}

0
投票

您应该清楚地说明您想要什么。基本上,要执行类似的操作,您需要检查所接收的HTTP标头的$_SERVER['HTTP_USER_AGENT']属性,并使用AndroidiPhone关键字进行过滤。

© www.soinside.com 2019 - 2024. All rights reserved.