3.3. 为 Hot Rod 客户端配置身份验证机制


数据网格服务器使用不同的机制来验证 Hot Rod 客户端连接。

流程

  • 使用 AuthenticationConfigurationBuilder 类中的 saslMechanism() 方法指定身份验证机制,或使用 infinispan.client.hotrod.sasl_mechanism 属性。

SCRAM

ConfigurationBuilder clientBuilder = new ConfigurationBuilder();
clientBuilder.addServer()
               .host("127.0.0.1")
               .port(11222)
             .security()
               .authentication()
                 .saslMechanism("SCRAM-SHA-512")
                 .username("myuser")
                 .password("qwer1234!");

摘要

ConfigurationBuilder clientBuilder = new ConfigurationBuilder();
clientBuilder.addServer()
               .host("127.0.0.1")
               .port(11222)
             .security()
               .authentication()
                 .saslMechanism("DIGEST-MD5")
                 .username("myuser")
                 .password("qwer1234!");

PLAIN

ConfigurationBuilder clientBuilder = new ConfigurationBuilder();
clientBuilder.addServer()
               .host("127.0.0.1")
               .port(11222)
             .security()
               .authentication()
                 .saslMechanism("PLAIN")
                 .username("myuser")
                 .password("qwer1234!");

OAUTHBEARER

String token = "..."; // Obtain the token from your OAuth2 provider
ConfigurationBuilder clientBuilder = new ConfigurationBuilder();
clientBuilder.addServer()
               .host("127.0.0.1")
               .port(11222)
             .security()
               .authentication()
                 .saslMechanism("OAUTHBEARER")
                 .token(token);

EXTERNAL

ConfigurationBuilder clientBuilder = new ConfigurationBuilder();
clientBuilder
   .addServer()
      .host("127.0.0.1")
      .port(11222)
   .security()
      .ssl()
         // TrustStore stores trusted CA certificates for the server.
         .trustStoreFileName("/path/to/truststore")
         .trustStorePassword("truststorepassword".toCharArray())
         .trustStoreType("PCKS12")
         // KeyStore stores valid client certificates.
         .keyStoreFileName("/path/to/keystore")
         .keyStorePassword("keystorepassword".toCharArray())
         .keyStoreType("PCKS12")
      .authentication()
         .saslMechanism("EXTERNAL");
remoteCacheManager = new RemoteCacheManager(clientBuilder.build());
RemoteCache<String, String> cache = remoteCacheManager.getCache("secured");

GSSAPI

LoginContext lc = new LoginContext("GssExample", new BasicCallbackHandler("krb_user", "krb_password".toCharArray()));
lc.login();
Subject clientSubject = lc.getSubject();

ConfigurationBuilder clientBuilder = new ConfigurationBuilder();
clientBuilder.addServer()
               .host("127.0.0.1")
               .port(11222)
             .security()
               .authentication()
                 .saslMechanism("GSSAPI")
                 .clientSubject(clientSubject)
                 .callbackHandler(new BasicCallbackHandler());

基本回调处理程序

BasicCallbackHandler (如 GSSAPI 示例所示)调用以下回调:

  • NameCallbackPasswordCallback 构建客户端主题。
  • 在 SASL 身份验证过程中调用 AuthorizeCallback

带有令牌回调处理程序的 OAUTHBEARER

使用 TokenCallbackHandler 在 OAuth2 令牌过期前刷新 OAuth2 令牌,如下例所示:

String token = "..."; // Obtain the token from your OAuth2 provider
TokenCallbackHandler tokenHandler = new TokenCallbackHandler(token);
ConfigurationBuilder clientBuilder = new ConfigurationBuilder();
clientBuilder.addServer()
               .host("127.0.0.1")
               .port(11222)
             .security()
               .authentication()
               .saslMechanism("OAUTHBEARER")
               .callbackHandler(tokenHandler);
remoteCacheManager = new RemoteCacheManager(clientBuilder.build());
RemoteCache<String, String> cache = remoteCacheManager.getCache("secured");
// Refresh the token
tokenHandler.setToken("newToken");

Custom CallbackHandler

热备份客户端设置默认 CallbackHandler,将凭据传递给 SASL 机制。在某些情况下,您可能需要提供自定义 回调处理程序,如下例所示:

public class MyCallbackHandler implements CallbackHandler {
   final private String username;
   final private char[] password;
   final private String realm;

