本主题介绍如何在使用 API 时,使用 NSX 基于会话的身份验证生成 JSESSIONID Cookie。使用此方法可减少必须输入用户名和密码的次数。您可以将这种类型的身份验证与 vIDM 和 LDAP 身份验证结合使用。

NSX 使用多种不同的机制对 NSX 用户进行身份验证。其中包括:

  • HTTP 身份验证
  • 基于会话的身份验证
  • 主体身份或基于证书的身份验证
  • 使用 vIDM 和云服务平台 (Cloud Service Platform, CSP) 进行单点登录

NSX 会在会话创建期间使用用户名和密码生成会话 Cookie。创建会话 Cookie 后,后续 API 请求可以选择使用此会话 Cookie,而不是使用用户名和密码凭据。这意味着,会话状态针对的是生成会话 Cookie 的特定 NSX Manager。在客户端向 NSX Manager 发出请求时,只有在它们提供的会话 Cookie 与管理器生成的某个 Cookie 匹配时,才允许客户端进行身份验证。在任何用户从 NSX Manager 注销时,将立即从 NSX Manager 的反向代理中消除会话 Cookie,且无法重用。闲置会话会自动超时,或者您可以使用 API 将其删除。

使用 API 请求进行访问将生成审核日志详细信息。将始终启用这种日志记录,并且无法将其禁用。会话审核是在系统启动时启动的。审核日志消息在日志消息的结构化数据部分中包含 audit="true" 文本。

此示例介绍了如何使用 cURL 为 API 调用创建基于会话的身份验证。

