Home/Guides/Java middleware: Spring Boot and servlet filter

Java middleware: Spring Boot and servlet filter

The Java middleware is the counterpart of the .NET one, with the same settings: it pseudonymizes what your REST services receive, return and send, through your Anonymizer server (Enterprise Edition) or the online service. A starter for Spring Boot 3, a Java 17 library for everything else.

8 min read8 steps
0:00 / 0:00

What you need

sealforge-anonymizer-spring-boot-starterSpring Boot 3 (Spring MVC): sealforge.anonymizer.* settings, annotations on controllers, RestClient / RestTemplate interceptor
sealforge-anonymizerJava 17 without Spring: a jakarta.servlet filter (Tomcat, Jetty, Quarkus, Jakarta EE…), outgoing calls, the client of the server

Both modules belong to the fr.sealforge group and need Java 17 or later. They come with the Enterprise Edition and with the online service, from the Maven repository supplied with your licence (a URL or a folder).

Warning. Spring Boot 1 and 2 are not supported.

Set it up from the desktop app

In the app, open Settings → .NET and Java middleware, choose the folder of your project, then Scan: the app recognizes a Spring Boot 3 project (Maven pom.xml or Gradle build.gradle(.kts) with spring-boot-starter-web) and reads your controllers' actions from src/main/java, without building anything.

Preview the changes shows every file and the lines added; Apply writes them, after a copy of each file.

pom.xml / build.gradlethe starter's dependency and the Maven repository given
application.yml / .propertiesthe sealforge.anonymizer.* settings
actions@AnonymizeRequestBody / @AnonymizeResponseBody and their import
~/.sealforge/secrets/the API key or the licence key, outside the repository, readable by you alone

Or by hand: the dependency and two settings

<dependency>
  <groupId>fr.sealforge</groupId>
  <artifactId>sealforge-anonymizer-spring-boot-starter</artifactId>
  <version>1.0.0</version>
</dependency>
# application.yml
sealforge:
  anonymizer:
    server-url: https://anonymizer.corp.example
    api-key: ${SEALFORGE_API_KEY}

The starter configures itself: no configuration class to write. sealforge.anonymizer.enabled: false turns it off.

Tip. Keep the key out of the repository: an environment variable or a secret store. The settings are checked at startup.

Annotate the actions to protect

@RestController
class Tickets {
    @PostMapping("/tickets")
    @AnonymizeRequestBody          // the action, its logs and its database only see the pseudonymized ticket
    Ticket create(@RequestBody Ticket ticket) { return store(ticket); }

    @GetMapping("/export")
    @AnonymizeResponseBody         // the caller receives the data pseudonymized
    List<Customer> export() { return customers(); }
}

@AnonymizeBodies pseudonymizes both. The annotations go on an action or on a controller (all its actions). For functional routes, declare an EndpointBodies bean: new UrlPatternEndpoints().request("POST", "/tickets").response("GET", "/export/**").

Which body? The request, when the service itself must never hold the values. The response, when the service may hold them but not its caller. Both: a chat.

Warning. Only the string values of a JSON body are pseudonymized. A response written asynchronously (SseEmitter, StreamingResponseBody, DeferredResult) cannot be held: in block mode the caller gets a 503.

Outgoing calls: an LLM

@Bean
RestClient llm(RestClient.Builder builder, SealforgeOutgoing sealforge) {
    return builder.baseUrl("https://api.openai.com")
        .requestInterceptor(sealforge.interceptor("openai"))
        .build();
}

The LLM only sees tokens; the answer comes back with the real values. The same interceptor goes on a RestTemplate; interceptor(provider, false) leaves the tokens in the answers.

Any other HTTP client uses SealforgeOutgoing.anonymization(…): prepare(body, contentType) gives the body to send, restore(answer, answerContentType) the answer with its values.

Strategies and categories

As in .NET: either explicit, or a named strategy saved on the server, never both.

sealforge:
  anonymizer:
    strategy: placeholder
    categories:
      FirstName: ""
      LastName: ""
      Email: hash
sealforge:
  anonymizer:
    central-strategy: rh-standard

Categories: FirstName, LastName, Company, Address, Login, Secret, PhoneNumber, BirthDate, SocialSecurityNumber, BankAccount, Email, CustomerId.

Warning. An unknown category name stops the application at startup.

The online service

sealforge:
  anonymizer:
    target: online
    server-url: https://anonymizer.sealforge.fr
    license-key: ${SEALFORGE_LICENSE_KEY}
    pseudonym-key: ${SEALFORGE_PSEUDONYM_KEY}   # 16+
    central-strategy: reversible

Your licence key goes with every call. pseudonym-key is your own key (16 characters or more), required by the reversible strategy: it alone opens the values again.

Tip. What a file cannot hold (the key of each tenant, a secret store, OIDC tokens) goes in a SealforgeAnonymizerCustomizer bean.
Warning. With the online service, content goes through our servers for the time of the request, without being kept.

Without Spring, and when something fails

var options = new SealforgeAnonymizerOptions();
options.setServerUrl("https://anonymizer.corp.example");
options.setApiKey(System.getenv("SEALFORGE_API_KEY"));
var anonymizer = SealforgeAnonymizer.create(options);

var endpoints = new UrlPatternEndpoints().request("POST", "/tickets").response("GET", "/customers/{id}");
servletContext.addFilter("sealforge", new SealforgeAnonymizationFilter(anonymizer, options, endpoints))
    .addMappingForUrlPatterns(null, false, "/*");

anonymizer.anonymizeJson(json) and anonymizeText(text) pseudonymize a body anywhere else (a message queue, a batch).

failure-mode: block (the default): server unreachable or licence refused, the request gets a 503 and nothing leaves in clear. pass-through sends the body as it is and logs it.