a pure java solution that worked for me:
public static void main(String[] args) throws NoSuchAlgorithmException {
String zookeeperHost = "localhost:2181";
int sessionTimeoutMs = 300000;
int connectionTimeoutMs = 15000;
boolean isSecure = false;
int maxInFlightRequests = 10;
Time time = Time.SYSTEM;
String metricGroup = "myGroup";
String metricType = "myType";
String username = "alice";
String password = "alice-secret";
KafkaZkClient zkClient = KafkaZkClient.apply(zookeeperHost, isSecure, sessionTimeoutMs, connectionTimeoutMs,
maxInFlightRequests, time, metricGroup, metricType, null, Option.apply(new ZKClientConfig()));
AdminZkClient adminZkClient = new AdminZkClient(zkClient);
HashMap<String, String> userProp = new HashMap<>();
ScramCredential scramCredential = new ScramFormatter(ScramMechanism.SCRAM_SHA_256).generateCredential(password, 4096);
userProp.put(userSecureSchema, ScramCredentialUtils.credentialToString(scramCredential));
Properties configs = adminZkClient.fetchEntityConfig(ConfigType.User(), username);
configs.putAll(userProp);
adminZkClient.changeConfigs(ConfigType.User(), username, configs);
}
you need SASL_SCRAM enabled on kafka.
and you need org.apache.kafka:kafka_2.13:2.5.0 in dependency