00:00

Kafka Authentication with SSL/TLS and SASL

Last updated:

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.

ProtocolEncryptionClient authenticationUse it for
PLAINTEXTNoNone (principal ANONYMOUS)Local development only
SSLTLSOptional: TLS client certificates (mutual TLS)Service-to-service traffic with a certificate infrastructure
SASL_PLAINTEXTNoSASL (SCRAM, OAUTHBEARER, GSSAPI, PLAIN)Rarely appropriate; credentials or tokens cross the network unencrypted
SASL_SSLTLSSASLThe 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

TLS and SASL authentication steps for a Kafka clientA client connects to a broker's SASL_SSL listener. Step 1, the TCP connection is opened. Step 2, the TLS handshake: the broker presents its certificate, the client checks it against its truststore and verifies that the host name matches the certificate. Step 3, the SASL exchange: the client proves its identity with SCRAM credentials or an OAuth token, and the broker establishes the principal User:orders-service. Step 4, every request is authorized against ACLs for that principal. Clientorders-service BrokerSASL_SSL listener :9094 1. TCP connect 2. TLS handshake: broker certificate client checks CA (truststore) and host name (SAN) 3. SASL: SCRAM proof or OAuth token broker sets principal User:orders-service 4. Produce / Fetch requests each request is checked against ACLs for the principal
TLS proves who the broker is and encrypts the channel; SASL proves who the client is; ACLs decide what the client may do.

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:

server.properties (listeners)
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.

server.properties (TLS for the CLIENTS listener)
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:

server.properties (mTLS on the INTERNAL listener)
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.

client.properties (mTLS client)
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:

server.properties (SCRAM on the CLIENTS listener)
sasl.enabled.mechanisms=SCRAM-SHA-512
listener.name.clients.scram-sha-512.sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required;
client.properties (SCRAM client)
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.

server.properties (OAuth validation on the CLIENTS listener)
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:

client.properties (OAuth client)
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:

application.yml (SCRAM over TLS)
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

SymptomLikely causeFix
SSLHandshakeException: No subject alternative names matching ...The host name used by the client is not in the broker certificate’s SAN listReissue the certificate with all advertised host names; do not disable hostname verification
PKIX path building failedClient truststore does not contain the CA that signed the broker certificateAdd the CA (or full chain) to the truststore
Authentication failed during authentication due to invalid credentials with SASL mechanism SCRAM-SHA-512Wrong password, missing user, or user created for SCRAM-SHA-256 onlyCheck the user with kafka-configs.sh --describe --entity-type users
Client disconnects repeatedly, or reports unexpected handshake errors, against a TLS portClient uses PLAINTEXT or SASL_PLAINTEXT against an SSL/SASL_SSL listenerMatch security.protocol to the listener’s protocol
OAuth tokens rejected with audience or issuer errorsToken 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 loginAuthentication worked; the principal has no ACL for the operationGrant 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.

13. References