{"id":486,"date":"2024-07-31T20:22:32","date_gmt":"2024-07-31T20:22:32","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/spring-boot\/?p=486"},"modified":"2024-08-02T13:21:42","modified_gmt":"2024-08-02T13:21:42","slug":"spring-boot-password-reset","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/spring-boot\/spring-boot-password-reset\/","title":{"rendered":"Spring Boot \u2013 Password Reset"},"content":{"rendered":"<p>In this tutorial, you will learn how to implement Forget Password functionality in your SpringBoot application.<\/p>\n<figure id=\"attachment_506\" aria-describedby=\"caption-attachment-506\" style=\"width: 733px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2024\/07\/User-Password-Reset-Workflow-scaled.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-506 size-large\" src=\"https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2024\/07\/User-Password-Reset-Workflow-733x1024.jpg\" alt=\"Spring Boot User Password Reset Workflow\" width=\"733\" height=\"1024\" srcset=\"https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2024\/07\/User-Password-Reset-Workflow-733x1024.jpg 733w, https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2024\/07\/User-Password-Reset-Workflow-215x300.jpg 215w, https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2024\/07\/User-Password-Reset-Workflow-768x1073.jpg 768w, https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2024\/07\/User-Password-Reset-Workflow-1099x1536.jpg 1099w, https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2024\/07\/User-Password-Reset-Workflow-1466x2048.jpg 1466w, https:\/\/kindsonthegenius.com\/spring-boot\/wp-content\/uploads\/2024\/07\/User-Password-Reset-Workflow-scaled.jpg 1832w\" sizes=\"auto, (max-width: 733px) 100vw, 733px\" \/><\/a><figcaption id=\"caption-attachment-506\" class=\"wp-caption-text\">Spring Boot User Password Reset Workflow<\/figcaption><\/figure>\n<p><strong>The Password Reset Flow<\/strong><\/p>\n<ol>\n<li>From the login page, the user clicks on the <em>&#8216;Forgot Password&#8217;<\/em> link.<\/li>\n<li>The request hits the \/forgotPassword route in the SecurityController<\/li>\n<li>The<em> forgotPassword.html<\/em> template (which is in the security directory) is returned to the user. This template has a form with a single email field.<\/li>\n<li>User enters his email and submits the form.<\/li>\n<li>This new request arrives via the <em>\/passwordRequest<\/em> route in the PasswordResetController<\/li>\n<li>The forgottenPassword(email) is invoked with the passed in user&#8217;s email<\/li>\n<li>If and email exists, then a token is generated and send along to the user via a password reset email<\/li>\n<li>User receives the email and clicks on the link in the email<\/li>\n<li>The request arrives via the <em>\/passwordChange<\/em> route in the PasswordResetController<\/li>\n<li>The changePassword() method sends the user the password reset form along with the token. This form contains two fields: newPassword and confirmPassword<\/li>\n<li>User fills the form and submits via the \/passwordChange route<\/li>\n<li>The updatePassword is called with the password and token and the user data is saved<\/li>\n<li>The user is redirected to the passwordChangeSuccessful page<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h3 style=\"text-align: center;\"><strong>Showing the\u00a0 Forgot Password Form<\/strong><\/h3>\n<p><strong>Step 1 &#8211; Setup the <em>\/forgotPassword<\/em> route<\/strong><\/p>\n<p>This is the first endpoint that would be hit when a user clicks on the <em>Forgot Password<\/em> link in the login form where the user must enter his email or username.<\/p>\n<ol>\n<li>Add the\u00a0 <em>forgotPassword()<\/em> method to the SecurityController<\/li>\n<\/ol>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@GetMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/forgotPassword\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> String <span style=\"color: #0066bb; font-weight: bold;\">forgotPassword<\/span><span style=\"color: #333333;\">(){<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"security\/forgotPassword\"<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>2. Create the <a href=\"https:\/\/raw.githubusercontent.com\/KindsonTheGenius\/fleetms-v2\/master\/src\/main\/resources\/templates\/security\/forgotPassword.html\" target=\"_blank\" rel=\"noopener\"><em>forgotPassword.html<\/em> <\/a>template in the security folder. This is the template that displays a form for use to enter his email. <a href=\"https:\/\/raw.githubusercontent.com\/KindsonTheGenius\/fleetms-v2\/master\/src\/main\/resources\/templates\/security\/forgotPassword.html\" target=\"_blank\" rel=\"noopener\">Get it here<\/a>.<\/p>\n<p>3. Update the AppSecurityConfig to allow the <em>\/forgotPassword<\/em> route<\/p>\n<p>4. <strong>Test(1)<\/strong> the <em>\/forgotPassword<\/em> route<\/p>\n<p>&nbsp;<\/p>\n<h3 style=\"text-align: center;\"><strong>Sending the ForgetPassword Email<\/strong><\/h3>\n<p><strong>Step 2 &#8211; Create the ForgotPasswordEmailContext<\/strong><\/p>\n<p>This would be similar to the AccountVerificationEmail context but for password reset request. It will extend AbstractEmailContext.<\/p>\n<p>The difference would be that it would use the forgot-password template from the mailing folder.\u00a0 Do the following:<\/p>\n<ul>\n<li>add the forgotPassword.html template to the security folder. <a href=\"https:\/\/raw.githubusercontent.com\/KindsonTheGenius\/fleetms-v2\/master\/src\/main\/resources\/templates\/security\/forgotPassword.html\" target=\"_blank\" rel=\"noopener\">Get it from here<\/a>.<\/li>\n<li>copy and modify the AccountVerificationEmailContext. The different could be in the verification url path and the name of course.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Step 3 &#8211; Setup the <em>\/passwordRequest<\/em> Route and <em>sendPasswordResetEmail<\/em> Function<\/strong><\/p>\n<p>When the user enters his email in the <em>forgotPassword.html<\/em> form and submits, the request comes to this route. Do the following:<\/p>\n<ol>\n<li>Create and Implement the UserAccountService for managing password reset request. It would have two functions\n<ul>\n<li><em>forgottenPassword(username)<\/em> for accepting the request containing the user email<\/li>\n<li><em>updatePassword(password, token)<\/em> for performing password update. Takes the new password and performs the update<\/li>\n<\/ul>\n<\/li>\n<li>Write the <em>sendResetPasswordEmail(user)<\/em> function. This function takes the user as input and does the following:\n<ul>\n<li>create a secureToken<\/li>\n<li>sets the user and saves the token<\/li>\n<li>creates a new ForgottenPasswordEmailContext<\/li>\n<li>sets the user and the token<\/li>\n<li>sends the email<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>The <em>sendResetPasswordResetEmail()<\/em> is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">protected<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">sendResetPasswordEmail<\/span><span style=\"color: #333333;\">(<\/span>User user<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    SecureToken secureToken<span style=\"color: #333333;\">=<\/span> secureTokenService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">createSecureToken<\/span><span style=\"color: #333333;\">();<\/span>\r\n    secureToken<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">setUser<\/span><span style=\"color: #333333;\">(<\/span>user<span style=\"color: #333333;\">);<\/span>\r\n    secureTokenRepository<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">save<\/span><span style=\"color: #333333;\">(<\/span>secureToken<span style=\"color: #333333;\">);<\/span>\r\n    ForgotPasswordEmailContext emailContext <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> ForgotPasswordEmailContext<span style=\"color: #333333;\">();<\/span>\r\n    emailContext<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">init<\/span><span style=\"color: #333333;\">(<\/span>user<span style=\"color: #333333;\">);<\/span>\r\n    emailContext<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">setToken<\/span><span style=\"color: #333333;\">(<\/span>secureToken<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getToken<\/span><span style=\"color: #333333;\">());<\/span>\r\n    emailContext<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">buildVerificationUrl<\/span><span style=\"color: #333333;\">(<\/span>baseURL<span style=\"color: #333333;\">,<\/span> secureToken<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getToken<\/span><span style=\"color: #333333;\">());<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">try<\/span> <span style=\"color: #333333;\">{<\/span>\r\n        emailService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">sendMail<\/span><span style=\"color: #333333;\">(<\/span>emailContext<span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #333333;\">}<\/span> <span style=\"color: #008800; font-weight: bold;\">catch<\/span> <span style=\"color: #333333;\">(<\/span>MessagingException e<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n        e<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">printStackTrace<\/span><span style=\"color: #333333;\">();<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>3. Override the<em> forgottenPassword(user)<\/em> function. It simply finds the user and sends the email<\/p>\n<p>4. Add the forgot-password html template to the mailing folder. <a href=\"https:\/\/raw.githubusercontent.com\/KindsonTheGenius\/fleetms-v2\/master\/src\/main\/resources\/templates\/mailing\/forgot-password.html\" target=\"_blank\" rel=\"noopener\">Get it from here<\/a>.<\/p>\n<p>5. Implement the \/passwordRequest controller method. It&#8217;s named resetPassword. Here&#8217;s the implementation here:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@PostMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/passwordRequest\"<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #888888;\">\/\/ Display form with only email field<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> String <span style=\"color: #0066bb; font-weight: bold;\">resetPassword<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #008800; font-weight: bold;\">final<\/span> ResetPasswordData forgotPasswordForm<span style=\"color: #333333;\">,<\/span> RedirectAttributes redirAttr<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">try<\/span> <span style=\"color: #333333;\">{<\/span>\r\n        customerAccountService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">forgottenPassword<\/span><span style=\"color: #333333;\">(<\/span>forgotPasswordForm<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getEmail<\/span><span style=\"color: #333333;\">());<\/span> <span style=\"color: #888888;\">\/\/ Send email<\/span>\r\n    <span style=\"color: #333333;\">}<\/span> <span style=\"color: #008800; font-weight: bold;\">catch<\/span> <span style=\"color: #333333;\">(<\/span>UnkownIdentifierException e<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n       <span style=\"color: #888888;\">\/\/ log the error<\/span>\r\n        redirAttr<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addFlashAttribute<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"error\"<\/span><span style=\"color: #333333;\">,<\/span>\r\n                messageSource<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getMessage<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"error\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #008800; font-weight: bold;\">null<\/span><span style=\"color: #333333;\">,<\/span> LocaleContextHolder<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getLocale<\/span><span style=\"color: #333333;\">())<\/span>\r\n        <span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n    redirAttr<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addFlashAttribute<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"message\"<\/span><span style=\"color: #333333;\">,<\/span>\r\n            messageSource<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getMessage<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"user.forgotpwd.msg\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #008800; font-weight: bold;\">null<\/span><span style=\"color: #333333;\">,<\/span> LocaleContextHolder<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getLocale<\/span><span style=\"color: #333333;\">())<\/span>\r\n    <span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> REDIRECT_LOGIN<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>6. <strong>Test(2)<\/strong> that email is raised by the<em> \/passwordRequest<\/em><\/p>\n<p>&nbsp;<\/p>\n<h3 style=\"text-align: center;\"><strong>Displaying the ChangePassword Form by Clicking the Link<\/strong><\/h3>\n<p><strong>Step 4 &#8211; Create the PasswordResetData and PasswordResetController<\/strong><\/p>\n<p>1. Create the <em>PasswordResetData<\/em>. This is the POJO for data used for password reset and would have\u00a0 4 string fields:<\/p>\n<ul>\n<li>email<\/li>\n<li>token<\/li>\n<li>password<\/li>\n<li>repeatPassword<\/li>\n<\/ul>\n<p>2. Create the <em>PasswordResetController<\/em> and write the controller method <em>\/passwordRequest<\/em>. This method is named <em>resetPassword<\/em> and is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@PostMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/passwordRequest\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> String <span style=\"color: #0066bb; font-weight: bold;\">resetPassword<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #008800; font-weight: bold;\">final<\/span> ResetPasswordData forgotPasswordForm<span style=\"color: #333333;\">,<\/span> RedirectAttributes redirAttr<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">try<\/span> <span style=\"color: #333333;\">{<\/span>\r\n        customerAccountService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">forgottenPassword<\/span><span style=\"color: #333333;\">(<\/span>forgotPasswordForm<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getEmail<\/span><span style=\"color: #333333;\">());<\/span> <span style=\"color: #888888;\">\/\/ Send email<\/span>\r\n    <span style=\"color: #333333;\">}<\/span> <span style=\"color: #008800; font-weight: bold;\">catch<\/span> <span style=\"color: #333333;\">(<\/span>UnkownIdentifierException e<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n       <span style=\"color: #888888;\">\/\/ log the error<\/span>\r\n        redirAttr<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addFlashAttribute<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"error\"<\/span><span style=\"color: #333333;\">,<\/span>\r\n                messageSource<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getMessage<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"error\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #008800; font-weight: bold;\">null<\/span><span style=\"color: #333333;\">,<\/span> LocaleContextHolder<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getLocale<\/span><span style=\"color: #333333;\">())<\/span>\r\n        <span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n    redirAttr<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addFlashAttribute<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"message\"<\/span><span style=\"color: #333333;\">,<\/span>\r\n            messageSource<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getMessage<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"user.forgotpwd.msg\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #008800; font-weight: bold;\">null<\/span><span style=\"color: #333333;\">,<\/span> LocaleContextHolder<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getLocale<\/span><span style=\"color: #333333;\">())<\/span>\r\n    <span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> REDIRECT_LOGIN<span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>3. Update the AppSecurityConfig to allow <em>\/passwordRequest<\/em> route<\/p>\n<p>4. Create the changePassword template in the security folder. <a href=\"https:\/\/raw.githubusercontent.com\/KindsonTheGenius\/fleetms-v2\/master\/src\/main\/resources\/templates\/security\/changePassword.html\" target=\"_blank\" rel=\"noopener\">Get it here<\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 5 &#8211; Setup the \/passwordChange route for GET<\/strong><\/p>\n<p>This is the route that will be hit when the user clicks on the link in the email. The token is received and if it&#8217;s not empty, we create a ResetPasswordData form, sets the token and sends it to the user via the changePassword template.<\/p>\n<p>1. Write the controller method for passwordChange. The method is given below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@GetMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/passwordChange\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> String <span style=\"color: #0066bb; font-weight: bold;\">changePassword<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #555555; font-weight: bold;\">@RequestParam<\/span><span style=\"color: #333333;\">(<\/span>required <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">false<\/span><span style=\"color: #333333;\">)<\/span> String token<span style=\"color: #333333;\">,<\/span> <span style=\"color: #008800; font-weight: bold;\">final<\/span> RedirectAttributes redirAttr<span style=\"color: #333333;\">,<\/span> <span style=\"color: #008800; font-weight: bold;\">final<\/span> Model model<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>StringUtils<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">isEmpty<\/span><span style=\"color: #333333;\">(<\/span>token<span style=\"color: #333333;\">))<\/span> <span style=\"color: #333333;\">{<\/span>\r\n        redirAttr<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addFlashAttribute<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"tokenError\"<\/span><span style=\"color: #333333;\">,<\/span>\r\n                messageSource<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getMessage<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"user.registration.verification.missing.token\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #008800; font-weight: bold;\">null<\/span><span style=\"color: #333333;\">,<\/span> LocaleContextHolder<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getLocale<\/span><span style=\"color: #333333;\">())<\/span>\r\n        <span style=\"color: #333333;\">);<\/span>\r\n        <span style=\"color: #008800; font-weight: bold;\">return<\/span> REDIRECT_LOGIN<span style=\"color: #333333;\">;<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n    ResetPasswordData data <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> ResetPasswordData<span style=\"color: #333333;\">();<\/span>\r\n    data<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">setToken<\/span><span style=\"color: #333333;\">(<\/span>token<span style=\"color: #333333;\">);<\/span>\r\n    setResetPasswordForm<span style=\"color: #333333;\">(<\/span>model<span style=\"color: #333333;\">,<\/span> data<span style=\"color: #333333;\">);<\/span> <span style=\"color: #888888;\">\/\/ add the resetPassword form to the model to send to the template<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"security\/changePassword\"<\/span><span style=\"color: #333333;\">;<\/span> \u00a0\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>You will have an error at the setPasswordResetForm(model, data)<\/p>\n<p>2. Create the <em>setPasswordResetForm()<\/em> function &#8211; it adds the passwordResetData to the model. The key is &#8220;forgotPassword&#8221;.<\/p>\n<p>3. Update the AppSecurityConfig to allow <em>\/passwordChange<\/em>\u00a0route<\/p>\n<p>4. <strong>Test(3)<\/strong> that the link in the email displays the password request form<\/p>\n<p>&nbsp;<\/p>\n<h3 style=\"text-align: center;\"><strong>Performing the Password Update (Fill and Submit new Password)<\/strong><\/h3>\n<p><strong>Step 7 &#8211; Setup the \/passwordChange Route for Post<\/strong><\/p>\n<p>Here, the user enters and confirms the new password and clicks on submit.<\/p>\n<ol>\n<li>Override the <em>updatePassword()<\/em> method in the UserAccountService. You can uncomment the code and implement the method. This is given below:<\/li>\n<\/ol>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Override<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">updatePassword<\/span><span style=\"color: #333333;\">(<\/span>String password<span style=\"color: #333333;\">,<\/span> String token<span style=\"color: #333333;\">)<\/span> <span style=\"color: #008800; font-weight: bold;\">throws<\/span> InvalidTokenException<span style=\"color: #333333;\">,<\/span> UnknownIdentifierException <span style=\"color: #333333;\">{<\/span>\r\n    SecureToken secureToken <span style=\"color: #333333;\">=<\/span> secureTokenService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">findByToken<\/span><span style=\"color: #333333;\">(<\/span>token<span style=\"color: #333333;\">);<\/span>\r\n    \r\n    <span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>Objects<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">isNull<\/span><span style=\"color: #333333;\">(<\/span>secureToken<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">|<\/span> <span style=\"color: #333333;\">!<\/span>StringUtils<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">equals<\/span><span style=\"color: #333333;\">(<\/span>token<span style=\"color: #333333;\">,<\/span> secureToken<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getToken<\/span><span style=\"color: #333333;\">())<\/span> <span style=\"color: #333333;\">|<\/span> secureToken<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">isExpired<\/span><span style=\"color: #333333;\">()){<\/span>\r\n        <span style=\"color: #008800; font-weight: bold;\">throw<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #0066bb; font-weight: bold;\">InvalidTokenException<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Token does not exist\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n    \r\n    User user <span style=\"color: #333333;\">=<\/span> userRepository<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getById<\/span><span style=\"color: #333333;\">(<\/span>secureToken<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getUser<\/span><span style=\"color: #333333;\">().<\/span><span style=\"color: #0000cc;\">getId<\/span><span style=\"color: #333333;\">());<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>Objects<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">isNull<\/span><span style=\"color: #333333;\">(<\/span>user<span style=\"color: #333333;\">)){<\/span>\r\n        <span style=\"color: #008800; font-weight: bold;\">throw<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #0066bb; font-weight: bold;\">UnknownIdentifierException<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"User does not exist for this user\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n    \r\n    user<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">setPassword<\/span><span style=\"color: #333333;\">(<\/span>cryptPasswordEncoder<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">encode<\/span><span style=\"color: #333333;\">(<\/span>password<span style=\"color: #333333;\">));<\/span>\r\n    userRepository<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">save<\/span><span style=\"color: #333333;\">(<\/span>user<span style=\"color: #333333;\">);<\/span>\r\n    secureTokenService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">removeToken<\/span><span style=\"color: #333333;\">(<\/span>secureToken<span style=\"color: #333333;\">);<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>2. Write the <em>changePassword()<\/em> controller method. This would call the updatePassword method from the service.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@PostMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/passwordChange\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> String <span style=\"color: #0066bb; font-weight: bold;\">changePassword<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #008800; font-weight: bold;\">final<\/span> ResetPasswordData data<span style=\"color: #333333;\">,<\/span> Model model<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">try<\/span> <span style=\"color: #333333;\">{<\/span>\r\n        userAccountService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">updatePassword<\/span><span style=\"color: #333333;\">(<\/span>data<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getPassword<\/span><span style=\"color: #333333;\">(),<\/span> data<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getToken<\/span><span style=\"color: #333333;\">());<\/span>\r\n    <span style=\"color: #333333;\">}<\/span> <span style=\"color: #008800; font-weight: bold;\">catch<\/span> <span style=\"color: #333333;\">(<\/span>InvalidTokenException <span style=\"color: #333333;\">|<\/span> UnknownIdentifierException e<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n        e<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">printStackTrace<\/span><span style=\"color: #333333;\">();<\/span>\r\n        model<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addAttribute<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"tokenError\"<\/span><span style=\"color: #333333;\">,<\/span>\r\n                messageSource<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getMessage<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"user.registration.verification.invalid.token\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #008800; font-weight: bold;\">null<\/span><span style=\"color: #333333;\">,<\/span> LocaleContextHolder<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getLocale<\/span><span style=\"color: #333333;\">()));<\/span>\r\n        <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"security\/changePassword\"<\/span><span style=\"color: #333333;\">;<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n    model<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">addAttribute<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"passwordUpdatedMsg\"<\/span><span style=\"color: #333333;\">,<\/span>\r\n            messageSource<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getMessage<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"user.password.updated.msg\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"color: #008800; font-weight: bold;\">null<\/span><span style=\"color: #333333;\">,<\/span> LocaleContextHolder<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getLocale<\/span><span style=\"color: #333333;\">()));<\/span>\r\n    setResetPasswordForm<span style=\"color: #333333;\">(<\/span>model<span style=\"color: #333333;\">,<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> ResetPasswordData<span style=\"color: #333333;\">());<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"security\/passwordChangeSuccessful\"<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>3. Create the <em>passwordChangeSuccessful.html<\/em> template in the security folder.<a href=\"https:\/\/raw.githubusercontent.com\/KindsonTheGenius\/fleetms-v2\/master\/src\/main\/resources\/templates\/security\/changePasswordSuccessful.html\" target=\"_blank\" rel=\"noopener\"> Get it here<\/a>.<\/p>\n<p>4. Add the user.password.updated.msg to your messages.properties file<\/p>\n<p>5. <strong>Test(4)<\/strong> that the password is updated after a new password is entered and submitted.<\/p>\n<p>&nbsp;<\/p>\n<p>Happy coding!!<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to implement Forget Password functionality in your SpringBoot application. The Password Reset Flow From the login page, the &hellip; <!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-486","post","type-post","status-publish","format-standard","hentry","category-spring-boot-tutorials"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/comments?post=486"}],"version-history":[{"count":22,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/486\/revisions"}],"predecessor-version":[{"id":509,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/posts\/486\/revisions\/509"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/media?parent=486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/categories?post=486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/spring-boot\/wp-json\/wp\/v2\/tags?post=486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}