spring-projects / spring-projects/spring-security

Support @PostFilter when returning single object

Open
#4,028 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

It may appear a bit contrived, but consider this example:

An object is soft-deleted in a server application, setting pojo.deleted = true; In general, this object shall not be retrievable any more, except for users with role="admin".
Imagine a manager method pojoManger.getById(long id). To not return deleted objects in the general case this method would have to be aware about the identity of the current user, and issue a corresponding query against the DAO layer, to (not) deliver an object that was soft-deleted.

My preferred solution would be though, to put @PostFilter(returnObject.deleted == false or hasRole('admin')) on that method, returning null if the expression evaluates to false. Throwing an AccessDeniedException would be inappropriate when a non-admin tries to get a deleted object.

The caller could then throw a NotFoundException() in that case, assuming a REST API application.
At least to me that appears to be a very sensible extension of the filter functionality.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how @PostFilter processes methods that return a single object, using the described getById(long id) case as the behavioral example. Done means a failed filter can return null for a non-admin while retaining the requested role-based filtering behavior; the payload names no source files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
security
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.