NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

package com.demo.controller;

import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.demo.model.User;
import com.demo.service.EmailService;
import com.demo.UserService;

@Controller
public class PasswordController {

@Autowired
private UserService userService;

@Autowired
private EmailService emailService;

@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;

// Display forgotPassword page
@RequestMapping(value = "/forgot", method = RequestMethod.GET)
public ModelAndView displayForgotPasswordPage() {
return new ModelAndView("forgotPassword");
}

// Process form submission from forgotPassword page
@RequestMapping(value = "/forgot", method = RequestMethod.POST)
public ModelAndView processForgotPasswordForm(ModelAndView modelAndView, @RequestParam("email") String userEmail, HttpServletRequest request) {

// Lookup user in database by e-mail
Optional<User> optional = userService.findUserByEmail(userEmail);

if (!optional.isPresent()) {
modelAndView.addObject("errorMessage", "We didn't find an account for that e-mail address.");
} else {

// Generate random 36-character string token for reset password
User user = optional.get();
user.setResetToken(UUID.randomUUID().toString());

// Save token to database
userService.saveUser(user);

String appUrl = request.getScheme() + "://" + request.getServerName();

// Email message
SimpleMailMessage passwordResetEmail = new SimpleMailMessage();
passwordResetEmail.setFrom("[email protected]");
passwordResetEmail.setTo(user.getEmail());
passwordResetEmail.setSubject("Password Reset Request");
passwordResetEmail.setText("To reset your password, click the link below:n" + appUrl
+ "/reset?token=" + user.getResetToken());

emailService.sendEmail(passwordResetEmail);

// Add success message to view
modelAndView.addObject("successMessage", "A password reset link has been sent to " + userEmail);
}

modelAndView.setViewName("forgotPassword");
return modelAndView;

}

// Display form to reset password
@RequestMapping(value = "/reset", method = RequestMethod.GET)
public ModelAndView displayResetPasswordPage(ModelAndView modelAndView, @RequestParam("token") String token) {

Optional<User> user = userService.findUserByResetToken(token);

if (user.isPresent()) { // Token found in DB
modelAndView.addObject("resetToken", token);
} else { // Token not found in DB
modelAndView.addObject("errorMessage", "Oops! This is an invalid password reset link.");
}

modelAndView.setViewName("resetPassword");
return modelAndView;
}

// Process reset password form
@RequestMapping(value = "/reset", method = RequestMethod.POST)
public ModelAndView setNewPassword(ModelAndView modelAndView, @RequestParam Map<String, String> requestParams, RedirectAttributes redir) {

// Find the user associated with the reset token
Optional<User> user = userService.findUserByResetToken(requestParams.get("token"));

// This should always be non-null but we check just in case
if (user.isPresent()) {

User resetUser = user.get();

// Set new password
resetUser.setPassword(bCryptPasswordEncoder.encode(requestParams.get("password")));

// Set the reset token to null so it cannot be used again
resetUser.setResetToken(null);

// Save user
userService.saveUser(resetUser);

// In order to set a model attribute on a redirect, we must use
// RedirectAttributes
redir.addFlashAttribute("successMessage", "You have successfully reset your password. You may now login.");

modelAndView.setViewName("redirect:login");
return modelAndView;

} else {
modelAndView.addObject("errorMessage", "Oops! This is an invalid password reset link.");
modelAndView.setViewName("resetPassword");
}

return modelAndView;
}

// Going to reset page without a token redirects to login page
@ExceptionHandler(MissingServletRequestParameterException.class)
public ModelAndView handleMissingParams(MissingServletRequestParameterException ex) {
return new ModelAndView("redirect:login");
}
}
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.