只允许我的Unity Game通过.htaccess访问web文件


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

(这个问题已经得到了回答,但如果有人有更好的方法不用硬编码用户名和密码,那就太好了)


我正在Unity3D制作一款手机游戏(用c#编写)。这个游戏可以连接到我的服务器,在在线数据库中检查/保存分数。(服务器是运行Apache2的ubuntu)


但问题是:

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


这是我的问题:

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


资源:

  • Apache2.conf:http://pastebin.com/1ZQhNGa4



提前感谢!

~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

您可以使用生成器创建.htpasswd文件