kafka java客户端:classloader找不到sasl/scram登录类

v09wglhw  于 2021-06-04  发布在  Kafka
关注(0)|答案(1)|浏览(1038)

构建一个spi将事件推送到kafka中,作为keydepose6.0.1中的ear部署,keydepose6.0.1使用wildfly服务器,打包在基于 jboss/keycloak:6.0.1 .
我遇到:kafka producer-org.apache.kafka.common.serialization.stringserializer找不到
所以我采用了建议的解决方案 Thread.currentThread().setContextClassLoader(null); .
这似乎工作良好,我当地的Kafka在港口9092,没有认证。一旦我按此处所述进行身份验证:

String jaasTemplate = "org.apache.kafka.common.security.scram.ScramLoginModule required username=\"%s\" password=\"%s\";";
String jaasCfg = String.format(jaasTemplate, username, password);

Properties props = new Properties();
props.put("sasl.jaas.config", jaasCfg);
// ...
Thread.currentThread().setContextClassLoader(null);
KafkaProducer<String, String> producer = new KafkaProducer<>(props);

我遇到了错误:

org.apache.kafka.common.KafkaException: javax.security.auth.login.LoginException: unable to find LoginModule class: org.apache.kafka.common.security.scram.ScramLoginModule
    at org.apache.kafka.common.network.SaslChannelBuilder.configure(SaslChannelBuilder.java:160)
    at org.apache.kafka.common.network.ChannelBuilders.create(ChannelBuilders.java:146)
    at org.apache.kafka.common.network.ChannelBuilders.clientChannelBuilder(ChannelBuilders.java:67)
    at org.apache.kafka.clients.ClientUtils.createChannelBuilder(ClientUtils.java:99)
    at org.apache.kafka.clients.producer.KafkaProducer.newSender(KafkaProducer.java:441)
    at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:422)

我怀疑这是由于将classloader设置为 null ,但我不确定。
jaas字符串提到了这一点 org.apache.kafka.common.security.scram.ScramLoginModule . 我尝试不使用jaas,而是使用普通用户名+密码,如下所示:

Properties props = new Properties();
props.put("sasl.username", username);
props.put("sasl.password", password);
// ...
Thread.currentThread().setContextClassLoader(null);
KafkaProducer<String, String> producer = new KafkaProducer<>(props);

但这也导致了一个例外,只是提到一个不同的类无法定位:

org.apache.kafka.common.KafkaException: javax.security.auth.login.LoginException: unable to find LoginModule class: org.jboss.as.security.remoting.RemotingLoginModule
    at org.apache.kafka.common.network.SaslChannelBuilder.configure(SaslChannelBuilder.java:160)
    at org.apache.kafka.common.network.ChannelBuilders.create(ChannelBuilders.java:146)
    at org.apache.kafka.common.network.ChannelBuilders.clientChannelBuilder(ChannelBuilders.java:67)
    at org.apache.kafka.clients.ClientUtils.createChannelBuilder(ClientUtils.java:99)
    at org.apache.kafka.clients.producer.KafkaProducer.newSender(KafkaProducer.java:441)
    at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:422)

我怎样才能做到,那就是 org.apache.kafka.common.security.scram.ScramLoginModule 找到了吗?谢谢!

xcitsw88

xcitsw881#

警告:丑陋的黑客!
在创建kafkaproducer之前,设置如下所示的上下文类加载器:

Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());

相关问题