0% found this document useful (0 votes)
22 views5 pages

SpringBoot MongoDB Login Signup Guide

This document provides a step-by-step guide to create login and signup modules in Spring Boot with MongoDB. It covers project setup, package structure, dependencies, configuration, repository creation, password encoding, JWT utilities, controller implementation, Spring Security configuration, and application testing. The guide includes specific code snippets and instructions for each step to facilitate the development process.

Uploaded by

24mx347
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

SpringBoot MongoDB Login Signup Guide

This document provides a step-by-step guide to create login and signup modules in Spring Boot with MongoDB. It covers project setup, package structure, dependencies, configuration, repository creation, password encoding, JWT utilities, controller implementation, Spring Security configuration, and application testing. The guide includes specific code snippets and instructions for each step to facilitate the development process.

Uploaded by

24mx347
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Step-by-Step Guide to Create Login and

Signup Modules in Spring Boot with


MongoDB
1. Project Setup
Link: https://start.spring.io/

Create a Spring Boot project using Spring Initializr or Intellij Idea Paid Version with
dependencies:
- Spring Web
- Spring Security
- Spring Data MongoDB
- jjwt (for JWT token authentication).

2. Package Structure
Organize your code with the following structure:
- config
- controller
- dto
- model
- repository
- service
- security

3. Add Dependencies in pom.xml


Ensure `pom.xml` contains:
- spring-boot-starter-web
- spring-boot-starter-security
- spring-boot-starter-data-mongodb
- jjwt-api, jjwt-impl, jjwt-jackson
- spring-boot-starter-validation

4. Configure application.properties
Add MongoDB connection details:
```
server.port=8080

# MongoDB Connection
spring.data.mongodb.uri=mongodb+srv://shyamsundar41550:[email protected].
mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
spring.data.mongodb.database=login_db
5. Create Repository
Create `UserRepository` extending `MongoRepository<User, String>`.

Include methods like this,


Optional<User> findByUsername(String username);
boolean existsByUsername(String username);
}

6. Configure Password Encoder


Create a `@Bean` in `SecurityConfig` to return `PasswordEncoder` using
`BCryptPasswordEncoder`.

7. Implement JWT Utilities


- Generate JWT token on login.
- Validate token on protected requests.
- Create JwtUtil and JwtFilter classes.

8. Create Controllers and save it in MongoDB


Create `AuthController`:
- `@PostMapping("/api/auth/signup")` for registration and save the user
- `@PostMapping("/api/auth/login")` for login and authenticate with mongodb
- Return token on success

09. Configure Spring Security


- Allow `/api/auth/**` URLs to be accessed publicly
- Use `JwtAuthenticationFilter` for secured requests
- Disable CSRF, use stateless session management

10. Test the Application


Using Postman:
- POST to `/api/auth/signup` with name, email, password
- POST to `/api/auth/login` with email, password
- Use returned JWT in `Authorization: Bearer <token>` header to access secure APIs
Output from springBoot Tomcat Server :

You might also like