title: Kafka Java Client: ClassLoader doesn't find SASL/Scram Login Class
body: <p>Building an <strong>SPI</strong> to push events into <strong>Kafka</strong>, to get deployed as an <strong>EAR</strong> inside <strong>Keycloak 6.0.1</strong>, which uses the <strong>WildFly</strong> Server, packaged in a <strong>Docker</strong> image based on <code>jboss/keycloak:6.0.1</code>.</p>
  
  <p>I ran into: <a href="https://stackoverflow.com/questions/37363119/kafka-producer-org-apache-kafka-common-serialization-stringserializer-could-no">Kafka Producer - org.apache.kafka.common.serialization.StringSerializer could not be found</a></p>
  
  <p>So I applied the suggested solution of setting <code>Thread.currentThread().setContextClassLoader(null);</code>.</p>
  
  <p>This seems to work fine for my local Kafka on port 9092, without authentication. As soon as I authenticate as described <a href="https://github.com/CloudKarafka/java-kafka-example/blob/master/src/main/java/KafkaExample.java" rel="nofollow noreferrer">here</a>:</p>
  
  <pre class="lang-java prettyprint-override"><code>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&lt;String, String&gt; producer = new KafkaProducer&lt;&gt;(props);
  </code></pre>
  
  <p>I run into the error:</p>
  
  <pre><code>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.&lt;init&gt;(KafkaProducer.java:422)
  </code></pre>
  
  <p>I suspect that this is caused by setting the ClassLoader to <code>null</code>, but I'm not certain.</p>
  
  <p>The JAAS string mentions this <code>org.apache.kafka.common.security.scram.ScramLoginModule</code>. I tried not using JAAS but instead plain username + password like so:</p>
  
  <pre class="lang-java prettyprint-override"><code>Properties props = new Properties();
  props.put("sasl.username", username);
  props.put("sasl.password", password);
  // ...
  Thread.currentThread().setContextClassLoader(null);
  KafkaProducer&lt;String, String&gt; producer = new KafkaProducer&lt;&gt;(props);
  </code></pre>
  
  <p>But that also resulted in an Exception, just mentioning a different Class could not be located:</p>
  
  <pre><code>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.&lt;init&gt;(KafkaProducer.java:422)
  </code></pre>
  
  <p>How can I make it, that the <code>org.apache.kafka.common.security.scram.ScramLoginModule</code> is found? Thanks!</p>