Kafka Authentication with SSL/TLS and SASL
Kafka authentication with SSL/TLS and SASL answers two questions for every connection: is the traffic encrypted, and who is on the other side? TLS encrypts the connection and proves the broker’s identity; the client then proves its own identity either with a TLS client certificate (mutual TLS) or with a SASL mechanism such as SCRAM or OAuth.
This tutorial goes deeper than the Kafka security overview. It explains the four security protocols, how a TLS and SASL handshake works step by step, how to design listeners, and how to configure mutual TLS, SCRAM and OAuth/OIDC on brokers, Java clients and Spring Boot, with a troubleshooting guide for the errors you will meet.
1. The Four Security Protocols
Every Kafka listener uses one of four security protocols, combining two independent choices: encrypt with TLS or not, and authenticate with SASL or not.
| Protocol | Encryption | Client authentication | Use it for |
|---|---|---|---|
PLAINTEXT | No | None (principal ANONYMOUS) | Local development only |
SSL | TLS | Optional: TLS client certificates (mutual TLS) | Service-to-service traffic with a certificate infrastructure |
SASL_PLAINTEXT | No | SASL (SCRAM, OAUTHBEARER, GSSAPI, PLAIN) | Rarely appropriate; credentials or tokens cross the network unencrypted |
SASL_SSL | TLS | SASL | The usual production choice for applications |
Note that the protocol named SSL actually uses TLS; the name is historical. Modern Java versions negotiate TLS 1.3 or 1.2.
2. What Happens When a Client Connects
With mutual TLS, step 3 is replaced by the client presenting its own certificate during step 2; the principal is then derived from the certificate’s distinguished name. In both cases the result is a principal, such as User:orders-service, which ACL authorization uses for every request.
3. Designing Listeners
A broker can expose several listeners, each with its own name, port and protocol. A common production layout separates traffic by trust level:
listeners=INTERNAL://:9093,CLIENTS://:9094,CONTROLLER://:9095
advertised.listeners=INTERNAL://broker-1.internal:9093,CLIENTS://kafka-1.example.com:9094
listener.security.protocol.map=INTERNAL:SSL,CLIENTS:SASL_SSL,CONTROLLER:SSL
inter.broker.listener.name=INTERNAL
controller.listener.names=CONTROLLER
- INTERNAL carries replication between brokers, here authenticated with mutual TLS.
- CLIENTS is what applications use, with SASL over TLS.
- CONTROLLER carries KRaft metadata traffic. Secure it too: a client that can talk to the controllers can affect the whole cluster.
Settings can be scoped to one listener with the prefix listener.name.<listener>. (in lower case), for example a different keystore or JAAS configuration per listener.
4. TLS: Certificates, Truststores and Hostname Verification
Each broker needs a keystore with its private key and a certificate signed by a certificate authority (CA). Clients need a truststore containing that CA. Kafka supports JKS, PKCS12 and, since Kafka 2.7, PEM keys and certificates, either as files or inline in the configuration.
listener.name.clients.ssl.keystore.type=PKCS12
listener.name.clients.ssl.keystore.location=/etc/kafka/secrets/kafka-1.p12
listener.name.clients.ssl.keystore.password=${file:/etc/kafka/secrets/creds.properties:keystore.password}
listener.name.clients.ssl.truststore.type=PKCS12
listener.name.clients.ssl.truststore.location=/etc/kafka/secrets/ca-truststore.p12
listener.name.clients.ssl.truststore.password=${file:/etc/kafka/secrets/creds.properties:truststore.password}
The ${file:...} syntax uses Kafka’s file configuration provider (enable it with config.providers=file and config.providers.file.class=org.apache.kafka.common.config.provider.FileConfigProvider), so passwords are not written into server.properties itself.
Hostname verification is on by default for clients and brokers (ssl.endpoint.identification.algorithm=https): the host name the client connects to must appear in the certificate’s Subject Alternative Names. It protects against man-in-the-middle attacks, so every broker certificate must contain the advertised host names. Disabling it by setting the property to an empty string is a common shortcut that the Kafka documentation explicitly warns against.
5. Mutual TLS Authentication
With mutual TLS (mTLS), the client also presents a certificate, and the broker uses it to identify the client:
listener.name.internal.ssl.client.auth=required
# map the certificate DN to a short principal name: CN=orders-service,OU=apps,O=Acme -> orders-service
ssl.principal.mapping.rules=RULE:^CN=([a-zA-Z0-9.-]*),.*$/$1/L,DEFAULT
ssl.client.auth has three values: none (default), required and requested. The documentation discourages requested, because clients without a certificate still connect and a misconfiguration goes unnoticed. Without mapping rules, the principal is the full distinguished name, such as User:CN=orders-service,OU=apps,O=Acme, which makes ACLs long and brittle.
security.protocol=SSL
ssl.truststore.location=/etc/orders/ca-truststore.p12
ssl.truststore.password=changeit
ssl.keystore.location=/etc/orders/orders-service.p12
ssl.keystore.password=changeit
mTLS fits environments that already issue certificates to services, such as a service mesh or an internal PKI. Certificate rotation is then the main operational task: renew before expiry, and keep the CA in the truststores during the transition.
6. SASL/SCRAM over TLS
SCRAM authenticates with a user name and password without sending the password itself; in KRaft clusters the salted credentials are stored in the metadata log. The security overview walks through creating users; the broker side of a SASL_SSL listener looks like this:
sasl.enabled.mechanisms=SCRAM-SHA-512
listener.name.clients.scram-sha-512.sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required;
security.protocol=SASL_SSL
sasl.mechanism=SCRAM-SHA-512
sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required \
username="orders-service" password="${ORDERS_KAFKA_PASSWORD}";
ssl.truststore.location=/etc/orders/ca-truststore.p12
ssl.truststore.password=changeit
The ${ORDERS_KAFKA_PASSWORD} placeholder is resolved by your deployment tooling or Spring Boot, not by the Kafka client. Kafka accepts only SHA-256 and SHA-512 for SCRAM, with at least 4096 iterations. Never use the PLAIN mechanism without TLS, because it sends the password as is.
7. SASL/OAUTHBEARER with an OIDC Provider
With OAUTHBEARER, clients obtain a JSON Web Token from an identity provider (Keycloak, Okta, Microsoft Entra ID and so on) using the OAuth client-credentials grant, and send it to the broker, which validates it with the provider’s public keys. This lets you manage Kafka identities in the same place as other service identities, with short-lived tokens instead of long-lived passwords.
sasl.enabled.mechanisms=OAUTHBEARER
listener.name.clients.oauthbearer.sasl.jaas.config=org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule required;
listener.name.clients.oauthbearer.sasl.server.callback.handler.class=org.apache.kafka.common.security.oauthbearer.OAuthBearerValidatorCallbackHandler
listener.name.clients.oauthbearer.sasl.oauthbearer.jwks.endpoint.url=https://idp.example.com/realms/acme/protocol/openid-connect/certs
listener.name.clients.oauthbearer.sasl.oauthbearer.expected.audience=kafka-prod
listener.name.clients.oauthbearer.sasl.oauthbearer.expected.issuer=https://idp.example.com/realms/acme
In Kafka 4.4, a broker configured with a JWKS endpoint refuses to start unless the expected audience and issuer are also set, so tokens issued for other applications are rejected. Clients use the built-in client-credentials support:
security.protocol=SASL_SSL
sasl.mechanism=OAUTHBEARER
sasl.jaas.config=org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule required ;
sasl.oauthbearer.token.endpoint.url=https://idp.example.com/realms/acme/protocol/openid-connect/token
sasl.oauthbearer.client.credentials.client.id=orders-service
sasl.oauthbearer.client.credentials.client.secret=${ORDERS_CLIENT_SECRET}
sasl.oauthbearer.scope=kafka
The client refreshes the token automatically before it expires. By default the principal is taken from the token’s sub claim, which is the name you use in ACLs.
8. Configuring Spring Boot Clients
Spring Boot exposes the common security settings as properties; everything else is passed through spring.kafka.properties. Spring Boot’s SSL bundles keep certificate configuration in one place:
spring:
ssl:
bundle:
pem:
kafka-ca:
truststore:
certificate: classpath:certs/kafka-ca.pem
kafka:
bootstrap-servers: kafka-1.example.com:9094,kafka-2.example.com:9094
security:
protocol: SASL_SSL
ssl:
bundle: kafka-ca
properties:
sasl.mechanism: SCRAM-SHA-512
sasl.jaas.config: >-
org.apache.kafka.common.security.scram.ScramLoginModule required
username="${KAFKA_USER}" password="${KAFKA_PASSWORD}";
Spring resolves ${KAFKA_USER} and ${KAFKA_PASSWORD} from the environment or a secret store before the value reaches the Kafka client. The same settings apply to producers, consumers, admin clients and Kafka Streams created by Spring Boot.
9. Troubleshooting Connection Errors
| Symptom | Likely cause | Fix |
|---|---|---|
SSLHandshakeException: No subject alternative names matching ... | The host name used by the client is not in the broker certificate’s SAN list | Reissue the certificate with all advertised host names; do not disable hostname verification |
PKIX path building failed | Client truststore does not contain the CA that signed the broker certificate | Add the CA (or full chain) to the truststore |
Authentication failed during authentication due to invalid credentials with SASL mechanism SCRAM-SHA-512 | Wrong password, missing user, or user created for SCRAM-SHA-256 only | Check the user with kafka-configs.sh --describe --entity-type users |
| Client disconnects repeatedly, or reports unexpected handshake errors, against a TLS port | Client uses PLAINTEXT or SASL_PLAINTEXT against an SSL/SASL_SSL listener | Match security.protocol to the listener’s protocol |
| OAuth tokens rejected with audience or issuer errors | Token issued for a different audience, or issuer URL differs (for example a trailing slash) | Align the IdP client configuration with expected.audience and expected.issuer |
TopicAuthorizationException after a successful login | Authentication worked; the principal has no ACL for the operation | Grant the needed ACLs; see ACL authorization |
10. Best Practices
- Encrypt every listener outside a local laptop, including inter-broker and controller traffic.
- Prefer SASL_SSL with SCRAM-SHA-512 or OAUTHBEARER for applications, or mTLS where a PKI already exists.
- Keep hostname verification on and put every advertised host name in the certificates.
- Use one identity per application, never a shared “kafka” user, so ACLs and audit logs mean something.
- Keep secrets out of files under version control: use configuration providers, environment variables or a secret manager.
- Automate certificate and credential rotation and alert well before certificates expire.
- Remember the cost of TLS: it uses broker CPU and disables the zero-copy send path, so size brokers accordingly.
11. Summary
Kafka listeners combine encryption (TLS or none) with authentication (SASL or none). TLS encrypts traffic and proves the broker’s identity through certificates and hostname verification; clients then authenticate with mutual TLS, SCRAM or OAuth tokens, and the resulting principal is used for ACL checks. Design separate listeners for internal, client and controller traffic, use SASL_SSL or mTLS everywhere outside development, keep hostname verification enabled, and manage certificates and secrets as part of normal operations.
12. Related Reading
- Kafka security overview: the layers of Kafka security and SCRAM user setup
- Kafka ACL authorization: what authenticated principals may do
- Kafka Docker setup: listeners and advertised addresses in containers
- What is OAuth 2.0: the client-credentials flow used by OAUTHBEARER
13. References
Kafka SSL and SASL Authentication Interview Questions and Answers
Common interview questions on Kafka SSL and SASL Authentication, from basic to scenario-based, with short answers you can explain in your own words.
Basic questions
Q. What is the difference between the SSL, SASL_PLAINTEXT and SASL_SSL security protocols?
Answer: SSL encrypts the connection with TLS and can authenticate clients with certificates (mutual TLS). SASL_PLAINTEXT authenticates clients with a SASL mechanism but does not encrypt, so tokens or credentials and all data travel in clear text. SASL_SSL combines both: TLS encryption plus SASL authentication, which is the usual production choice for applications.
Q. What is the difference between one-way TLS and mutual TLS in Kafka?
Answer: In one-way TLS only the broker presents a certificate; the client verifies it, and the connection is encrypted, but the client is not identified by TLS (it may still authenticate with SASL). In mutual TLS, enabled with ssl.client.auth=required, the client also presents a certificate signed by a trusted CA, and the broker derives the client’s principal from the certificate’s distinguished name.
Intermediate questions
Q. Why is hostname verification important, and how is it configured?
Answer: A valid certificate only proves that some trusted CA issued it; hostname verification proves it belongs to the server you meant to reach, which prevents man-in-the-middle attacks. Kafka enables it by default with ssl.endpoint.identification.algorithm=https, so each broker certificate must list its advertised host names as Subject Alternative Names. Setting the property to an empty string disables the check and should be avoided.
Q. Why is ssl.client.auth=requested discouraged?
Answer: With requested, the broker asks for a client certificate but still accepts clients that do not present one. A misconfigured client then connects without being authenticated by TLS, and the problem goes unnoticed. Use required on listeners that rely on mutual TLS, and none where clients authenticate with SASL.
Q. How does the broker validate OAUTHBEARER tokens, and why must the audience and issuer be configured?
Answer: With OAuthBearerValidatorCallbackHandler, the broker downloads the identity provider’s public keys from the JWKS endpoint, verifies each token’s signature and expiry, and takes the principal from the sub claim. Checking the expected audience and issuer ensures the token was issued by your provider for this Kafka cluster, not for another application; since Kafka 4.4 a broker with a JWKS endpoint refuses to start unless both are set.
Tricky and scenario questions
Q. After broker certificates were renewed, clients fail with "No subject alternative names matching". What happened, and what is the wrong fix?
Answer: The new certificates do not contain the host names that clients connect to, usually the advertised listener names, so hostname verification fails. The wrong fix is to disable verification on all clients. The right fix is to reissue the certificates with every advertised host name in the SAN list, and to add that check to the certificate-renewal process.
Q. Your cluster serves internal services that already have certificates and external partners who do not. How would you design the listeners?
Answer: Use separate listeners. An internal listener with SSL and ssl.client.auth=required authenticates services by certificate, with principal mapping rules that turn DNs into short names. An external listener with SASL_SSL and SCRAM or OAUTHBEARER authenticates partners without a PKI. Keep replication and controller traffic on their own encrypted listeners, and give every client its own principal so ACLs can limit each one.