A production-ready JWT authentication system with role-based access control, built with Spring Boot and Angular.
- β JWT Authentication & Authorization
- β Role-Based Access Control (Admin/User)
- β Secure Password Storage with BCrypt
- β PostgreSQL Integration
- β CORS Configuration
- β Angular Frontend Demo
- β API Rate Limiting
- β Refresh Token Support
Backend
- Java 17
- Spring Boot 3.2
- Spring Security
- JJWT 0.12.5
- PostgreSQL
- Maven
Frontend
- Angular 19
- RxJS 7.8
- Angular Material
- JWT Interceptors
git clone https://github.com/yourusername/spring-security-jwt.git
cd spring-security-jwtCREATE DATABASE security_db;
CREATE USER api_user WITH PASSWORD 'your_strong_password';
GRANT ALL PRIVILEGES ON DATABASE security_db TO api_user;# src/main/resources/application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/security_db
spring.datasource.username=api_user
spring.datasource.password=your_strong_password
jwt.secret=your-512-bit-secret-key # Generate using: openssl rand -base64 512
jwt.expiration=86400000 # 24 hoursmvn spring-boot:runcd frontend
npm install
ng serve --open| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /authenticate |
Get JWT Token | Public |
| POST | /registerNewUser |
Register new user | Public |
| GET | /forAdmin |
Admin-only endpoint | ADMIN |
| GET | /forUser |
User-specific endpoint | USER |
POST /authenticate HTTP/1.1
Content-Type: application/json
{
"userName": "admin",
"userPassword": "admin"
}{
"jwtToken": "eyJhbGciOiJIUzUxMiJ9...",
"user": {
"userName": "admin",
"roles": ["ROLE_ADMIN"]
}
}GET /forAdmin HTTP/1.1
Authorization: Bearer eyJhbGciOiJIUzUxMiJ9...sequenceDiagram
Client->>Server: POST /authenticate
Server->>Client: JWT Token
Client->>Server: Requests with JWT
Server->>Server: Validate Token & Roles
Server->>Client: Secure Data
ADMIN > USERAngular Interceptor
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler) {
const token = localStorage.getItem('jwtToken');
if (token) {
req = req.clone({
setHeaders: {
Authorization: `Bearer ${token}`
}
});
}
return next.handle(req);
}
}Auth Guard
@Injectable({ providedIn: 'root' })
export class AuthGuard implements CanActivate {
constructor(private authService: UserAuthService) {}
canActivate(route: ActivatedRouteSnapshot): boolean {
const requiredRoles = route.data['roles'];
return this.authService.hasRoles(requiredRoles);
}
}- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Distributed under the MIT License. See LICENSE for more information.
Made with β€οΈ by [Aashif Sajah] - @cliff.adventurer_