c++ - 服务器端识别客户端的方法

标签 c++ architecture client-server server-side

我正在用 C++ 创建客户端-服务器应用程序。服务器将接受来自多个客户端的请求。每个客户都有在服务器上创建的个人帐户。身份验证后,我知道客户端使用特定 ip 登录到特定帐户。现在我想确定哪些请求考虑特定帐户,例如:

客户端 A 登录:

username: user
password: pass123

服务器发现这些数据与 id = 3 的帐户匹配。

现在,当此客户端 A 发送一些请求时,我希望服务器访问(并最终更改)id = 3 的帐户。到目前为止,我已经想出了这两个想法:

  1. 我有一个 std::map,其中键是 client ip,值是 account id。身份验证后,服务器将客户端的 ip 和它的帐户 ID 存储到此 map 中,稍后当服务器收到来自客户端的请求时,它会检查它的 ip 并在 map 中查找它。

  2. 我有一个 std::map,其中键是随机生成的键,值是 account id。身份验证后,服务器为这个特定的客户端生成随 secret 钥,将这个 key 发送给客户端,客户端保存它以供进一步使用,服务器将这个 key 和account id存储在map中。

我想知道这些是否是处理此类问题的好方法。哪个更好,还要考虑安全性(这对我来说很重要)?或者有更好的方法吗?

最佳答案

1) I have a std::map where key is client ip and value is account id. After authentication, server stores client's ip and it's account id into this map and later when server receives request from client, it checks it's ip and looks for it in map.

IP 本身是不够的:可以有多个不同的客户端从同一个 IP 连接(或者来自同一台计算机,或者来自 NAT 后面的不同计算机,这样您只能看到 NAT IP)。如果你想要一个基于 IP 的唯一 key ,你需要使用客户端的 IP/端口元组。

2) I have a std::map where key is randomly generated key and value is account id. After authentication, server generates random key for this specific client, sends this key to client, client saves it for further use, server stores this key and account id in map.

这是非常危险的:是什么禁止客户端发送另一个客户端的“ session ID”而不是他自己的“ session ID”,从而劫持另一个客户端的 session ?这与您可能想要了解的 HTTP session 劫持问题完全相同。简而言之:如果可以避免,就不要这样做。


其他可能的解决方案:

  • 仍然使用 std::map,您可以使用套接字句柄作为键:它在服务器上必须是唯一的,因此不会造成混淆,并且可以避免检索每条消息的客户端 IP/端口。

  • 如果您的服务器使用旧的“每个连接一个线程”模型,那么您就不必跳过这些障碍。只需将您的 session 数据与线程本地存储变量相关联即可。或者,几乎所有线程库都允许您将参数传递给您的线程,这些参数可用于将特定数据与您的线程相关联(请参见下面的示例)。

  • 如果您的服务器使用旧的“每个连接一个进程”模型(fork),那么它会更容易,每个进程都有自己的变量,因此您不需要做任何特别的事情。


不幸的是,只要我们不知道您的服务器使用的模型(线程、 fork 、选择、aio,...?),您的问题就很开放,所以很难给您一个明确的答案。


如果您使用的是线程模型,这里(大致)是我通常的做法(C++11 线程,但任何其他线程库也可以这样做):

class ClientSession {
public:
    ClientSession(int sock)
        : m_sock(sock),
          m_thread(&ClientSession::threadFunction, this)
    {
    }
private:
    int m_sock;
    WhateverType m_someSessionVariable;

    std::thread m_thread; // the thread object should be declared last so that
    // it is initialised last in the constructor, thus avoiding race conditions
    // during the initialisation (you really don't want the thread to access
    // your member variables if they are not yet initialised!)

    static void threadFunction(ClientSession* object) {
      object->threadMethod();
    }

    void threadMethod() {
      // handle your connection
      // the current ClientSession object (this) *is* your session
      // put whatever you want in it, eg. m_someSessionVariable
    }
};

//...

int sock_client = TEMP_FAILURE_RETRY(accept(sock_server, 0, 0));
if (sock_client >= 0)
  new ClientSession(sock_client);

警告:显然这段代码是有缺陷的,它从不破坏 ClientSession 对象,所以它有内存泄漏,但我的意思是展示如何将线程与特定的线程相关联 session 对象,我将留给您根据您的确切架构和需求来管理对象的生命周期。

关于c++ - 服务器端识别客户端的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16037683/

相关文章:

c++ - 如何在 OSX 应用程序的标题栏中的 Qt 中添加全屏图标?

c# - 包含 ASP.NET MVC 和 Web 表单的项目解决方案结构良好吗?

javascript - 在 brunch.io 中创建自定义 JS 目录

java - 当收到来自服务器的消息时,如何在 Android 客户端上启动新的 Activity

c - 为了将 tcp 程序转换为 udp 需要进行哪些更改

java - Android - Java TCP连接NAT穿越

c++ - 尝试使变量检查数组中的内容

c++ - 使用 PCLVisualizer 可视化 PCL 1.6 的网格

c++ - 初学者 C++ 程序上的 APPCRASH,调用函数上的公共(public)方法

iphone - 需要 iOS 核心数据架构技巧