@PostFilter protects returned data after a method completes. Spring Security allows the method call, then filters its returned collection or array using a SpEL authorization expression. section 12.2
Apply @PostFilter with these constraints:
- Return a collection or array; other return types are not supported by postfiltering. section 12.2 section 12.4
- Write the authorization rule as the annotation value, using filterObject for each returned element and authentication for the current security context. section 12.2
- Prefer database-level filtering for Spring Data repository queries; postfiltering can retrieve every record before filtering and cause memory or performance problems. section 12.3 section 12.4
Filter products by owner
Annotate a service method with @PostFilter("filterObject.owner == authentication.name"). A returned list containing products owned by nikolai and julien is reduced to the current user's products. Calling /find as julien returns chocolate; calling it as nikolai returns beer and candy.
