VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/spring-security-resend-verification-email/

⇱ Spring Security – Resend Verification Email - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Spring Security – Resend Verification Email

Last Updated : 30 May, 2024

When building web applications, ensuring that users verify their email addresses is important to confirm their identity and secure access. In scenarios where the user does not receive the initial verification email or the verification link has expired. It becomes necessary to provide the option to resend the verification email. This feature can enhance the user experience by accommodating email delivery issues and user errors in accessing the email on time.

Resending the verification emails in the Spring Security application involves several key components and processes. Here is the detailed process:

User Registration Flow

  • User Registration: The user signs up with their email and password and possibly other details. The application can store these details in its database.
  • Email Verification Token: Upon registration, the application can generate a unique token linked to the user's email. This token is typically stored in the database along with its expiration time.
  • Sending the Verification Email: The email containing the verification link with the token is sent to the user's email addresses.
  • Account verification: The user can click the verification link, leading them to the application's verification endpoint. The application can check the token; if it is valid, the user's email is marked as verified of the user account.

Resend Verification Email Flow

  • Request to Resend the Verification Email: If the user did not receive the email or the token has been expired, then they can request to resend the verification email of the user account.
  • Token Validation and Regeneration: Before sending the email, the application can check if the existing token has expired. If it has, the new token is generated and stored.
  • Resending the Email: The new verification email with the updated token is sent to the user of the application.

Implementation to Resend Verification Email in Spring Security

Below are the implementation steps to resend verification email in Spring Security.

Step 1: Create a new Spring Boot project using Spring Initialize and include the required dependencies.

  • Spring Web
  • Spring Security
  • Java Mail Sender
  • Spring Data JPA
  • MySQL Driver
  • Validation
  • Thymeleaf
  • Lombok
  • Spring DevTools

After doing this step, the project structure will be like below:

👁 Project Structure


Step 2: Open the application.properties file and add the configuration for the MySQL database, mail and thymeleaf of the application.

# Database connection settings
spring.datasource.url=jdbc:mysql://localhost:3306/email_verification
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# Hibernate settings
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update

# Mail settings (replace with your mail server details)
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=mahi.looser123@gmail.com
spring.mail.password=rsgazwlsiokhazsl
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.main.allow-circular-references=true


Step 3: Create the new package and name it the model. In that package, create the new Java class and name it User.

Go to src > com.gfg.springsecurityaccountactivateemail > model > User and write the below code.


Step 4: Create a new package and name it the repository. In that package, create the new Java interface and name it UserRepository.

Go to src > com.gfg.springsecurityaccountactivateemail > repository > UserRepository and write the below code.


Step 5: Create a new package and name it the DTO. In that package, create the new Java class and name it RegistrationDto.

Go to src > com.gfg.springsecurityaccountactivateemail > dto > RegistrationDto and write the below code.


Step 6: Create a new package and name it the service. In that package, create the new Java class and name it UserService.

Go to src > com.gfg.springsecurityaccountactivateemail > service > UserService and write the below code.


Step 7: Create a new package and name it the service. In that package, create the new Java class and name it EmailService.

Go to src > com.gfg.springsecurityaccountactivateemail > service > EmailService and write the below code.


Step 8: Create a new package and name it the service. In that package, create the new Java class and name it VerificationTokenService.

Go to src > com.gfg.springsecurityaccountactivateemail > service > VerificationTokenService and write the below code.


Step 9: Create a new package and name it the service. In that package, create the new Java class and name it LoginService.

Go to src > com.gfg.springsecurityaccountactivateemail > service > LoginService and write the below code.


Step 10: Create a new package and name it the handler. In that package, create the new Java class and name it CustomAuthenticationFailure.

Go to src > com.gfg.springsecurityaccountactivateemail > handler > CustomAuthenticationFailure and write the below code.


Step 11: Create a new package and name it the handler. In that package, create the new Java class and name it CustomAuthenticationSuccess.

Go to src > com.gfg.springsecurityaccountactivateemail > handler > CustomAuthenticationSuccess and write the below code.


Step 12: Create a new package and name it config. In that package, create the new Java class and name it SecurityConfig.

Go to src > com.gfg.springsecurityaccountactivateemail > config > SecurityConfig and write the below code.


Step 13: Create a new package and name it util. In that package, create the new Java class and name it RandomStringGenerator.

Go to src > com.gfg.springsecurityaccountactivateemail > util > RandomStringGenerator and write the below code.


Step 14: Create a new package and name it the event. In that package, create the new Java class and name it OnRegistrationCompleteEvent.

Go to src > com.gfg.springsecurityaccountactivateemail > event > OnRegistrationCompleteEvent and write the below code.


Step 15: Create a new package and name it the event. In that package, create the new Java class and name it RegistrationListener.

Go to src > com.gfg.springsecurityaccountactivateemail > event > RegistrationListener and write the below code.


Step 16: Create a new package and name it the controller. In that package, create the new Java class and name it RegistrationController.

Go to src > com.gfg.springsecurityaccountactivateemail > controller > RegistrationController and write the below code.


Step 17: Create a new package and name it the controller. In that package, create the new Java class and name it LoginController.

Go to src > com.gfg.springsecurityaccountactivateemail > controller > LoginController and write the below code.


Step 18: Create a new package and name it the controller. In that package, create the new Java class and name it DashboardController.

Go to src > com.gfg.springsecurityaccountactivateemail > controller > DashboardController and write the below code.


Step 19: Create the index HTML file.

Go to src > main > resources > templates > index.html and put the below HTML code.


Step 20: Create the registration HTML file.

Go to src > main > resources > templates > registration.html and enter the below HTML code.


Step 21: Create the VerifyEmail HTML file.

Go to src > main > resources > templates > verify-email.html and enter the below HTML code.


Step 22: Create the login HTML file.

Go to src > main > resources > templates > login.html and enter the below HTML code.


Step 23: Create the verified email HTML file.

Go to src > main > resources > templates > verified-email.html and enter the below HTML code.


Step 24: Create the Dashboard HTML file.

Go to src > main > resources > templates > dashboard.html and enter the below HTML code.


Step 25: Run the Application

Now, we will run the application, and it will start start port at 8080.

👁 Application Running


HomePage

http://localhost:8080/

Output:

👁 Home Page


Register Page:

Click on the register page button.

http://localhost:8080/register

Output:

👁 Register Page


There is a 11 minute of time, then enable resend verification of the user account of the application.

👁 Time Limit


Resend Verification Mail:

Click on resend verification button:

👁 Resend Verification Button


Resend email messages:

👁 Resend Mail Message


Verification message:

👁 Email Verification Message


Once you click on the verify button, it will verify the account.

Dashboard:

Now, click on the dashboard, and then we will redirect to the dashboard.

👁 Dashboard

This project demonstrates how to resend the verification email in Spring Security in Spring Boot application.

Comment
Article Tags:

Explore