zookeeper开启SASL权限认证

目录

一、SASL介绍

二、使用 SASL 进行身份验证

2.1 服务器到服务器的身份验证

2.2 客户端到服务器身份验证

三、验证功能


一、SASL介绍

默认情况下,ZooKeeper 不使用任何形式的身份验证并允许匿名连接。但是,它支持 Java 身份验证与授权服务(JAAS),可用于使用简单身份验证和安全层(SASL)设置身份验证。zookeeper 支持使用带有本地存储的凭证的 DIGEST-MD5 SASL 机制进行身份验证。

SASL是一种用来扩充C/S模式验证能力的机制认证机制, 全称Simple Authentication and Security Layer。

二、使用 SASL 进行身份验证

JAAS 使用单独的配置文件进行配置。建议将 JAAS 配置文件放在与 ZooKeeper 配置相同的目录中(/zookeeper/conf/)。如文件名是 jaas.conf。ZooKeeper 是集群部署时,必须在所有节点上创建 JAAS 配置文件。

SASL 身份验证是单独配置的,用于服务器对服务器通信( ZooKeeper 实例之间的通信)和客户端对服务器通信( 客户端和 ZooKeeper 之间的通信)。服务器对服务器身份验证仅与具有多个节点的 ZooKeeper 集群相关。

2.1 服务器到服务器的身份验证

对于服务器到服务器身份验证,JAAS 配置文件包含两个部分:

  • 服务器配置
  • 客户端配置

使用 DIGEST-MD5 SASL 机制时,QuorumServer 上下文用于配置身份验证服务器。它必须包含允许所有用户名,以便其以未加密的形式与其密码连接。第二个上下文 QuorumLearner 必须为内置在 ZooKeeper 中的客户端配置。它还包含未加密格式的密码。以下是 DIGEST-MD5 机制的 JAAS 配置文件示例:

QuorumServer {
       org.apache.zookeeper.server.auth.DigestLoginModule required
       user_zookeeper="123456";
};

QuorumLearner {
       org.apache.zookeeper.server.auth.DigestLoginModule required
       username="zookeeper"
       password="123456";
};

除了 JAAS 配置文件外,还必须通过指定以下选项在 ZooKeeper 的配置文件中启用服务器到服务器身份验证:

quorum.auth.enableSasl=true
quorum.auth.learnerRequireSasl=true
quorum.auth.serverRequireSasl=true
quorum.auth.learner.loginContext=QuorumLearner
quorum.auth.server.loginContext=QuorumServer
quorum.cnxn.threads.size=20

# 客户端到服务器身份验证
requireClientAuthScheme=sasl
# authProvider.myId=org.apache.zookeeper.server.auth.SASLAuthenticationProvider
authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider
# authProvider.2=org.apache.zookeeper.server.auth.SASLAuthenticationProvider
# authProvider.3=org.apache.zookeeper.server.auth.SASLAuthenticationProvider

然后,通过系统环境变量设置jaas.conf的路径

"-Djava.security.auth.login.config=/opt/kafka/config/jaas.conf"

2.2 客户端到服务器身份验证

客户端到服务器身份验证在与服务器到服务器身份验证相同的 JAAS 文件中配置。

将服务器上下文添加到 JAAS 配置文件,以配置客户端到服务器的身份验证。对于 DIGEST-MD5 机制,它会配置所有用户名和密码:

QuorumServer {
	org.apache.zookeeper.server.auth.DigestLoginModule required
	user_zookeeper="123456";
};

QuorumLearner {
	org.apache.zookeeper.server.auth.DigestLoginModule required
	username="zookeeper"
	password="1234567";
};

Server {
	org.apache.zookeeper.server.auth.DigestLoginModule required
	user_test="123456";
};

Client {
	org.apache.zookeeper.server.auth.DigestLoginModule required
	username="test"
	password="123456";
};

同时,zookeeper的配置文件中需要添加如下配置:

# 客户端连接是否必须进行SASL认证
sessionRequireClientSASLAuth=true

三、验证功能

1、启动zk集群,zk集群可正常选举

2、编写客户端demo

System.setProperty("java.security.auth.login.config", "E:\\study\\apache-zookeeper-3.8.4-bin\\conf\\jaas.conf");
        System.out.println("********************** start zk ********************** ");
        CountDownLatch countDownLatch = new CountDownLatch(1);
        ZooKeeperAdmin zooKeeper = new ZooKeeperAdmin("127.0.0.1:2181", 5000, event -> {
            System.out.println("触发了事件:" + event.getState());
            countDownLatch.countDown();
        });

        Stat existsName = zooKeeper.exists("/age", watchedEvent -> System.out.println("--" + watchedEvent.getState()));
        if (existsName == null) {
            zooKeeper.create("/age", "18".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } else {
            byte[] data = zooKeeper.getData("/name", false, null);
            System.out.println("--->" + new String(data));
        }

        String srvr = FourLetterWordMain.send4LetterWord("127.0.0.1", 2181, "srvr", false);
        System.out.println(srvr);

  • 13
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值