This guide takes a Spring Boot application from plain HTTP to mutual TLS with SPIFFE identities. You will add the starter, enable mTLS with one property, restrict who may call you, and call another service securely.

Prerequisites

  • Java 21 or later and Spring Boot 4.2.0.

  • A SPIFFE agent on the machine or node where the app runs: a SPIRE agent or an IDIRA SWA swa-agent. No agent at all? See SWA agentless mode.

Step 1: Add the starter

The starter is not on Maven Central yet. Until the first release, install it into your local Maven repository from source:

git clone https://github.com/spiffe-spring-boot/spiffe-spring-boot.git
cd spiffe-spring-boot && ./mvnw install -DskipTests

Then add the dependency:

Maven
<dependency>
    <groupId>org.spiffe-spring-boot</groupId>
    <artifactId>spiffe-spring-boot-starter</artifactId>
    <version>0.1.0-SNAPSHOT</version>
</dependency>
Gradle
repositories {
    mavenLocal() // until the first release on Maven Central
    mavenCentral()
}

dependencies {
    implementation("org.spiffe-spring-boot:spiffe-spring-boot-starter:0.1.0-SNAPSHOT")
}

Step 2: Turn on mutual TLS

Point the server at the spiffe SSL bundle the starter registers, and ask clients for their certificate:

application.yaml
server:
  ssl:
    bundle: spiffe        # present our SVID, verify callers against the trust bundle
    client-auth: need     # every caller must present an SVID
application.properties
server.ssl.bundle=spiffe
server.ssl.client-auth=need

The server now presents the workload’s X.509-SVID and accepts any caller whose SVID chains to a trusted bundle. When the agent rotates the SVID, the server picks up the new one without a restart.

Step 3: Choose who may call you

Callers from your trust domain can all connect so far. Restrict them to the SPIFFE IDs you expect:

spiffe:
  mtls:
    server:
      allowed-ids:
        - spiffe://example.org/ns/shop/sa/frontend    # one exact workload
        - spiffe://example.org/ns/ops/*               # anything under a path

A request with no SVID gets 401; a caller that is not on the list gets 403. Health probes under /actuator/** and /error are never blocked; change that with spiffe.mtls.server.exclude-paths.

To know who is calling inside a controller, declare a @SpiffePeer argument:

@RestController
class HelloController {

    @GetMapping("/hello")
    String hello(@SpiffePeer SpiffeId caller) {
        return "Hello, " + caller;
    }

}

Step 4: Call another service over mTLS

Build a RestClient from the same bundle. It presents your SVID and checks the server’s SPIFFE identity:

@Service
class OrdersClient {

    private final RestClient orders;

    OrdersClient(RestClient.Builder builder, RestClientSsl ssl) {
        this.orders = builder.baseUrl("https://orders:8443")
            .apply(ssl.fromBundle("spiffe"))
            .build();
    }

    String latest() {
        return this.orders.get().uri("/orders/latest").retrieve().body(String.class);
    }

}

To make every client built by Spring Boot use the bundle, set spring.http.clients.ssl.bundle=spiffe instead. WebClient works the same way through WebClientSsl.

Step 5: Run it next to an agent

The starter looks for the Workload API socket in this order:

  1. the spiffe.workload-api.socket property (unix:///path or tcp://host:port);

  2. the SPIFFE_ENDPOINT_SOCKET environment variable;

  3. the first of these well-known paths that exists:

Path Agent

/tmp/spire-agent/public/api.sock

SPIRE

/run/spire/sockets/agent.sock

SPIRE

/run/spire/agent-sockets/api.sock

SPIRE

/spiffe-workload-api/spire-agent.sock

SPIRE (SPIFFE CSI driver)

/tmp/swa-agent/public/api.sock

IDIRA SWA (Kubernetes)

/run/swa-agent/api.sock

IDIRA SWA (VM)

On Kubernetes with the SPIFFE CSI driver, mounting the socket is all it takes, because /spiffe-workload-api/spire-agent.sock is one of the known paths:

spec:
  containers:
    - name: orders
      image: example/orders:1.0
      volumeMounts:
        - name: spiffe-workload-api
          mountPath: /spiffe-workload-api
          readOnly: true
  volumes:
    - name: spiffe-workload-api
      csi:
        driver: csi.spiffe.io
        readOnly: true

At startup the app logs the identity it received:

SPIFFE identity spiffe://example.org/ns/shop/sa/orders obtained from the Workload API at unix:///spiffe-workload-api/spire-agent.sock
If no socket is found, the starter configures nothing and the app starts as if it were not there, which keeps local development on plain HTTP simple. If a socket is found but no SVID arrives within spiffe.workload-api.init-timeout (30s by default), startup fails with a message that says what to check.

Step 6: Check it works

With Actuator on the classpath, the spiffe health indicator reports the identity and goes DOWN if rotation stalls:

curl -s http://localhost:8081/actuator/health/spiffe

The sample serves Actuator on a separate plain-HTTP management port (management.server.port=8081, management.server.ssl.enabled=false). On the main port, health requests go through mTLS like any other request.

You can also inject the identity anywhere:

@RestController
class WhoAmIController {

    private final SpiffeIdentitySource identity;

    WhoAmIController(SpiffeIdentitySource identity) {
        this.identity = identity;
    }

    @GetMapping("/whoami")
    String whoami() {
        return this.identity.spiffeId().toString();
    }

}

Next steps

SPIFFE Spring Boot is an independent open-source project under the Apache License 2.0. It is not affiliated with or endorsed by the SPIFFE project, the CNCF, or Broadcom (Spring). SPIFFE, SPIRE, Spring and Spring Boot are trademarks of their respective owners.

GitHub · Issues · Sample