liveBook · Manning

Prefiltering

As explained in Spring Security in Action, Second Edition

Prefiltering is filtering applied to a method's input parameters before the method receives them.

Prefiltering filters collection or array elements before a method receives them, allowing the method call to proceed with only authorized values rather than rejecting the call. section 12.0 section 12.4

Apply prefiltering by:

  • Enabling method security with @EnableMethodSecurity. section 11.1
  • Annotating the method with @PreFilter and supplying a SpEL condition. section 12.1

Filtering products by owner

A service can declare @PreFilter("filterObject.owner == authentication.name") on sellProducts(List<Product> products). A request authenticated as nikolai receives only beer and candy; julien receives only chocolate. Replacing the mutable list with List.of causes an UnsupportedOperationException.

How Spring Security in Action, Second Edition covers it

How deep it goes
Explains aspect interception, @PreFilter SpEL expressions, collection constraints, and mutable-input behavior through runnable examples.
Examples
Demonstrates owner-filtered products through a /sell endpoint, curl requests, response lists, and an immutable-list failure.
What it assumes
Assumes basic Spring and Java, while introducing method-security enablement, aspects, SpEL, filterObject, and authentication references.
What you can do after
Apply it via Spring Security to restrict collection or array inputs before authorized service methods process them.
How it connects
Connects prefiltering with method security, call authorization, postfiltering, SpEL, authentication, and Spring Data.

Also explained in

Compare how 2 books explain prefiltering →

Related concepts

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