Microservices and Kerberos Authentication

DZone 's Guide to

Microservices and Kerberos Authentication

How to use Kerberos authentication with microservice architectures and API gateways.

· Integration Zone ·
Free Resource

Kerberos authentication is used to secure a variety of big data products like Apache Hadoop and more recently Apache Kafka (>0.9). In case of Hadoop it is used to authenticate each user and service in order to use the Hadoop ecosystem. Also a lot of modern NoSQL databases offer support for Kerberos, for example Apache Cassandra and MongoDB. Kerberos authentication can also be useful in microservice architectures. It can be used to achieve single sign on functionality. A property that is less known is the possibility to delegate tokens to sub services.

In a microservice architecture you probably have an API gateway which is a single entry point for a native mobile app or a HTML5 application. The gateway can forward requests to several microservices and aggregates the results in a single response.

Image title

Source: http://microservices.io/patterns/apigateway.html

The challenge here is to know which user is logged in at the backing REST services. In case of Kerberos it is possible to delegate the user credentials to these REST services. These microservice can validate the credentials again.

How Does this Work in Java?

Since Java is enterprise ready there is support for Kerberos since the early days (JDK 1.4) by the GSS-API. However, this API is pretty awkward to use. Luckily there is a kerberos plugin for Spring Security but this plugin has no out of the box support for credential delegation. So I have written some code to create a delegated service ticket: (note that this ticket is also forwardable)

import org.ietf.jgss.*;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.kerberos.authentication.KerberosServiceRequestToken;

import javax.security.auth.Subject;
import java.security.PrivilegedExceptionAction;

public static byte[] createServiceToken(String serviceName) throws Exception {
    KerberosServiceRequestToken authentication = (KerberosServiceRequestToken)

    Subject subject = authentication.getTicketValidation().subject();

    return Subject.doAs(subject, (PrivilegedExceptionAction<byte[]>) () -> {
        GSSManager manager = GSSManager.getInstance();
        GSSName name = manager.createName("HTTP@" + serviceName, GSSName.NT_HOSTBASED_SERVICE);
        GSSContext context = manager.createContext(name, 
        byte[] serviceToken = context.initSecContext(authentication.getToken(), 0, authentication.getToken().length);
        return serviceToken;

Some explanation:

Line 9,10    Retrieves the KeberosServiceToken from the Spring Security context

Line 12        Get the current logged in subject

Line 14        Run the lambda expression as this subject

Line 15        Get the GSSManager

Line 16        Create a GSSName for the REST endpoint

Line 17-20  Create a new GSSContext for this REST endpoint

Line 21        Request a forwardable Kerberos token, so the new service ticket is also forwardable

Line 22        Initiate the secure context with the current Kerberos token to get a new service ticket

Line 23        Close the context and

Line 24         Return the new ticket

A colleague of mine also created an extension to the Spring Security Kerberos plugin to delegate the user credentials to an LDAP server. So, there is no need to use a bind user (with special permissions) anymore. The major advantage is that you can create an audit trail of any user logged in to your systems.

authentication ,java ,microservices ,security

Published at DZone with permission of Jethro Bakker . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}