A complete Authentication and Authorization project built using Spring Boot, Spring Security, Thymeleaf, Spring Data JPA, and MySQL.
This project implements user registration, secure login, role-based authorization, admin user management, password reset using token-based verification, and custom error pages.
Before starting project implement your application.properties file has name in project is eg-application.properties rename that into application properties and implement that .
Keshav Janglwa
An Engineering Student.
Built as a Spring Boot backend/authentication project for learning and practicing:
Java
Spring Boot
Spring Security
Spring Data JPA
MySQL
Thymeleaf
MVC Architecture
Authentication & Authorization
- User Registration
- User Login
- Logout
- Email and Password based authentication
- BCrypt password encryption
- Custom login page
- Login failure handling
The application supports two roles:
USERADMIN
Access is controlled using Spring Security.
| Role | Access |
|---|---|
| USER | User Dashboard |
| ADMIN | Admin Dashboard + User Management |
Admin users can:
- View Admin Dashboard
- View all registered users
- Add new users
- Activate / Deactivate users
- Delete users
- Assign
USERrole to newly created users
Normal users can:
- Register an account
- Login securely
- Access User Dashboard
- Logout
The project includes a basic password reset workflow:
- User enters their registered email.
- System generates a unique reset token.
- Token is stored in the database.
- Token expires after 10 minutes.
- User enters the token and new password.
- Password is encrypted using BCrypt.
- Reset token is removed after successful password change.
Note: The current implementation displays the generated reset token on the page for development/testing purposes. In a production application, the token should be sent through a secure email service.
- Java 21
- Spring Boot 3.5.16
- Spring Security 6
- Spring Data JPA
- Thymeleaf
- Thymeleaf Spring Security Extras
- MySQL
- Lombok
- Maven
- HTML5
- CSS3
src
โโโ main
โโโ java
โ โโโ SpringBoot_Auth
โ โโโ demo
โ โโโ Config
โ โ โโโ SecurityConfig.java
โ โ
โ โโโ Controller
โ โ โโโ AdminController.java
โ โ โโโ AuthController.java
โ โ โโโ HomeController.java
โ โ โโโ UserController.java
โ โ
โ โโโ Entity
โ โ โโโ Role.java
โ โ โโโ User.java
โ โ
โ โโโ Repository
โ โ โโโ UserRepo.java
โ โ
โ โโโ Service
โ โ โโโ CustomSuccessHandler.java
โ โ โโโ CustomUserDetailsService.java
โ โ โโโ UserService.java
โ โ
โ โโโ DemoApplication.java
โ
โโโ resources
โโโ static
โ โโโ css
โ โโโ style.css
โ โโโ styles.css
โ
โโโ templates
โโโ admin
โ โโโ add-user.html
โ โโโ users.html
โ
โโโ error
โ โโโ 403.html
โ โโโ 404.html
โ
โโโ adminpage.html
โโโ forgot-password.html
โโโ home.html
โโโ login.html
โโโ register.html
โโโ reset-password.html
โโโ userpage.html
The project uses Spring Security to protect application endpoints.
/home/**
/login
/css/**
/js/**
/error
/register-user
/user/**
Accessible by:
USER
ADMIN
/admin/**
Accessible only by:
ADMIN
All other endpoints require authentication.
After successful login, users are redirected according to their role.
Login
โ
โผ
Spring Security
โ
โผ
Check User Role
/ \
/ \
ADMIN USER
โ โ
โผ โผ
Admin Dashboard User Dashboard
This redirection is handled by:
CustomSuccessHandler.java
Passwords are not stored as plain text.
The project uses:
BCryptPasswordEncoderExample:
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}During registration:
Plain Password
โ
BCrypt Encoder
โ
Encrypted Password
โ
MySQL Database
Create a MySQL database before running the application.
Example:
CREATE DATABASE springboot_auth;Then configure your database credentials in:
src/main/resources/application.properties
Example configuration:
spring.application.name=SpringBoot-Auth
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_auth
spring.datasource.username=root
spring.datasource.password=YOUR_PASSWORD
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=trueNever upload your real database password or other secrets to GitHub.
git clone YOUR_GITHUB_REPOSITORY_URLOpen the project in:
- IntelliJ IDEA
- Eclipse
- VS Code
- Spring Tool Suite
Create the database and update your database credentials.
Using Maven:
mvn spring-boot:runOr run:
DemoApplication.java
from your IDE.
After starting the application:
http://localhost:8080/home/home-page
http://localhost:8080/login
http://localhost:8080/register-user
http://localhost:8080/home/forgot-password
http://localhost:8080/home/reset-password
http://localhost:8080/user/user-home
http://localhost:8080/admin/admin-home
http://localhost:8080/admin/users
| Method | Endpoint | Description |
|---|---|---|
| GET | /login |
Login page |
| GET | /register-user |
Registration page |
| POST | /register-user |
Register user |
| GET | /user/user-home |
User dashboard |
| GET | /admin/admin-home |
Admin dashboard |
| GET | /admin/users |
List users |
| GET | /admin/users/add |
Add user page |
| POST | /admin/users/add |
Add user |
| POST | /admin/users/{id}/toggle-status |
Enable/disable user |
| POST | /admin/users/{id}/delete |
Delete user |
| GET | /home/forgot-password |
Forgot password page |
| POST | /home/forgot-password |
Generate reset token |
| GET | /home/reset-password |
Reset password page |
| POST | /home/reset-password |
Change password |
| POST | /logout |
Logout |
Responsible for:
- Spring Security configuration
- URL authorization
- Login configuration
- Logout configuration
- Password encoder
- Role-based access control
Loads users from the database using their email.
Email
โ
UserRepo
โ
MySQL
โ
User Details
โ
Spring Security
Redirects users after successful login:
ADMIN โ /admin/admin-home
USER โ /user/user-home
Handles user creation and password encryption.
It supports:
registerNewUser()
registerNewAdmin()
Spring Data JPA repository responsible for database operations.
Important methods include:
findByEmail()
findByToken()
existsByEmail()The User entity contains fields such as:
id
fullname
email
password
role
enabled
token
resetTokenExpiry
Roles are represented using:
public enum Role {
USER,
ADMIN
}The frontend uses:
- HTML
- CSS
- Thymeleaf
Thymeleaf templates are located inside:
src/main/resources/templates
CSS files are located inside:
src/main/resources/static/css
This project is intended primarily for learning and development.
Before using it in production, consider adding:
- Email-based password reset
- Stronger password validation
- Confirm-password validation
- CSRF protection
- Input validation
- Global exception handling
- Secure environment variables
- HTTPS
- Rate limiting
- Account lockout
- Better token management
- Audit logging
Possible improvements for this project:
- Email verification
- Real email-based password reset
- JWT authentication
- Refresh tokens
- User profile management
- Admin role management
- Pagination for users
- Search and filter users
- Password strength validation
- REST API integration
- Docker support
- Deployment using AWS
- PostgreSQL support
The project contains separate Thymeleaf pages for:
- Login
- Registration
- User Dashboard
- Admin Dashboard
- User Management
- Add User
- Forgot Password
- Reset Password
- 403 Unauthorized
- 404 Not Found
If you find this project useful, consider giving the repository a โญ on GitHub.
This project is available for educational and personal learning purposes.