过程

  1. 要创建用于向 NSX Manager 进行身份验证并从标头检索 xsrf 的新会话 Cookie,请输入:
    # curl -v -k -c session.txt -X POST -d '[email protected]&j_password=SecretPwsd3c4d' https://<manager-ip>/api/session/create 2>&1 > /dev/null | grep -i xsrf < x-xsrf-token: 5a764b19-5ad2-4727-974d-510acbc171c8

    在此示例中,cURL 命令会进行管理器身份验证,将会话 Cookie 放置在 sessions.txt 文件中,并将所有 HTTP 响应标头写入到 headers.txt 文件中。您需要使用 headers.txt 中的其中一个标头(x-xsrf-token 标头)在后续请求中提供。

    您还可以对用户名中的 @ 使用标准 Unicode/URI 编码。

    会话内容示例如下:

    # cat session.txt
          # Netscape HTTP Cookie File
          # https://curl.haxx.se/docs/http-cookies.html
          # This file was generated by libcurl! Edit at your own
            risk.
          # HttpOnly_172.182.183.135 FALSE / TRUE 0 JSESSIONID CFG588DF6DGF493C0EAEFC62685C42E1
  2. 如果需要创建两个会话,请更改 session.txt 文件的名称,以便这两个会话均处于有效状态。
    # curl -v -k -c session1.txt -X POST -d 'j_username= [email protected]&j_password=SecretPwsd3c4d' https://<manager-ip>/api/session/create 2>&1 > /dev/null | grep -i xsrf < x-xsrf-token: cbce48f3-48fc-46c0-a8e7-f2f55ebf8e15
    # curl -v -k -c session2.txt -X POST -d 'j_username= [email protected]&j_password=SecretPwsd3c4d' https://<manager-ip>/api/session/create 2>&1 > /dev/null | grep -i xsrf < x-xsrf-token: abf1fa2c-86d5-47e4-9a0a-242424dd1761
  3. 对于后续调用,请使用上一步中的会话 Cookie 和 xsrf 标头。
    # curl -k -b session.txt -H "x-xsrf-token: 5a764b19-5ad2-4727-974d-510acbc171c8"
          https://10.182.183.135/policy/api/v1/infra/segments
          {
            "results" : [ {
              "type" : "ROUTED",
              "subnets" : [ {
                "gateway_address" : "192.168.10.1/24",
                "network" : "192.168.10.0/24"
              } ],
              "connectivity_path" :
            "/infra/tier-1s/test_t1",
              "transport_zone_path" :
            "/infra/sites/default/enforcement-points/default/transport-zones/1b3a2f36-bfd1-443e-a0f6-4de01abc963e",
              "advanced_config" : {
                "address_pool_paths" : [ ],
                "hybrid" : false,
                "multicast" : true,
                "inter_router" : false,
                "local_egress" : false,
                "urpf_mode" : "STRICT",
                "connectivity" : "ON"
              },
              "admin_state" : "UP",
              "replication_mode" : "MTEP",
              "resource_type" : "Segment",
              "id" : "seg1",
              "display_name" : "seg1",
              "path" : "/infra/segments/seg1",
              "relative_path" : "seg1",
              "parent_path" : "/infra",
              "unique_id" :
            "6573d2c9-f4f9-4b37-b410-71bded8857c3",
              "marked_for_delete" : false,
              "overridden" : false,
              "_create_user" : "admin",
              "_create_time" : 1633331197569,
              "_last_modified_user" : "admin",
              "_last_modified_time" : 1633331252660,
              "_system_owned" : false,
              "_protection" : "NOT_PROTECTED",
              "_revision" : 1
            } ],
            "result_count" : 1,
            "sort_by" : "display_name",
            "sort_ascending" : true
        
    如果在集群中将同一会话 Cookie 与另一节点结合使用,命令将失败并显示以下错误消息:
    The credentials were incorrect or the account specified has been locked.","error_code":403.

    会话过期后,NSX Manager 将以“403 已禁止 HTTP”作为响应。然后,您必须获取新的会话 Cookie 和 x-xsrf-token。

  4. 要配置会话到期设置,请使用 connection_timeout API 命令。默认会话过期时间为 1800 秒(30 分钟)。
    1. 使用 GET https://<nsx-mgr>/api/v1/cluster/api-service 查看当前配置的快照。
      GET https://<nsx-mgr>/api/v1/cluster/api-service
      {
          "session_timeout": 1800,
          "connection_timeout": 30,
          "protocol_versions": [
              {
                  "name": "TLSv1.1",
                  "enabled": true
              },
              {
                  "name": "TLSv1.2",
                  "enabled": true
              }
          ],
          "cipher_suites": [
              {
                  "name": "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
                  "enabled": true
              },
              {
                  "name": "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
                  "enabled": true
              },
              {
                  "name": "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
                  "enabled": true
              },
              {
                  "name": "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
                  "enabled": true
              },
              {
                  "name": "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384",
                  "enabled": true
              },
              {
                  "name": "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
                  "enabled": true
              },
              {
                  "name": "TLS_RSA_WITH_AES_128_CBC_SHA",
                  "enabled": true
              },
              {
                  "name": "TLS_RSA_WITH_AES_128_CBC_SHA256",
                  "enabled": true
              },
              {
                  "name": "TLS_RSA_WITH_AES_128_GCM_SHA256",
                  "enabled": true
              },
              {
                  "name": "TLS_RSA_WITH_AES_256_CBC_SHA",
                  "enabled": true
              },
              {
                  "name": "TLS_RSA_WITH_AES_256_CBC_SHA256",
                  "enabled": true
              },
              {
                  "name": "TLS_RSA_WITH_AES_256_GCM_SHA384",
                  "enabled": true
              }        ,
              {
                  "name": "TLS_ECDSA_WITH_AES_256_GCM_SHA384",
                  "enabled": true
              }
          ],
          "redirect_host": "",
          "client_api_rate_limit": 100,
          "global_api_concurrency_limit": 199,
          "client_api_concurrency_limit": 40,
          "basic_authentication_enabled": true,
          "cookie_based_authentication_enabled": true,
          "resource_type": "ApiServiceConfig",
          "id": "reverse_proxy_config",
          "display_name": "reverse_proxy_config",
          "_create_time": 1658339081246,
          "_create_user": "system",
          "_last_modified_time": 1658339081246,
          "_last_modified_user": "system",
          "_system_owned": false,
          "_protection": "NOT_PROTECTED",
          "_revision": 0
      }
      
    2. 使用所需值编辑 JSON 并移除除 _revision 以外的所有下划线字段 (_xxxxx)。
       GET https://<nsx-mgr>/api/v1/cluster/api-service
      {
          "global_api_concurrency_limit": 199,
          "client_api_rate_limit": 100,
          "client_api_concurrency_limit": 40,
          "connection_timeout": 30,
          "redirect_host": "",
          "cipher_suites": [     
            {"enabled": true, "name": "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"},
            {"enabled": true, "name": "TLS_RSA_WITH_AES_256_GCM_SHA384"},
            {"enabled": true, "name": "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"},
            {"enabled": true, "name": "TLS_RSA_WITH_AES_128_GCM_SHA256"}
            {"enabled": true, "name": "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384}",
            {"enabled": true, "name": "TLS_RSA_WITH_AES_256_CBC_SHA256"},
            {"enabled": true, "name": "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA"},
            {"enabled": true, "name": "TLS_RSA_WITH_AES_256_CBC_SHA"},
            {"enabled": true, "name": "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"},
            {"enabled": true, "name": "TLS_RSA_WITH_AES_128_CBC_SHA256"},
            {"enabled": false, "name": "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA"},
            {"enabled": true, "name": "TLS_RSA_WITH_AES_128_CBC_SHA"},
        
            {"enabled": true, "name": "TLS_ECDSA_WITH_AES_256_GCM_SHA384"},
          ],
            "protocol_versions": [
              {"enabled": true, "name": "TLSv1.1"},
          {"enabled": true, "name": "TLSv1.2"}
        ]    
          "_revision": 0
      }
      
    3. 使用 PUT https://<nsx-mgr>/api/v1/cluster/api-service 修改当前配置。
  5. 要删除会话 Cookie,请使用 /api/session/destroy API 命令。
    curl -k -b cookies.txt -H "`grep x-xsrf-token headers.txt`" -X POST https://<nsx-manager>/api/session/destroy
    例如:
    curl -k -b cookies.txt -H "`grep x-xsrf-token headers.txt`" https://$TESTHOST/api/session/destroy
    curl -k -b cookies.txt -H "`grep x-xsrf-token headers.txt`" https://$TESTHOST/api/v1/logical-ports
    响应:
    {
        "module_name" : "common-services",
        "error_message" : "The credentials were incorrect or the account specified has been locked.",
        "error_code" : "403"
    }
    

下一步做什么

要查看使用基于会话的受支持身份验证服务对用户进行身份验证的要求,请参见与 VMware Identity Manager/Workspace ONE Access 集成与 LDAP 集成