Performance: Securing endpoints by role
This affects the server response time and user experience by controlling access before processing requests.
Jump into concepts and practice - no test required
@PreAuthorize("hasRole('ADMIN')") public String getAdminData() { return adminService.getData(); }
public String getAdminData() {
if (!user.hasRole("ADMIN")) {
throw new AccessDeniedException("Forbidden");
}
return adminService.getData();
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual role checks inside controller methods | 0 | 0 | 0 | [OK] |
| Declarative role checks with @PreAuthorize annotations | 0 | 0 | 0 | [OK] Good |
@PreAuthorize in a Spring Boot application?@PreAuthorize?hasRole('ROLE_NAME') expression inside @PreAuthorize restricts access to users with that role.hasRole('ADMIN'). Other options either allow all or restrict to different roles.@PreAuthorize("hasRole('MANAGER')")
public String getManagerData() {
return "Manager Info";
}getManagerData()?@PreAuthorize("hasRole('ADMIN')")
public String adminPanel() {
return "Welcome Admin";
}@PreAuthorize?@PreAuthorize("hasRole('USER')") on the user endpoint and @PreAuthorize("hasRole('ADMIN')") on the admin endpoint to enforce separate access.