Quick revision
The short answer
Set spring.kafka.security.protocol=SASL_SSL, spring.kafka.properties.sasl.mechanism=SCRAM-SHA-512 and a sasl.jaas.config line using ScramLoginModule whose password is a placeholder, ${KAFKA_PASSWORD}, filled from a secret manager or Kubernetes secret at runtime, and point spring.kafka.ssl.trust-store-location at a PKCS12 truststore holding the company CA (or use a PEM SSL bundle). A wrong truststore fails the TLS handshake with SSLHandshakeException: PKIX path building failed, while wrong credentials fail later with SaslAuthenticationException saying authentication failed due to invalid credentials with SASL mechanism SCRAM-SHA-512.
What to remember
In this answer, we saw that a SASL_SSL connection first does a TLS handshake, which needs the company CA in a truststore and a matching host name, and then SCRAM authentication. Spring Boot covers the protocol and truststore with spring.kafka properties, the SASL mechanism and JAAS line go through spring.kafka.properties, and the password comes from an environment variable filled by a secret manager. A wrong truststore shows as PKIX path building failed, and wrong credentials as a SaslAuthenticationException about invalid credentials for SCRAM-SHA-512.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Set spring.kafka.security.protocol=SASL_SSL, spring.kafka.properties.sasl.mechanism=SCRAM-SHA-512 and a sasl.jaas.config line using ScramLoginModule whose password is a placeholder, ${KAFKA_PASSWORD}, filled from a secret manager or Kubernetes secret at runtime, and point spring.kafka.ssl.trust-store-location at a PKCS12 truststore holding the company CA (or use a PEM SSL bundle). A wrong truststore fails the TLS handshake with SSLHandshakeException: PKIX path building failed, while wrong credentials fail later with SaslAuthenticationException saying authentication failed due to invalid credentials with SASL mechanism SCRAM-SHA-512.
Why this question is asked
Almost every production Kafka cluster requires encryption and authentication, so connecting a Spring Boot service to it is a routine task, and still one where a lot of time goes to cryptic errors. The configuration touches three separate concerns: trusting the brokers, proving who we are, and keeping the secret out of source control. Interviewers ask for it to see whether we know which property does what and can read the failures. In this answer, we will recap what the client has to do when it connects, write the configuration, keep the password out of Git, look at the PEM bundle alternative, and then read the errors that appear when the truststore or the credentials are wrong.
1A Quick Recap: What Happens When the Client Connects
With SASL_SSL, every connection goes through two stages. First a TLS handshake: the broker presents its certificate, and the client checks that the certificate was signed by a certificate authority (CA) it trusts and that the certificate's names match the host it connected to. The CA list lives in a truststore, a file of trusted certificates. Our company uses a private CA, which Java does not trust by default, so we must give the client a truststore that contains it.
Second, inside the encrypted connection, SASL authentication: with SCRAM-SHA-512 the client proves it knows the password for user orders-svc through a challenge and response, without sending the password itself. Only after both stages succeed does the client send produce and fetch requests, which the broker then checks against ACLs.
- TCP connect to kafka-1.internal:9093
- TLS: is the broker's CA in our truststore?YesCheck host nameNoSSLHandshakeException: PKIX path building failed
- Host name in the certificate's SANs?YesStart SASLNoSSLHandshakeException: no subject alternative names
- SCRAM: is the password right?YesSend produce and fetch requestsNoSaslAuthenticationException
- ACL allows the operation?YesRequest servedNoAuthorization exception
5 steps in order. Play them, or step through them one at a time.
Each stage has its own error, so the error tells us which stage failed.
2The Spring Boot Configuration
Spring Boot has first-class properties for the bootstrap servers, the security protocol and the truststore. The SASL settings have no dedicated Boot properties, so we pass them through spring.kafka.properties, which hands any key straight to the Kafka client.
Notice three details. The JAAS line ends with a semicolon, which the Kafka client requires; without it the client fails at startup with an error about the JAAS configuration. The ${KAFKA_PASSWORD} placeholder is resolved by Spring from the environment, so the file itself holds no secret. And because these are top-level spring.kafka properties, they apply to every client Boot creates; per-client overrides exist under spring.kafka.producer and spring.kafka.consumer if one client needs something different. Note also that we never set ssl.endpoint.identification.algorithm: its default, https, keeps host name verification on.
3Keeping the Password Out of Git
The configuration file goes into Git; the password must not. Spring resolves ${KAFKA_PASSWORD} from any property source, and the simplest is an environment variable that the platform fills from a secret store. On Kubernetes that is a Secret referenced by the deployment, and the Secret itself is created by a secret operator from a manager such as Vault or the cloud provider's secret manager.
The Kafka client treats sasl.jaas.config as a password-type setting, so when it logs its configuration at startup the value appears as [hidden]. That protects the logs, but not a heap dump or an actuator endpoint that prints environment values, so we keep the actuator env endpoint closed or sanitised as usual.
4The PEM Alternative: an SSL Bundle
Many companies distribute their CA as a PEM file, the text format that begins with BEGIN CERTIFICATE, rather than as a PKCS12 truststore. Spring Boot's SSL bundles accept PEM directly, so we can skip building a truststore at all.
The bundle also makes it easy to reuse the same CA for other clients in the application, such as an HTTP client calling internal services.
5Reading the Errors
When the truststore is wrong, missing the company CA, pointing at the wrong file, or containing only an old CA, the TLS stage fails. The client logs SSLHandshakeException with PKIX path building failed, often wrapped as SslAuthenticationException with a message that SSL handshake failed. PKIX is the name of Java's certificate path validation: it could not build a chain from the broker's certificate to anything it trusts. The client keeps retrying, so the service looks stuck rather than crashed.
When the truststore is right but the credentials are wrong, TLS succeeds and the SASL stage fails. The client throws SaslAuthenticationException with a message that authentication failed during authentication due to invalid credentials with SASL mechanism SCRAM-SHA-512. The same message appears if the user does not exist at all, because the broker deliberately does not say which part was wrong. If instead the mechanism is not enabled on the listener, the error says the mechanism is unsupported.
| Error | Stage | Usual cause |
|---|---|---|
| SSLHandshakeException: PKIX path building failed | TLS | Company CA not in the truststore |
| SSLHandshakeException: No subject alternative names | TLS | Connected to a name not in the certificate |
SaslAuthenticationException: invalid credentials | SASL | Wrong password or unknown user |
UnsupportedSaslMechanismException | SASL | Listener does not enable SCRAM-SHA-512 |
TopicAuthorizationException | After login | No ACL for the topic |
6Follow-up questions
Why not disable host name verification to make the error go away?
Because it removes half of what TLS gives us. Without host name verification, any certificate signed by our CA, including one stolen from another service, could impersonate a broker. The right fix is to connect to the advertised host names that are in the broker certificates' SANs.
Where do the SCRAM credentials come from?
The platform team creates them in the cluster with kafka-configs.sh, which stores a salted hash in the KRaft metadata, and puts the password into the secret manager for the service. Rotating it means adding the new password in the cluster and the secret store, then restarting or reloading the service.
Do producers and consumers need separate settings?
No. The top-level spring.kafka properties apply to all of them. We only use spring.kafka.producer.properties or spring.kafka.consumer.properties when one client must differ, for example a consumer of another cluster.
7Conclusion
In this answer, we saw that a SASL_SSL connection first does a TLS handshake, which needs the company CA in a truststore and a matching host name, and then SCRAM authentication. Spring Boot covers the protocol and truststore with spring.kafka properties, the SASL mechanism and JAAS line go through spring.kafka.properties, and the password comes from an environment variable filled by a secret manager. A wrong truststore shows as PKIX path building failed, and wrong credentials as a SaslAuthenticationException about invalid credentials for SCRAM-SHA-512.
Open with
In application.yml I set spring.kafka.bootstrap-servers to the brokers on port 9093 and spring.kafka.security.protocol to SASL_SSL. Boot has no dedicated SASL properties, so under spring.kafka.properties I set sasl.mechanism to SCRAM-SHA-512 and sasl.jaas.config to the ScramLoginModule required line with username orders-svc and password set to a placeholder, KAFKA_PASSWORD, ending with a semicolon.
Then, if they want more
For trust I point spring.kafka.ssl.trust-store-location at a PKCS12 truststore with the company CA, with its password also a placeholder; or, if the CA is a PEM file, I define an SSL bundle and set spring.kafka.ssl.bundle.
The placeholders come from environment variables that Kubernetes fills from a Secret synced from Vault or the cloud secret manager, so nothing secret is in Git, and the Kafka client logs the JAAS config as hidden. I leave ssl.endpoint.identification.algorithm at its default, https, so host names are verified.
If the truststore is wrong, the TLS handshake fails with SSLHandshakeException, PKIX path building failed, which means Java could not build a chain to a trusted CA. If the truststore is fine but the password or user is wrong, TLS succeeds and SASL fails with SaslAuthenticationException, authentication failed due to invalid credentials with SASL mechanism SCRAM-SHA-512. Knowing the stages tells me where to look.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Show the Spring Boot configuration for a service that connects to Kafka over SASL_SSL...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-operations/7abc36c5e0/