RBAC and Spring Security Tutorial: From Basic Role-Based Access Control to JWT Integration and JSON Login
This article provides a comprehensive guide on implementing role‑based access control (RBAC) with Spring Security, covering RBAC models, password encryption, in‑memory authentication, JWT integration, custom authentication filters for JSON login, and detailed configuration examples with full source code snippets.
This tutorial walks through the fundamentals of RBAC (including RBAC0‑RBAC3 models) and demonstrates how to implement role‑based access control using Spring Security in a Java Spring Boot application.
It starts with basic in‑memory authentication, showing Maven dependencies, configuration of WebSecurityConfigurerAdapter, and simple endpoint protection.
The guide then introduces password encryption using BCryptPasswordEncoder, and shows how to store encrypted passwords in the database.
Next, it explains how to integrate JWT for stateless authentication: adding JWT and Spring Security dependencies, creating a JwtUser class implementing UserDetails, a JwtTokenUtil utility for token generation and validation, a JwtAuthenticationTokenFilter to process JWTs, and a custom UserDetailsService implementation.
It also covers a complete login flow that returns a JWT token, and provides a security configuration that secures all endpoints while permitting the authentication URLs.
Finally, the article shows how to replace the default UsernamePasswordAuthenticationFilter with a custom filter to support JSON‑based login requests, including the custom filter implementation and its registration in the security config.
package com.example.demo;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
public class JwtUser implements UserDetails {
private String username;
private String password;
private Integer state;
private Collection<? extends GrantedAuthority> authorities;
// constructors, getters, and overridden methods omitted for brevity
} package com.example.demo;
import io.jsonwebtoken.*;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.*;
public class JwtTokenUtil implements Serializable {
private String secret;
private Long expiration;
private String header;
// token generation, parsing, validation methods omitted for brevity
} package com.example.demo;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.*;
import javax.servlet.http.*;
@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
// filter logic to extract JWT from header and set authentication
} package com.example.demo;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import com.fasterxml.jackson.databind.ObjectMapper;
public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
if (request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE)) {
// parse JSON login request
}
return super.attemptAuthentication(request, response);
}
}By following these steps, developers can secure their applications with robust RBAC policies, encrypted credentials, and modern JWT‑based stateless authentication.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
