跪拜 Guibai
← All articles
Backend

RBAC Done Right: Spring Security, JWT, JSON Login, and BCrypt

By 皮皮林551 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Most Java enterprise backends still run Spring Security with JWT and BCrypt; this walkthrough shows the exact integration steps, which is what developers will encounter when maintaining real codebases.

Summary

This guide walks through the full permission system design using RBAC models, from the basic RBAC0 to the more constrained RBAC2 and RBAC3, then shows how to implement it in Spring Security. It covers the default Spring Security setup, in-memory users, HTTP request interception, and then integrates JWT for stateless authentication. The JWT section includes a custom filter, a token utility class, and a UserDetailsService implementation. Later, it explains how to enable JSON-based login by overriding the UsernamePasswordAuthenticationFilter, and how to use BCrypt for password hashing. Finally, it demonstrates database-backed authentication by wiring a custom UserDetailsService into the SecurityConfig.

Takeaways
— RBAC0 is the most common model and supports user-role many-to-many relationships.
— RBAC2 adds constraints like role mutual exclusion, cardinality, prerequisites, and runtime exclusion.
— Spring Security's default login uses a generated password printed in the logs.
— JWT integration requires a custom filter that reads the token, validates it, and sets the security context.
— JSON login is achieved by overriding UsernamePasswordAuthenticationFilter and replacing the default filter in the security config.
— BCryptPasswordEncoder handles password hashing; using NoOpPasswordEncoder is insecure and only for demos.
— Database authentication wires a UserDetailsService into AuthenticationManagerBuilder.
Conclusions

The guide uses the deprecated WebSecurityConfigurerAdapter, which is no longer the recommended approach in Spring Security 5.7+; developers need to adapt these patterns to the new component-based configuration.

The tutorial correctly moves from an insecure NoOpPasswordEncoder to BCrypt, but the default Spring Security setup still accepts plain text in memory unless overridden, so it's easy to leave insecure config in place.

The JSON login override is a common need for SPAs, but the guide does not cover CSRF or CORS beyond a basic setup, which is a typical omission in tutorials.

Concepts & terms
RBAC
Role-Based Access Control: a permission model where users are assigned roles, and roles are assigned permissions, so users inherit permissions indirectly through roles.
JWT
JSON Web Token: a compact, URL-safe token used for stateless authentication, containing claims that can be verified and signed.
BCrypt
A password hashing function designed to be slow and salted, making brute-force attacks expensive.
UserDetailsService
A Spring Security interface that loads user data from a persistent store, used during authentication.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