advancedAuthorization: Roles, Authorities (RBAC) & Custom Filters
Why would hasRole('ADMIN') fail even though the user clearly has an 'ADMIN' authority in the database?
hasRole() automatically looks for an authority literally named 'ROLE_ADMIN' — if the database/authority-loading code stores just 'ADMIN' without the ROLE_ prefix, the check will never match. Either store the role correctly prefixed as ROLE_ADMIN, or use hasAuthority('ADMIN') instead, which does no automatic prefixing.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response