LI
lilflvme/spring-boot-security
Implementing JWT security based using Spring boot
Spring Boot Security
A sample project demonstrating how to implement security in a Spring Boot application. This project showcases best practices for authentication, authorization, and secure configuration using Spring Security.
Features
- User authentication and authorization
- Role-based access control
- Secure password storage
- JWT (JSON Web Token) support
- Custom login and error pages
- RESTful API security
Getting Started
Prerequisites
- Java 17 or higher
- Maven 3.6+
Setup
- Clone the repository:
git clone https://github.com/shounoop/spring-boot-security.git cd spring-boot-security - Build the project:
./mvnw clean install
- Run the application:
./mvnw spring-boot:run
Usage
- Access the application at
http://localhost:8080after starting. - Default credentials (if any) can be found in the application properties or configured in the database.
- Use the API endpoints as documented in the code or Swagger (if available).
Security Schema
Flow: Spring Boot Security
- User Request: A user tries to access a protected resource or endpoint.
- Authentication:
- If not authenticated, Spring Security intercepts the request and redirects to the login page or returns a 401/403 for APIs.
- User submits credentials (username/password or token).
- Credential Validation:
- Spring Security validates credentials against the configured user store (in-memory, database, LDAP, etc.).
- If valid, a session or JWT token is created.
- Authorization:
- Spring Security checks user roles/authorities for the requested resource.
- Access is granted or denied based on permissions.
- Access Granted/Denied:
- If authorized, the user accesses the resource.
- If not, an error or access denied page is shown.
- Logout:
- User can log out, which invalidates the session or token.
Flow: Spring Boot Security with JWT
-
User Login Request
- User sends credentials (username/password) to the authentication endpoint (e.g.,
/loginor/api/authenticate).
- User sends credentials (username/password) to the authentication endpoint (e.g.,
-
Authentication Manager & UserDetailsService
- The authentication endpoint uses the
AuthenticationManagerto authenticate the user. AuthenticationManagerdelegates toUserDetailsServiceto load user details from the database or another source.
- The authentication endpoint uses the
-
JWT Token Generation
- If authentication is successful, a JWT token is generated and returned to the user.
-
Subsequent Requests with JWT
- The client includes the JWT token in the
Authorizationheader (Bearer <token>) for subsequent requests.
- The client includes the JWT token in the
-
JWT Authentication Filter (
jwtAuthFilter)- For each request,
jwtAuthFilterintercepts and extracts the JWT token from the header. - The filter validates the token (signature, expiration, etc.).
- If valid, it sets the authentication in the security context.
- For each request,
-
Authorization
- Spring Security checks user roles/authorities for the requested resource.
- Access is granted or denied based on permissions.
-
Access Granted/Denied
- If authorized, the user accesses the resource.
- If not, an error or access denied response is returned.
Contributing
Contributions are welcome! Please open issues or submit pull requests for improvements and bug fixes.
On this page
Contributors
Created February 21, 2024
Updated June 3, 2025