Client 和 NN 创建Connection的详解(一)先尝试TOKEN的方式,再使用KERBOS


Client
private Connection getConnection(ConnectionId remoteId,
      Call call, int serviceClass, AtomicBoolean fallbackToSimpleAuth)


   -->
   connection.setupIOstreams(fallbackToSimpleAuth);

      -->  切换realUser的UGI建立Sasl连接
      UserGroupInformation ticket = remoteId.getTicket();
      if (ticket != null) {
        final UserGroupInformation realUser = ticket.getRealUser();
        if (realUser != null) {
          ticket = realUser;
        }
      }
      
      --> 创建socket
      setupConnection()
      ipcStreams = new IpcStreams(socket, maxResponseLength);
      
      
      先发送connection header
          /**
         * Write the connection header - this is sent when connection is established
         * +----------------------------------+
         * |  "hrpc" 4 bytes                  |      
         * +----------------------------------+
         * |  Version (1 byte)                |
         * +----------------------------------+
         * |  Service Class (1 byte)          |
         * +----------------------------------+
         * |  AuthProtocol (1 byte)           |      
         * +----------------------------------+
         */
      writeConnectionHeader(ipcStreams);
      
      如果kerbos enabled, 使用Sasl
      //boolean trySasl = UserGroupInformation.isSecurityEnabled() ||
      //                (ticket != null && !ticket.getTokens().isEmpty());
      //this.authProtocol = trySasl ? AuthProtocol.SASL : AuthProtocol.NONE;
      
      if (authProtocol == AuthProtocol.SASL) {
            try {
              authMethod = ticket
                  .doAs(new PrivilegedExceptionAction<AuthMethod>() {
                    @Override
                    public AuthMethod run()
                        throws IOException, InterruptedException {
                      创建SaslConnection
                      return setupSaslConnection(ipcStreams);
                    }
                  });
      
     


private synchronized AuthMethod setupSaslConnection(IpcStreams streams)
        throws IOException {
      // Do not use Client.conf here! We must use ConnectionId.conf, since the
      // Client object is cached and shared between all RPC clients, even those
      // for separate services.
      saslRpcClient &#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
创建一个 client_secret_jwt token,你需要遵循一些步骤。首,你需要获取 client_id 和 client_secret,这是你的应用程序在身份提供者注册时分配的凭据。接下来,你需要使用这些凭据生成一个 JSON Web Token (JWT)。 以下是一个示例代码,展示了如何使用 Python 中的 PyJWT 库来创建 client_secret_jwt token: ```python import jwt from datetime import datetime, timedelta # 设置 JWT 的有效期 expiration = datetime.utcnow() + timedelta(minutes=5) # 构建 JWT 的 payload payload = { "iss": "your_client_id", # 发行人 (client_id) "sub": "your_client_id", # 主题 (client_id) "aud": "https://identityprovider.com/oauth2/token", # 受众 (token endpoint) "exp": expiration.timestamp(), # 过期时间戳 "nbf": datetime.utcnow().timestamp() # 生效时间戳 } # 使用 client_secret 签名生成 JWT client_secret = "your_client_secret" algorithm = "HS256" # 使用 HS256 算法进行签名 jwt_token = jwt.encode(payload, client_secret, algorithm) print(jwt_token) ``` 请确保将上述代码中的 "your_client_id" 和 "your_client_secret" 替换为你的实际凭据。 此代码示例使用 PyJWT 库来生成 JWT,并使用 HS256 算法进行签名。生成的 JWT 包含了发行人、主题、受众、过期时间和生效时间等信息。 请注意,实际使用时,你可能需要根据你的身份提供者的要求进行适当的调整,例如更改有效期或添加其他必需的声明。 希望这可以帮助到你!如果你有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值