liveBook · Manning

Premium content

As explained in Spring AI in Action

Premium content is document content whose access is restricted to users with premium authorization.

Premium content is document content restricted to users with premium authorization. In a retrieval-augmented application, mark premium documents during ingestion, then exclude them from vector searches for unauthorized users. section 10.1.1 section 10.1.3

Implement the restriction in three stages:

  • Adopt a filename convention: documents whose names end in -premium are treated as premium. section 10.1.1
  • Read the filename from the incoming Message<byte[]> and add documentType=PREMIUM to every resulting document chunk. section 10.1.1
  • Use Spring Security to inspect the authenticated user's ROLE_PREMIUM_USER authority and add documentType != 'PREMIUM' to unauthorized searches. section 10.1.2 section 10.1.3
Listing 10.1 Altering the document reader bean to handle premium content
@Bean
Function<Flux<Message<byte[]>>, Flux<Document>> documentReader() {
  return resourceFlux -> resourceFlux
      .map(message -> {
        var fileName = (String) message.getHeaders().get("file_name");    #1
        LOGGER.info("Reading document from file: {}", fileName);

        var fileBytes = message.getPayload();
        var document = new TikaDocumentReader(
            new ByteArrayResource(fileBytes))
            .get()
            .getFirst();
        if (isPremiumDocument(fileName)) {
          document.getMetadata().put("documentType", "PREMIUM");  #2
        }
        return document;
      })
      .subscribeOn(Schedulers.boundedElastic());
}

private boolean isPremiumDocument(String fileName) {     #3
  var baseFilename = fileName
      .substring(0, fileName.toString().lastIndexOf('.'));
  return baseFilename.endsWith("-premium");
}

The document-reader listing shows how filename metadata becomes a premium-content marker during ingestion. section 10.1.1

Testing premium retrieval

Rename a game-rules PDF with the -premium suffix and load it into the vector store. A premium user can ask about it; an ordinary user receives no answer from that content. section 10.1.5

How Spring AI in Action covers it

How deep it goes
Explains ingestion metadata, Spring Security authority checks, vector filters, and HTTP testing, but defers broader tool security.
Examples
Uses premium game-rule PDFs, the -premium filename suffix, and Mickey and Donald querying Tortuga 1667.
What it assumes
Assumes a Spring application with document loading and vector search, while introducing security configuration and user authorities.
What you can do after
Operate/configure Spring Security and vector-store filters to prevent unauthorized retrieval of premium documents.
How it connects
Connects document metadata, vector search, retrieval-augmented generation, authentication, authorization, and Spring Security.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring AI in Action. The section links point to the parts of the book it draws on.