仅允许我的 Unity 游戏通过 .htaccess 访问 Web 文件

Allowing only my Unity Game to access web files through .htaccess

(已经回答了,但是如果有人有更好的方法而不硬编码用户名和密码那会很好)


我正在 Unity3D 中制作手机游戏(用 c# 编写)。该游戏可以连接到我的服务器以在在线数据库中获得 check/save 分数。 (服务器是 ubuntu 运行 Apache2)


但问题来了:

每当我去 http://SERVER_IP/WEB_NAME 时,我都可以在浏览器中看到我的所有文件,这并不安全。我试图用 "Deny from All" 制作 .htaccess,但这也阻止了我的游戏使用该文件夹中的任何文件..


这是我的问题:

我如何确保只有我的游戏可以使用这些文件,而不是来自外部的人进入 http://SERVER_IP/WEB_NAME


资源:




提前致谢!

~ JohnDoe

我假设您在 Unity 中使用 WWWForm 您可以使用 HTTP 基本身份验证之类的东西。 它不是超级安全,但可以防止任何人通过网络浏览器简单地访问您的端点。

让我们用协程创建一个简单的例子:

void SaveData()
{
    StartCoroutine(SubmitData());
}

IEnumerator SubmitData()
{
  // create and instance of WWWForm
  WWWForm form = new WWWForm();
  // Add Some data
  form.AddField( "playerName", "Bob" );
  form.AddField( "playerScore", 100 );

  //Get reference to headers so we can modify them.
  Hashtable headers = form.headers;
  byte[] rawData = form.data;
  string url = "SERVER_IP/WEB_NAME";

  //Add the authorisation header with username/password to access the endpoint
  headers["Authorization"] = "Basic " + System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes("username:password"));

  // Post the request
  WWW www = new WWW(url, rawData, headers);
  yield return www;
}

不在 .htaccess 中,我们设置 apache 使用 mod_auth_basic

检查授权

这是一个示例 .htaccess 您应该放在服务器上的文件。 您还需要一个包含用户名和密码的不可公开访问的 .htpasswd 文件。

AuthType Basic
AuthName "My Protected Area"
AuthUserFile /path/to/.htpasswd
Require valid-user

您可以使用 a generator 创建 .htpasswd 文件