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.
- User authentication and authorization
- Role-based access control
- Secure password storage
- JWT (JSON Web Token) support
- Custom login and error pages
- RESTful API security
- Java 17 or higher
- Maven 3.6+
- 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
- Access the application at
http://localhost:8080
after 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).
- 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.
-
User Login Request
- User sends credentials (username/password) to the authentication endpoint (e.g.,
/login
or/api/authenticate
).
- User sends credentials (username/password) to the authentication endpoint (e.g.,
-
Authentication Manager & UserDetailsService
- The authentication endpoint uses the
AuthenticationManager
to authenticate the user. AuthenticationManager
delegates toUserDetailsService
to 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
Authorization
header (Bearer <token>
) for subsequent requests.
- The client includes the JWT token in the
-
JWT Authentication Filter (
jwtAuthFilter
)- For each request,
jwtAuthFilter
intercepts 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.
Contributions are welcome! Please open issues or submit pull requests for improvements and bug fixes.