Content ITV PRO
This is Itvedant Content department
Learning Outcome
5
Authorization secures endpoints and business logic
4
Spring supports roles and authorities together
3
Roles contain permissions for allowed actions
2
RBAC controls access using assigned roles
1
JWT handles user authentication and identity
In the previous lecture, we understood how stateless authentication using JWT works....
JWT helps the application identify:
But after identifying the user, another important question arises:
Should every user access everything
Should a normal user delete data
Should admin features be public
It is commonly handled using: RBAC (Role based access control)
Users are assigned roles
Roles define allowed actions
Manage access through roles
Granular control
Better scalability
Precise access management
Limited flexibility
Hard to customize access
All-or-nothing approach
Real systems often use Roles + Permissions together
Convention
Roles must start with ROLE_
Example
Internally
hasRole("ADMIN") checks for
ROLE_ADMIN
http.authorizeHttpRequests(auth ->
auth.requestMatchers("/admin/**")
.hasRole("ADMIN") // Checks ROLE_ADMIN
);
GrantedAuthority authority =
new SimpleGrantedAuthority("ROLE_ADMIN");Automatically prefixed with ROLE_
High-level grouping
hasRole()
Example
ROLE_ADMIN
No prefix required
Fine-grained control
hasAuthority()
Example
DELETE_USER
Security configuration applied to URLs and request paths. It checks access before the request reaches the controller.
URL Patterns
Security applied directly to service methods using annotations. Offers fine-grained control over business logic.
@PreAuthorize
Both layers control access, but at different points of execution
Defined in SecurityFilterChain bean
Applied to URLs/Endpoints
Works before controller logic
Works before controller logic
Checks permissions during request flow
Works before controller logic
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) {
return http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/admin/**")
.hasRole("ADMIN")
.requestMatchers("/user/**")
.hasAnyRole("USER", "ADMIN")
)
.build();
}Secures specific business logic methods
More precise than URL patterns
@Service
public class ProductService {
// Using Role
@PreAuthorize("hasRole('ADMIN')")
public void deleteProduct() { ... }
// Using Authority
@PreAuthorize("hasAuthority('DELETE_USER')")
public void removeUser() { ... }
}Works before controller logic
Uses @PreAuthorize or @PostAuthorize
Protects core application functionality
Works before controller logic
User is NOT authenticated
No valid token or credentials
"Who are you?"
User is authenticated
But does NOT have permission
"You are not allowed"
User sends request with JWT
Server authenticates
user
Roles/Authorities extracted from token
Access rules are
checked
Allowed -Request proceeds
Denied - 403 Forbidden returned
USER_Role
View products
Browse catalog
ADMIN_Role
View products
Delete products
user
user
user
DELETE /api/products/1
DELETE /api/products/1
403 Forbidden
200 Allowed
admin
Summary
5
Identify 401 and 403 authorization errors
4
Apply endpoint and method-level security
3
Differentiate roles and authorities clearly
2
Explain RBAC working in Spring Security
1
Understand authentication and authorization differences
Quiz
A. JWT directly controls user permissions
B. RBAC is used for authentication only
C. Roles define what actions users can perform
D. Authorities always require ROLE_ prefix
Which statement correctly describes RBAC in Spring Security?
A. JWT directly controls user permissions
B. RBAC is used for authentication only
C. Roles define what actions users can perform
D. Authorities always require ROLE_ prefix
Which statement correctly describes RBAC in Spring Security?
Quiz-Answer
By Content ITV