This is a Spring Boot app that lets users register, log in, and reset their password via email. It uses a simple in-memory database (H2) and sends emails for password resets. You can test it using Postman. Follow these steps to set it up and run it on a new computer.
- Java 17: Download from Adoptium.
- Maven: Download from Maven or install via your package manager (e.g.,
brew install mavenon Mac). - Postman: Download from Postman.
- IDE: IntelliJ IDEA (Community Edition) or Eclipse (free).
- Gmail or Mailtrap Account: For sending password reset emails.
-
Get the Project
- Copy the project folder (
auth-service) to your computer. - Open it in your IDE (e.g., IntelliJ IDEA:
File > Open, select the folder).
- Copy the project folder (
-
Check Project Files
- The project uses this structure:
auth-service/ ├── src/ │ ├── main/ │ │ ├── java/com/spring/auth_service/ │ │ │ ├── AuthServiceApplication.java │ │ │ ├── controller/AuthController.java │ │ │ ├── dto/ (data files) │ │ │ ├── entity/ (database files) │ │ │ ├── repository/ (database access) │ │ │ ├── service/ (business logic) │ │ │ ├── security/ (authentication) │ │ │ └── util/ (JWT tools) │ │ └── resources/ │ │ └── application.properties ├── pom.xml └── README.md
- The project uses this structure:
-
Configure Email Settings
- Open
src/main/resources/application.properties. - For Gmail:
- Set
spring.mail.usernameto your Gmail address (e.g.,example@gmail.com). - Create an App Password:
- Go to Google Account.
- Enable 2-Step Verification.
- Go to "Security" > "App passwords", select "Mail" and "Other (Spring Boot)", and generate a 16-character password.
- Set
spring.mail.passwordto this App Password.
- Set
- For Mailtrap (easier for testing):
- Sign up at Mailtrap.
- Create an inbox and find SMTP credentials (under "Integrations" > "SMTP").
- Set:
spring.mail.host=smtp.mailtrap.io spring.mail.port=587 spring.mail.username=your-mailtrap-username spring.mail.password=your-mailtrap-password
- Set
jwt.secretto a 64-character random string (e.g.,your-secure-jwt-secret-key-1234567890abcdefghijklmnopqrstuvwxyz1234567890abcdefghijk).
- Open
-
Build the Project
- Open a terminal in the project folder (
auth-service). - Run:
mvn clean install
- This downloads needed libraries and builds the app. You should see
BUILD SUCCESS.
- Open a terminal in the project folder (
-
Run the App
- In the terminal, run:
mvn spring-boot:run
- Or, in your IDE, right-click
AuthServiceApplication.javaand select "Run". - The app starts at
http://localhost:8080.
- In the terminal, run:
Use Postman to test the app’s features. Create a new collection in Postman called "Auth Service".
-
Register a User
- Method: POST
- URL:
http://localhost:8080/api/auth/register - Body (raw, JSON):
{ "name": "Basu", "surname": "SK", "email": "basusk@gmail.com", "phone": "1234567890", "password": "password123" } - Result: You should see
"User registered successfully".
-
Log In
- Method: POST
- URL:
http://localhost:8080/api/auth/login - Body (raw, JSON):
{ "username": "basusk@gmail.com", "password": "password123" } - Result: You get a long token (JWT) like
eyJhbGciOiJIUzUxMiJ9....
-
Forgot Password
- Method: POST
- URL:
http://localhost:8080/api/auth/forgot-password - Body (raw, JSON):
{ "email": "basusk@gmail.com" } - Result: You see
"New password sent to email". Check your Gmail inbox (or spam) or Mailtrap inbox for the new password.
-
Test Protected Endpoint
- Method: GET
- URL:
http://localhost:8080/api/auth/test - Headers: Add
Authorization: Bearer <JWT_TOKEN>(use the token from login). - Result: You see
"Protected endpoint accessed".
- Open
http://localhost:8080/h2-console. - Use:
- JDBC URL:
jdbc:h2:mem:testdb - Username:
sa - Password: (leave blank)
- JDBC URL:
- Run
SELECT * FROM users;to see registered users.
- Email Not Sent: Check
application.propertiesfor correct SMTP settings. Use Mailtrap if Gmail doesn’t work. - Login Fails: Ensure the email/phone and password match a registered user. Check the H2 database.
- Errors: Run
mvn clean install -e -Xfor details and ask for help with the error log. - Port Conflict: If
8080is busy, addserver.port=8081toapplication.properties.
- Try tutorials on Spring Boot.
- Learn about JWT at jwt.io.
- Explore Postman for API testing.
Happy coding!