   public MyCallbackHandler(String username, String realm, char[] password) {
      this.username = username;
      this.password = password;
      this.realm = realm;
   }

   @Override
   public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
      for (Callback callback : callbacks) {
         if (callback instanceof NameCallback) {
            NameCallback nameCallback = (NameCallback) callback;
            nameCallback.setName(username);
         } else if (callback instanceof PasswordCallback) {
            PasswordCallback passwordCallback = (PasswordCallback) callback;
            passwordCallback.setPassword(password);
         } else if (callback instanceof AuthorizeCallback) {
            AuthorizeCallback authorizeCallback = (AuthorizeCallback) callback;
            authorizeCallback.setAuthorized(authorizeCallback.getAuthenticationID().equals(
                  authorizeCallback.getAuthorizationID()));
         } else if (callback instanceof RealmCallback) {
            RealmCallback realmCallback = (RealmCallback) callback;
            realmCallback.setText(realm);
         } else {
            throw new UnsupportedCallbackException(callback);
         }
      }
   }
}

ConfigurationBuilder clientBuilder = new ConfigurationBuilder();
clientBuilder.addServer()
               .host("127.0.0.1")
               .port(11222)
             .security().authentication()
               .serverName("myhotrodserver")
               .saslMechanism("DIGEST-MD5")
               .callbackHandler(new MyCallbackHandler("myuser","default","qwer1234!".toCharArray()));
注意

自定义 回调处理程序 需要处理特定于您使用的验证机制的回调。但是,这超出了本文档的范围,可提供各种可能的回调类型示例。

3.3.1. 创建 GSSAPI 登录上下文

要使用 GSSAPI 机制,您必须创建一个 LoginContext,以便 Hot Rod 客户端可以获取 Ticket Granting Ticket(TGT)。

流程

  1. 在登录配置文件中定义登录模块。

    gss.conf

    GssExample {
        com.sun.security.auth.module.Krb5LoginModule required client=TRUE;
    };

    对于 IBM JDK:

    gss-ibm.conf

    GssExample {
        com.ibm.security.auth.module.Krb5LoginModule required client=TRUE;
    };

  2. 设置以下系统属性:

    java.security.auth.login.config=gss.conf
    
    java.security.krb5.conf=/etc/krb5.conf
    注意

    krb5.conf 提供您的 KDC 的位置。使用 kinit 命令通过 Kerberos 进行身份验证并验证 krb5.conf

3.3.2. SASL 验证机制

Data Grid Server 使用 Hot Rod 端点支持以下 SASL 验证机制:

验证机制描述Security realm 类型相关详情

PLAIN

以纯文本格式使用凭证。您应该只对加密连接使用 PLAIN 身份验证。

属性 realm 和 LDAP 域

BASIC HTTP 机制类似。

DIGEST-*

使用哈希算法和非ce 值。热 Rod 连接器支持 DIGEST-MD5、 DIGEST-SHA -256、DIGEST-SHA-256DIGEST-SHA-SHA-384DIGEST-SHA-512 哈希算法,按优势顺序。

属性 realm 和 LDAP 域

Digest HTTP 机制类似。

SCRAM-*

除了哈希算法和非ce 值外,还使用 salt 值。热 Rod 连接器支持 SCRAM-SHASCRAM-SHA-256SCRAM-SHA-384SCRAM-SHA-512 哈希算法,按顺序支持 SCRAM-SHA-512 哈希算法。

属性 realm 和 LDAP 域

Digest HTTP 机制类似。

GSSAPI

使用 Kerberos 票据且需要 Kerberos 域控制器。您必须在域配置中添加对应的 kerberos 服务器身份。在大多数情况下,您还可指定 ldap-realm 来提供用户成员资格信息。

Kerberos realm

SPNEGO HTTP 机制类似。

GS2-KRB5

使用 Kerberos 票据且需要 Kerberos 域控制器。您必须在域配置中添加对应的 kerberos 服务器身份。在大多数情况下,您还可指定 ldap-realm 来提供用户成员资格信息。

Kerberos realm

SPNEGO HTTP 机制类似。

EXTERNAL

使用客户端证书。

信任存储域

CLIENT_CERT HTTP 机制类似。

OAUTHBEARER

使用 OAuth 令牌,需要 token-realm 配置。

令牌域

BEARER_TOKEN HTTP 机制类似。

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.