Quick revision
The short answer
Spring Data REST exports every CRUD method of every public repository by default, including delete and the full collection listing, and PATCH and PUT bind JSON straight onto the entity, so any field, including role, is writable, which is mass assignment. The fix is to switch to annotated detection, hide delete and findAll with @RestResource(exported = false), secure repository methods with @PreAuthorize, make sensitive fields read-only or use DTOs, check ownership, and test every verb; for public or partner APIs, replace generated endpoints with explicit controllers and DTOs.
What to remember
In this answer, we saw that the three findings came from Spring Data REST's defaults: every CRUD method of every public repository is exported, and PATCH and PUT merge JSON straight onto entities, which allows mass assignment. We fixed it in layers with annotated detection and hidden methods, @PreAuthorize on repository methods, ownership checks that rely on read-only ownership fields, @JsonProperty(access = READ_ONLY) or DTOs for sensitive fields, and tests for every verb, and we would use explicit controllers and DTOs for public or partner APIs.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Spring Data REST exports every CRUD method of every public repository by default, including delete and the full collection listing, and PATCH and PUT bind JSON straight onto the entity, so any field, including role, is writable, which is mass assignment. The fix is to switch to annotated detection, hide delete and findAll with @RestResource(exported = false), secure repository methods with @PreAuthorize, make sensitive fields read-only or use DTOs, check ownership, and test every verb; for public or partner APIs, replace generated endpoints with explicit controllers and DTOs.
Why this question is asked
This security finding is common in services that started with Spring Data REST as a quick way to get an API. Nothing in the code looks dangerous: a few repository interfaces and some entities. The danger is in what the framework generates by default. Interviewers ask this scenario to see whether we can explain each finding from how the framework works and then fix it in layers, not with one patch. In this answer, we will explain each of the three findings, then fix exports, method security, writable fields and ownership, and finally look at testing every endpoint and when to replace generated endpoints altogether.
1What Went Wrong: Everything Is Exported
Spring Data REST's default detection strategy exports every public repository, and for each one it exposes every CRUD method the repository has. A UserRepository extending JpaRepository therefore gets GET /users, which lists all users page by page, and DELETE /users/{id}. A CustomerRepository gets DELETE /customers/{id}. Nobody wrote these endpoints, which is exactly why nobody reviewed them.
The security configuration only required authentication, so any logged-in user could call them. Two of the three findings, deleting customers and listing all users, come straight from this default.
| Finding | Cause | Fix |
|---|---|---|
| any user can DELETE customers | delete methods exported by default | hide them or @PreAuthorize ADMIN |
| user can PATCH own role | JSON merged onto the entity | read-only field or DTO |
| any user can list all users | findAll exported as GET /users | hide or restrict findAll |
2What Went Wrong: PATCH Writes Any Field
For PUT and PATCH, Spring Data REST loads the entity, merges the request JSON onto it with Jackson, and calls save(). Every property Jackson can deserialize is writable. This is called mass assignment: the client can set fields the developer never intended to be set from outside.
Notice that the user changed only their own record, so an ownership check alone would not have stopped it. The problem is that the role field should never be writable through this API by anyone except administrators.
3Fix 1: Export Only What We Mean To
First, change the default. With spring.data.rest.detection-strategy=annotated, only repositories annotated with @RepositoryRestResource are exported. Then, in each exported repository, hide the methods we do not want as endpoints with @RestResource(exported = false). Delete may be backed by delete(T) or deleteById(ID) depending on the version, so hide or secure both, and the collection resource uses the findAll variants the repository declares.
Recent versions also allow disabling HTTP methods per type through the exposure configuration in a RepositoryRestConfigurer, which keeps these rules in one place; check your version.
4Fix 2: Method Security on the Repository
Because generated endpoints call repository methods, method security on those methods protects them, whatever URL reaches them. With @EnableMethodSecurity, we annotate the repository interface.
There is a catch with the ownership check on save. It sees the entity after the request JSON has been merged onto it. If the username field were writable, an attacker could PATCH someone else's record, set the username to their own, and pass the check. So ownership checks only work if the ownership field itself is read-only, which leads to the next fix. Also note that methods we do not override keep no security annotations, so every exported method must be covered.
5Fix 3: Read-Only Fields, DTOs and Tests
Fields that clients must never write, such as role, username, owner or balance, should be read-only for JSON input. Jackson's @JsonProperty(access = READ_ONLY) includes the field in responses but ignores it on input. Current versions of Spring Data REST respect this when merging, but verify it with a test. For anything more complex, such as admins being allowed to change roles, a dedicated endpoint with its own DTO and authorisation is clearer.
Finally, for public or partner APIs, I would replace the generated endpoints with explicit controllers and DTOs. Then every endpoint, every writable field and every authorisation rule is written and reviewed on purpose, and the database schema can change without changing the API.
6Follow-up questions
How would you test every exported endpoint automatically?
Generate the test cases from what Spring Data REST actually exports, so new repositories are covered without anyone remembering. In a test, read the exported resources from the root resource and the ALPS profile, or from Spring Data REST's resource mappings bean, and build a matrix of every resource, every HTTP verb and every role. Run each combination through MockMvc with @WithMockUser and compare the status with an expected-permissions table kept in the test. An endpoint that appears without an entry in the table fails the build, which forces a decision whenever something new is exported.
Why not secure everything with URL rules in the security filter chain?
URL rules help and should be there, but they are easy to get out of step with generated paths, and the same repository method can be reached through more than one URL, such as search resources and association links. Method security on the repository protects the operation itself, whatever URL calls it.
Is Spring Data REST unsafe in general?
No, but it is permissive by default. With annotated detection, hidden methods, method security, read-only sensitive fields and tests, it is fine for internal admin APIs. The risk is using it with the defaults for APIs exposed to untrusted users.
7Conclusion
In this answer, we saw that the three findings came from Spring Data REST's defaults: every CRUD method of every public repository is exported, and PATCH and PUT merge JSON straight onto entities, which allows mass assignment. We fixed it in layers with annotated detection and hidden methods, @PreAuthorize on repository methods, ownership checks that rely on read-only ownership fields, @JsonProperty(access = READ_ONLY) or DTOs for sensitive fields, and tests for every verb, and we would use explicit controllers and DTOs for public or partner APIs.
Open with
All three findings come from Spring Data REST's defaults, not from code anyone wrote. By default it exports every public repository with every CRUD method, so DELETE on customers and GET on the whole users collection exist, and the security config only required authentication.
Then, if they want more
And for PUT and PATCH it loads the entity, merges the JSON onto it with Jackson and calls save, so every deserializable field is writable, which is mass assignment, and a user can set their own role. I would fix it in layers.
First, spring.data.rest.detection-strategy set to annotated, so only repositories with @RepositoryRestResource are exported, and @RestResource(exported = false) on delete and deleteById and on findAll where they should not be endpoints. Second, @PreAuthorize on the repository methods themselves, admin only for delete and findAll, and an ownership check on save, because generated endpoints call those methods whatever the URL.
That ownership check only works if the ownership field is not writable, so third, sensitive fields like role and username get @JsonProperty with READ_ONLY access, or I use DTOs. Fourth, a test matrix of every endpoint, verb and role. And for public or partner APIs I would replace the generated endpoints with explicit controllers and DTOs.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A security review of a service built with Spring Data REST finds that any authenticated...": https://scrivio-interview-library.netlify.app/spring-boot/spring-projects/6ed4e1dbb1/