Spring Authentication API is a robust and secure authentication system built with Java and Spring Boot. It leverages JWT (JSON Web Tokens) for stateless authentication, allowing users to sign up, sign in, and logout efficiently. Additionally, it includes role-based access control, enabling administrators to manage and view user information seamlessly.
- User Registration: New users can create an account with a unique username and email.
- User Login: Authenticate users and provide JWT tokens for secure access.
- Role-Based Access Control: Assign roles like USER, MODERATOR, and ADMIN to manage permissions.
- Admin Dashboard: Admins can view all registered users and their roles.
- Secure Logout: Invalidate JWT tokens to ensure secure logout.
- JWT Token Management: Generate, validate, and blacklist JWT tokens for enhanced security.
- API Testing with Postman: Easily test API endpoints using Postman with provided configurations.
- Java 17
- Spring Boot
- Spring Security
- JWT (JSON Web Tokens)
- Hibernate & JPA
- MySQL
- Postman
- Maven
- Java Development Kit (JDK) 17 or higher installed.
- Maven installed for dependency management.
- MySQL installed and running.
- Postman for API testing.
- Git installed for version control.
- git clone https://github.com/kshitijrat/JWT-Authentication-Spring-Boot-MySQL-Java.git
- cd spring_authentication
Create a MySQL database named test (or any name you prefer). Update the application.properties file with your MySQL credentials:
- spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false
- spring.datasource.username=your_mysql_username
- spring.datasource.password=your_mysql_password
- kshitij.app.jwtSecret=your_secure_jwt_secret_key
- kshitij.app.jwtExpirationMs=86400000
- mvn clean install
- mvn spring-boot:run
- URL: /api/auth/signup
- Method: POST
- Description: Registers a new user with a username, email, and password. Optionally, roles can be assigned.
- If you mannually assign role then you can 'mod' for ROLE_MODERATE, 'user' for ROLE_USER, 'admin' for ROLE_ADMIN.
- 200 OK: User registered successfully.
- 400 Bad Request: Username or email already exists.
- URL: /api/auth/signin
- Method: POST
- Description: Authenticates a user and returns a JWT token upon successful login.
- 200 OK: Returns JWT token and user details.
- 401 Unauthorized: Invalid credentials.
- URL: /api/auth/logout
- Method: POST
- Description: Logs out the user by invalidating the JWT token.
- 200 OK: User logged out successfully.
- 400 Bad Request: Invalid or missing token.
- 401 Unauthorized: Authentication required.
- URL: /api/auth/admin/users
- Method: GET
- Description: Retrieves information of all registered users. Accessible only by admins.
- 200 OK: Returns a list of users with their details and roles.
- 403 Forbidden: Access denied for non-admin users.
- 401 Unauthorized: Authentication required.