liveBook · Manning

Filtering

As explained in Spring Security in Action, Second Edition

Filtering is a method-security technique that restricts method input parameters or returned results according to authorization rules.

Filtering controls which values cross a method boundary without rejecting the method call. It removes unauthorized elements from input collections before execution or from returned collections afterward. section 12.0 section 12.4

Apply filtering through two method-security annotations:

  • @PreFilter removes elements from a collection or array parameter before the method runs. Its SpEL condition uses filterObject for each element. section 12.1 section 12.4
  • @PostFilter removes elements from a collection or array returned after the method executes. section 12.0 section 12.4
  • Enable method security explicitly with @EnableMethodSecurity before using these annotations. section 11.1

Filter products by owner

A product-selling service can accept products owned by the authenticated user while ignoring the rest: @PreFilter("filterObject.owner == authentication.name"). Given products owned by nikolai and julien, nikolai receives only beer and candy, while julien receives only chocolate. The intercepted collection must be mutable because the aspect changes it.

Figure 12.1 The client calls the endpoint providing a value that doesn’t follow the authorization rule. With preauthorization, the method isn’t called at all, and the caller receives an exception. With prefiltering, the aspect calls the method but only provides the values that follow the given rules.

Compare preauthorization, which rejects the call, with prefiltering, which calls the method using only values that satisfy the rule. section 12.0

Use filtering to keep authorization rules separate from business logic. For Spring Data repositories, prefer authorization conditions in database queries over postfiltering large result sets in application memory. section 12.1 section 12.3

How Spring Security in Action, Second Edition covers it

How deep it goes
Explains annotations, SpEL expressions, interception, mutability, examples, and query-level alternatives, but not broader authorization internals.
Examples
Demonstrates owner-filtered products, curl responses, mutable collections, and repository queries constrained by the authenticated username.
What it assumes
Assumes basic Spring and Java while introducing method-security enablement, filtering annotations, filterObject, authentication, and SpEL rules.
What you can do after
Apply it via @PreFilter and @PostFilter, or diagnose repository performance problems by moving filtering into database queries.
How it connects
Connects method security, authorization, SpEL, Spring Data repositories, database queries, and authentication.

Also explained in

Compare how 33 books explain filtering →

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.