Skip to content

Latest commit

ย 

History

23 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ” Spring Boot Authentication & Authorization System

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 .

๐Ÿ‘จโ€๐Ÿ’ป Author

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


๐Ÿš€ Features

๐Ÿ”‘ Authentication

  • User Registration
  • User Login
  • Logout
  • Email and Password based authentication
  • BCrypt password encryption
  • Custom login page
  • Login failure handling

๐Ÿ‘ฅ Role-Based Authorization

The application supports two roles:

  • USER
  • ADMIN

Access is controlled using Spring Security.

Role Access
USER User Dashboard
ADMIN Admin Dashboard + User Management

๐Ÿ‘จโ€๐Ÿ’ผ Admin Features

Admin users can:

  • View Admin Dashboard
  • View all registered users
  • Add new users
  • Activate / Deactivate users
  • Delete users
  • Assign USER role to newly created users

๐Ÿ‘ค User Features

Normal users can:

  • Register an account
  • Login securely
  • Access User Dashboard
  • Logout

๐Ÿ”„ Password Reset

The project includes a basic password reset workflow:

  1. User enters their registered email.
  2. System generates a unique reset token.
  3. Token is stored in the database.
  4. Token expires after 10 minutes.
  5. User enters the token and new password.
  6. Password is encrypted using BCrypt.
  7. 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.


๐Ÿ› ๏ธ Technologies Used

  • Java 21
  • Spring Boot 3.5.16
  • Spring Security 6
  • Spring Data JPA
  • Thymeleaf
  • Thymeleaf Spring Security Extras
  • MySQL
  • Lombok
  • Maven
  • HTML5
  • CSS3

๐Ÿ“‚ Project Structure

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

๐Ÿ” Spring Security Configuration

The project uses Spring Security to protect application endpoints.

Public Endpoints

/home/**
/login
/css/**
/js/**
/error
/register-user

User Endpoints

/user/**

Accessible by:

USER
ADMIN

Admin Endpoints

/admin/**

Accessible only by:

ADMIN

All other endpoints require authentication.


๐Ÿ”€ Login Flow

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

๐Ÿ”’ Password Security

Passwords are not stored as plain text.

The project uses:

BCryptPasswordEncoder

Example:

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

During registration:

Plain Password
      โ†“
BCrypt Encoder
      โ†“
Encrypted Password
      โ†“
MySQL Database

๐Ÿ—„๏ธ Database Configuration

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=true

Never upload your real database password or other secrets to GitHub.


โ–ถ๏ธ How to Run the Project

1. Clone the Repository

git clone YOUR_GITHUB_REPOSITORY_URL

2. Open the Project

Open the project in:

  • IntelliJ IDEA
  • Eclipse
  • VS Code
  • Spring Tool Suite

3. Configure MySQL

Create the database and update your database credentials.

4. Run the Application

Using Maven:

mvn spring-boot:run

Or run:

DemoApplication.java

from your IDE.


๐ŸŒ Application URLs

After starting the application:

Home

http://localhost:8080/home/home-page

Login

http://localhost:8080/login

Register

http://localhost:8080/register-user

Forgot Password

http://localhost:8080/home/forgot-password

Reset Password

http://localhost:8080/home/reset-password

User Dashboard

http://localhost:8080/user/user-home

Admin Dashboard

http://localhost:8080/admin/admin-home

Admin User Management

http://localhost:8080/admin/users

๐Ÿ“ก Important Endpoints

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

๐Ÿงฉ Main Components

SecurityConfig

Responsible for:

  • Spring Security configuration
  • URL authorization
  • Login configuration
  • Logout configuration
  • Password encoder
  • Role-based access control

CustomUserDetailsService

Loads users from the database using their email.

Email
 โ†“
UserRepo
 โ†“
MySQL
 โ†“
User Details
 โ†“
Spring Security

CustomSuccessHandler

Redirects users after successful login:

ADMIN โ†’ /admin/admin-home

USER โ†’ /user/user-home

UserService

Handles user creation and password encryption.

It supports:

registerNewUser()
registerNewAdmin()

UserRepo

Spring Data JPA repository responsible for database operations.

Important methods include:

findByEmail()
findByToken()
existsByEmail()

๐Ÿ—ƒ๏ธ User Entity

The User entity contains fields such as:

id
fullname
email
password
role
enabled
token
resetTokenExpiry

Roles are represented using:

public enum Role {
    USER,
    ADMIN
}

๐Ÿ–ฅ๏ธ Frontend

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

โš ๏ธ Development Notes

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

๐Ÿ”ฎ Future Improvements

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

๐Ÿ“ธ Project Screens

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

โญ Support

If you find this project useful, consider giving the repository a โญ on GitHub.


๐Ÿ“„ License

This project is available for educational and personal learning purposes.

About

๐Ÿ” Spring Boot Authentication & Authorization system using Spring Security, Thymeleaf, MySQL, and JPA with role-based access, user management, secure BCrypt passwords, and password reset functionality.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors