由于业务需求,需要给Zookeeper去配置一个权限,什么类型都可以。查阅技术文档后,选择了可以通过配置文件配置的SASL模型。auth、digest都无法通过配置文件去配置用户名和密码。然而在写Java连接代码时,遇到了问题
问题
在Java代码中,通常使用addAuthInfo(schema,auth)
来给连接添加权限,digest、auth都是通过该方法添加权限,而在尝试之后,发现无论如何都没法通过addAuthInfo(schema,auth)
通过SASL认证。
原因
在查阅官方文档后,发现:若使用SASL方式认证,那么addAuth方法将会失效。wiki说明地址
addauth
The SASL authentication scheme differs from certain other schemes in that the "addauth " command has no effect if scheme is “sasl”. This is because authentication is performed using SASL-enabled token exchange immediately after connection, rather than occuring any time after connection, as addauth is.
SASL认证方案与某些其他方案的不同之处在于“addauth <scheme><auth>"如果scheme为"sasl"则命令无效。这是因为身份验证是在连接之后立即使用支持sasl的令牌交换执行的,而不是像addauth那样在连接之后的任何时间执行。