← Java / Spring · Contents

JAVA-011 / Beginner

Missing Spring Security method authorization annotations

A01:2021 Broken Access Control · CWE-862 Missing Authorization

Why is this a problem?

Even if controllers are protected, direct service calls lack authorization.

How to fix it

java
@PreAuthorize("hasRole('ADMIN') or #id == authentication.principal.id")
public void deleteUser(Long id) { repo.deleteById(id); }

Further reading