拒绝所有但只允许来自同一服务器上的 Angularjs 的访问

Deny all but only allow access from Angularjs on the same server

我在没有虚拟主机的同一台服务器上有一个 Lumen 后端和一个 Angularjs 项目。我访问 API 的方式只是通过 URL 之类的 www.example.com/api/public/get_some_data。我想从 public 中隐藏这些端点,但只允许从 Angularjs.

访问

我尝试将其添加到 api/public 文件夹中的 .htaccess,

Order deny, allow
Deny from all
Allow from 127.0.0.1

但是Angular也会被拒绝。

我像这样从 Angular 访问端点,

function login(user) {
  return $http({
    method: 'POST',
    url: 'api/public/login/user',
    data: user,
    headers: {'Content-Type': 'application/json'}
  }).then(function(response){
    console.log(response);
    localStorage.setItem('token', response.data.token);
  }).catch(dataServiceError);
}

Angular 是客户端框架。如果请求来自 Angular 应用程序或其他任何地方,服务器没有区别。

可以根据客户端应用程序可以满足的某些条件限制访问,例如请求 headers (example)。

隐藏式安全是最弱的安全形式,不应依赖。在这种情况下,正确的解决方案是基本身份验证或 token-based 身份验证。

不过,如果入侵者可以访问 Angular 个应用程序,他或她会自动获得对 API 个端点的访问权限,并可以从应用程序中提取身份验证信息。