← Java / Spring · Contents

JAVA-071 / Intermediate

Returning JPA entities directly as JSON: sensitive fields and lazy loading

A01:2021 Broken Access Control · CWE-200 Exposure of Sensitive Information

Why is this a problem?

Password hashes and entire related entities are exposed, and circular references can cause DoS.

How to fix it

java
@GetMapping("/me") UserDto me() { return UserDto.from(userRepo.findById(id).orElseThrow()); }

Further reading