Code Monkey home page Code Monkey logo

kerb4j's Introduction

Kerb4J

Java CI Maven Central

Industry standard library for working with Kerberos/SPNEGO authentication in Java in 2020+.

Main features:

  • Getting group membership information from Kerberos token - no need to query LDAP
  • Proper caching of tickets - make just one request to domain controller and cache the ticket on both client and server sides
  • Flexible SPN resolution - you own the code and do not have to rely on magic SPN calculation algorithms

Installation

Kerb4J is available from Maven Central repo:

Client

<dependency>
    <groupId>com.kerb4j</groupId>
    <artifactId>kerb4j-client</artifactId>
    <version>0.1.2</version>
</dependency>

Spring Security

<dependency>
    <groupId>com.kerb4j</groupId>
    <artifactId>kerb4j-server-spring-security</artifactId>
    <version>0.1.2</version>
</dependency>

Tomcat

<dependency>
    <groupId>com.kerb4j</groupId>
    <artifactId>kerb4j-server-tomcat</artifactId>
    <version>0.1.2</version>
</dependency>

SPNEGO/Kerberos Client

Kerb4J provides efficient way to create Kerberos/SPNEGO HTTP Clients. Main two classes you'll need are SpnegoClient and SpnegoContext

  • SpnegoClient provides API for authenticating client in KDC (e.g. in Active Directory Domain Controller).
  • SpnegoContext is responsible for accessing downstream systems, creating and validating appropriate security HTTP headers.

SpnegoClient supports authentication using name and password, keytab file or ticket cache.

Example usage:

SpnegoClient spnegoClient = SpnegoClient.loginWithKeyTab("svc_consumer", "/opt/myapp/consumer.keytab");

SpnegoContext allows creating 'Authorization: Negotiate XXXXX' header and optionally validating WWW-Authenticate response header for SPNEGO mutual authentication

Example usage:

URL url = new URL("http://api.provider.acme.com/api/operation1");
SpnegoContext context = spnegoClient.createContext("http://provider.acme.com"); // Will result in HTTP/provider.acme.com SPN
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", context.createTokenAsAuthroizationHeader());

SPN resolution

Kerb4J allows you to specify SPN for the downstream system manually without doing any reverse DNS lookups, canonicalizations, e.t.c. You can even use SPN HTTP/foo for calling service bar

Caching tickets

Consider you want to make a million of HTTP requests to Kerberos-protected server. Kerb4J will allow you to make just two requests to the KDC (e.g. in Active Directory Domain Controller).

One SpnegoClient is created, Kerb4J will make first request for TGT (authentication). TGT will be cached and renewed only when tickets expired. Reuse the SpnegoClient instance for all requests you want to make using the same credentials.

When you create first SpnegoContext instance for the given SPN, Kerb4J will make another request for a service ticket. This service ticket will be reused when creating new SpnegoContext instances from the same SpnegoClient.

So the rule of thumb - reuse the same SpnegoClient instance (it is threadsafe by the way), create new SpnegoContext instance for each request.

SPNEGO/Kerberos Server

Validating Kerberos/SPNEGO tickets on server side is even simpler than client side.

Use SpnegoClient and authenticate in KDC (e.g. in Active Directory Domain Controller) using your server account. Call spnegoClient.createAcceptContext() method to create a SpnegoContext instance responsible for authenticating your client. Pass decoded SPNEGO token (Base64 decoded value of token in 'Authorization: Negotiate' header) to spnegoContext.acceptToken method to validate it.

Kerb4J comes with an Authenticator for Apache Tomcat (kerb4j-server-tomcat artifact) as well as authentication provider for Spring Security (See kerb4j-server-spring-security)

Extracting groups from Kerberos ticket generated by Active Directory

Spnego allows you to extract user groups from SPNEGO token (one sent from client to server) without making any additional requests to Active Directory.

String negotiateHeaderValue = request.getHeader("Authorization").substring(10);
byte[] decoded = Base64.decodeBase64(negotiateHeaderValue);
SpnegoInitToken spnegoInitToken = new SpnegoInitToken(decoded);
SpnegoKerberosMechToken spnegoKerberosMechToken = spnegoInitToken.getSpnegoKerberosMechToken();
Pac pac = spnegoKerberosMechToken.getPac(spnegoClient.getKerberosKeys());
PacLogonInfo logonInfo = pac.getLogonInfo();
List<String> roles = Stream.of(logonInfo.getGroupSids()).map(PacSid::toHumanReadableString).collect(Collectors.toList());

This functionality is specific to Microsoft Active Directory and supported both by Kerb4J Tomcat and Spring Security integrations.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.