Overview
Authorization in the User Management System is implemented using Spring Security’s role-based access control (RBAC). After a user is authenticated via JWT, their role determines which endpoints they can access.Role-Based Access Control (RBAC)
The system uses two primary roles:- ROLE_USER: Standard user with access to basic user operations
- ROLE_ADMIN: Administrator with elevated privileges
How RBAC Works
Authorization happens after authentication. The
JwtAuthenticationFilter first validates the token and extracts the user’s role, then Spring Security’s authorization mechanism checks if that role has permission to access the requested endpoint.Protected Endpoints
Endpoint protection is configured inSecurityConfig.java:27-31 using Spring Security’s authorizeHttpRequests DSL:
Endpoint Access Requirements
| Endpoint | Method | Required Role | Description |
|---|---|---|---|
/auth/signup | POST | None (Public) | Register a new user |
/auth/login | POST | None (Public) | Authenticate and receive JWT token |
/users/me | GET | ROLE_USER | Get current user information |
/admin/users | GET | ROLE_ADMIN | List all users (admin only) |
The
/users/me endpoint requires ROLE_USER, which means both regular users and admins can access it (since admins typically also have user permissions in many systems). However, based on this configuration, only users with exactly ROLE_USER can access it.How Spring Security Enforces Authorization
Spring Security enforces authorization through a series of filters and security interceptors. Here’s the complete flow:1. JWT Authentication Filter
TheJwtAuthenticationFilter runs first and extracts the role from the JWT token (JwtAuthenticationFilter.java:33):
GrantedAuthority (JwtAuthenticationFilter.java:35-39):
2. Security Context
The authentication object is stored in Spring Security’sSecurityContextHolder (JwtAuthenticationFilter.java:41):
3. Authorization Decision
When the request reaches a protected endpoint, Spring Security’sAuthorizationFilter checks if the user’s authorities match the required authorities for that endpoint.
For example, when accessing /admin/users:
- Spring Security retrieves the authentication from
SecurityContextHolder - It extracts the
GrantedAuthoritylist (containing the role) - It compares against the required authority:
ROLE_ADMIN - If the role matches, access is granted; otherwise, a
403 Forbiddenresponse is returned
Filter Chain Order
TheJwtAuthenticationFilter is added before Spring Security’s default authentication filter (SecurityConfig.java:32):
Authorization vs Authentication
It’s important to understand the difference:Authentication
Who are you?Verifies the user’s identity through JWT token validation. Handled by
JwtAuthenticationFilter.Authorization
What can you do?Determines if the authenticated user has permission to access a resource. Handled by Spring Security’s authorization filters.
Stateless Authorization
The system uses stateless authorization, meaning:- No server-side session storage required
- Better scalability (no session replication needed)
- Each request is self-contained
- The JWT token must be included in every request
- Role changes require a new token to be issued
- Token expiration cannot be revoked server-side without additional mechanisms
Access Denied Scenarios
401 Unauthorized
Returned when:- No JWT token is provided
- JWT token is invalid or expired
- JWT token has an incorrect signature
403 Forbidden
Returned when:- User is authenticated but lacks the required role
- For example, a
ROLE_USERtrying to access/admin/users
Security Best Practices
1. Principle of Least Privilege
Users should be assigned the minimum role necessary for their tasks. By default, new users are assignedROLE_USER (see User.java:33):
2. Role Validation
Roles are stored as an enum (Role.java) to prevent invalid role values:
3. Secure Defaults
The security configuration denies access to all endpoints by default unless explicitly permitted:Example: Admin Access Flow
Here’s how an admin accesses the user list:Request with Token
Admin sends a GET request to
/admin/users with the token in the Authorization header.Authorization Check
Spring Security checks that the endpoint requires
ROLE_ADMIN and the user has it.Controller Implementation
The controllers don’t need to manually check roles—Spring Security handles it automatically:User Endpoint
Admin Endpoint
Notice that the controller methods don’t contain any authorization logic. The security configuration in
SecurityConfig handles all authorization concerns, keeping the controller code clean and focused on business logic.Testing Authorization
Testing User Access
Testing Admin Access
Next Steps
Roles
Learn about specific roles and their permissions
Authentication
Understand how JWT authentication works