This project is a Backend REST API developed using Spring Boot and Spring Security. It incorporates JWT-based authentication, OAuth2 login with Google, email confirmation, and password recovery functionalities.
The primary goal of this project is to provide a secure and robust authentication and authorization system for backend services. It demonstrates how to integrate various security features, including:
- JWT Authentication: Enables stateless authentication for RESTful services.
- OAuth2 with Google: Allows users to log in using their Google accounts.
- Email Confirmation: Ensures that users verify their email addresses during registration.
- Forgotten Password: Provides functionality for users to reset their passwords securely again with email confirmation.
- Exception Handling: The project incorporates comprehensive exception handling to effectively manage all types of errors that may arise during interactions with the security system.
Ensure you have the following installed:
- Java 21 (I guess other versions might work as well)
- Maven
- MySQL
-
Clone the repository:
git clone https://github.com/BelisAliosmanova/SpringSecurity.git cd SpringSecurity
-
Configure the database:
-
Create a MySQL database named
springSecurity
. (Or use thisjdbc:mysql://localhost:3306/springSecurity?createDatabaseIfNotExist=true
for an easier approach.) -
Update the
application.yaml
file with your database credentials:datasource: url: ${MYSQL_URL} // jdbc:mysql://localhost:3306/springSecurity?createDatabaseIfNotExist=true username: ${MYSQLUSER} // root password: ${MYSQLPASSWORD} // blank driver-class-name: com.mysql.cj.jdbc.Driver
-
-
Configure email settings:
- Update the
application.yaml
file with your email SMTP server details to enable email confirmation and password reset functionalities.
mail: host: smtp.gmail.com port: 465 protocol: smtps username: ${GMAIL_USERNAME} // your email SMTP server details password: ${GMAIL_PASSWORD} // your email SMTP server details properties: mail: transport: protocol: smtps smtps: auth: true starttls: enable: true timeout: 8000
- Update the
-
Configure Google OAuth2:
-
Obtain OAuth2 credentials from the Google Developer Console.
-
Update the
application.yaml
file with your Google client ID and client secret:oauth2: resourceserver: opaquetoken: introspection-uri: https://www.googleapis.com client-id: ${GOOGLE_CLIENT_ID} // your Google client ID client-secret: ${GOOGLE_CLIENT_SECRET} // your Google client secret
-
-
Build and run the application:
mvn clean install mvn spring-boot:run
- After a user registers, they will receive a confirmation email. Clicking the link in the email enables their account in the database.
- If the user doesn't confirm their email - they can't use the profile and the created user will be deleted after 24hours. (UserCleanupScheduler makes that)
![](https://private-user-images.githubusercontent.com/107267662/399159325-5e097644-016c-4c30-b145-d956f636fe05.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4NTg3ODMsIm5iZiI6MTczODg1ODQ4MywicGF0aCI6Ii8xMDcyNjc2NjIvMzk5MTU5MzI1LTVlMDk3NjQ0LTAxNmMtNGMzMC1iMTQ1LWQ5NTZmNjM2ZmUwNS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjA2JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIwNlQxNjE0NDNaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT02ZTZhM2M3MmY2MDZmOTcxOWM5MDUyZDQwNjBlN2EyOTdhNDFjMDhkMjRiYzc5OGYyMzZjMTgyODM3NzA1NTc2JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.31ckdTqrpLTF9F--q3nR_83Xj0Vz2law7pJM8WXvdI8)
- With the proper email and password, you recieve the JTW tokens
![](https://private-user-images.githubusercontent.com/107267662/399159756-69a5f555-4057-4dfb-8940-62d94a07a4f3.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4NTg3ODMsIm5iZiI6MTczODg1ODQ4MywicGF0aCI6Ii8xMDcyNjc2NjIvMzk5MTU5NzU2LTY5YTVmNTU1LTQwNTctNGRmYi04OTQwLTYyZDk0YTA3YTRmMy5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjA2JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIwNlQxNjE0NDNaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0yN2QzYzZjMTRiNjc3YjZiOTQ3YjA1YWI5MGQwMWRiMWVlMGEyZTU0MjRhYTc0OTFhNzBmYWE0ZmRkYjgwMmM0JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.NpbImoyNLKGRO-okhEg1wx6AaLuqgSn4SPL26BbOnqg)
- When a user provides their email, the system sends an email containing a link with a token to reset their password.
- Here is an example of how the link shoud look:
http://localhost:8080/api/v1/auth/registrationConfirm?token=889f5170-25b9-4bee-a49a-4e927bfe7d59
![](https://private-user-images.githubusercontent.com/107267662/399160104-28478ef9-ec32-4686-bb0a-2acacfb85409.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4NTg3ODMsIm5iZiI6MTczODg1ODQ4MywicGF0aCI6Ii8xMDcyNjc2NjIvMzk5MTYwMTA0LTI4NDc4ZWY5LWVjMzItNDY4Ni1iYjBhLTJhY2FjZmI4NTQwOS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjA2JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIwNlQxNjE0NDNaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT04NTA0ZjQ1YzBhMDQ1ZGY4NWZlOWU2NjdiNDkzZDBjZjgwODRmODZmNDA5N2VmM2RjNjFhODgyYzdmODhkYzI3JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.NsseIzsEfCVdzV-r1S5VU1pBCnS6mnqbHtYmfqI0tW8)
![](https://private-user-images.githubusercontent.com/107267662/399160377-a2c0cfed-3954-42ff-ba70-440c7df84732.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4NTg3ODMsIm5iZiI6MTczODg1ODQ4MywicGF0aCI6Ii8xMDcyNjc2NjIvMzk5MTYwMzc3LWEyYzBjZmVkLTM5NTQtNDJmZi1iYTcwLTQ0MGM3ZGY4NDczMi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjA2JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIwNlQxNjE0NDNaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1lNDZjY2E2N2I5MzFhZTkzYjQ3NThhZTRkMGMxNzlhZDc3YjQxMWY0ZjFiN2Y0ZmEzZGNiNmZmMzI5Y2M1M2RiJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.vrcyPv7mzQS83oY8RgAUtgfNLogYtkHXww4QCe4S62Y)
- Accessing
/api/v1/oauth2/url/google
will generate a link that opens the Google OAuth2 window, allowing you to select the profile you wish to log in with.
![](https://private-user-images.githubusercontent.com/107267662/399161417-9dae55c5-9aa3-4a97-8020-2c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XotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjA2JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIwNlQxNjE0NDNaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1hOGVlMjVmNTYwODM3OTNlOGQ1NDgxZmEyMjZlMzI5OGEyZjg1Y2ZmNTZhZGFiM2ZhMmExZmE3NTQ1YjE0ZjIwJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.XbysDIr-vkN80_qJRro4o4kSqcLo9lg0ISixb9UP3N4)
- After selecting the email you want to log in with, the system generates a link containing a code that you will need to use for the
/authenticate/google
endpoint. - Example of the generated link:
http://localhost:8081/process-oauth2?code=4%2F0AanRRrsJm6RGgnffRwKEezRW_1riG-4-LszeX8qYJ0dTclo-fawM7YNtC7SoJocuVvxmxg&scope=email+profile+openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&authuser=1&prompt=none
![](https://private-user-images.githubusercontent.com/107267662/399162096-4037a346-26d7-4508-9a30-b9cb60543633.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4NTg3ODMsIm5iZiI6MTczODg1ODQ4MywicGF0aCI6Ii8xMDcyNjc2NjIvMzk5MTYyMDk2LTQwMzdhMzQ2LTI2ZDctNDUwOC05YTMwLWI5Y2I2MDU0MzYzMy5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjA2JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIwNlQxNjE0NDNaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT00ZThjN2UzMDNmNTMzNzNmMzExMTQ0NmNjODZjYjIxYzFmNmQ2NDRmZmUyMGUzOTdjYzc1ZTFlMTIyYWRkYWFiJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.JPTFqf7SSvJB2Q0ocuJ_TUQdQ0LYuKuYpmvqpk_FlPA)