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
