SPRING SECURITY Reset password with sending email using JWT

╄→尐↘猪︶ㄣ 提交于 2021-02-11 12:15:41

问题


So, I'm working on a web application using angular on frontend, JWT tokens, and Spring Boot microservices on the backend, and to Reset password with sending the email I found a tuto it worked for me but when migrating to my project it didn't work (in the Controller) cause it's with ModelandView and I'm working with ResponseEntity. So How can i change ModelandView to ResponseEntity in controller


/**
     * Display the forgot password page and form
     */
    @RequestMapping(value="/forgot-password", method=RequestMethod.GET)
    public ModelAndView displayResetPassword(ModelAndView modelAndView, User user) {
        modelAndView.addObject("user", user);
        modelAndView.setViewName("forgotPassword");
        return modelAndView;
    }

    /**
     * Receive email of the user, create token and send it via email to the user
     */
    @RequestMapping(value="/forgot-password", method=RequestMethod.POST)
    public ModelAndView forgotUserPassword(ModelAndView modelAndView, User user) {
        User existingUser = userRepository.findByEmailIdIgnoreCase(user.getEmailId());
        if(existingUser != null) {
            // create token
            ConfirmationToken confirmationToken = new ConfirmationToken(existingUser);
            
            // save it
            confirmationTokenRepository.save(confirmationToken);
            
            // create the email
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setTo(existingUser.getEmailId());
            mailMessage.setSubject("Complete Password Reset!");
            mailMessage.setFrom("bouraouimalek011@gmail.com");
                    mailMessage.setText("To complete the password reset process, please click here: "
            +"http://localhost:8082/confirm-reset?token="+confirmationToken.getConfirmationToken());
            
            emailSenderService.sendEmail(mailMessage);

            modelAndView.addObject("message", "Request to reset password received. Check your inbox for the reset link.");
            modelAndView.setViewName("successForgotPassword");

        } else {    
            modelAndView.addObject("message", "This email does not exist!");
            modelAndView.setViewName("error");
        }
        
        return modelAndView;
    }


----------

    @RequestMapping(value="/confirm-reset", method= {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView validateResetToken(ModelAndView modelAndView, @RequestParam("token")String confirmationToken)
    {
        ConfirmationToken token = confirmationTokenRepository.findByConfirmationToken(confirmationToken);
        
        if(token != null) {
            User user = userRepository.findByEmailIdIgnoreCase(token.getUser().getEmailId());
            user.setEnabled(true);
            userRepository.save(user);
            modelAndView.addObject("user", user);
            modelAndView.addObject("emailId", user.getEmailId());
            modelAndView.setViewName("resetPassword");
        } else {
            modelAndView.addObject("message", "The link is invalid or broken!");
            modelAndView.setViewName("error");
        }
        
        return modelAndView;
    }   


----------


    /**
     * Receive the token from the link sent via email and display form to reset password
     */
    @RequestMapping(value = "/reset-password", method = RequestMethod.POST)
    public ModelAndView resetUserPassword(ModelAndView modelAndView, User user) {
        // ConfirmationToken token = confirmationTokenRepository.findByConfirmationToken(confirmationToken);
        
        if(user.getEmailId() != null) {
            // use email to find user
            User tokenUser = userRepository.findByEmailIdIgnoreCase(user.getEmailId());
            tokenUser.setEnabled(true);
            tokenUser.setPassword(encoder.encode(user.getPassword()));
            // System.out.println(tokenUser.getPassword());
            userRepository.save(tokenUser);
            modelAndView.addObject("message", "Password successfully reset. You can now log in with the new credentials.");
            modelAndView.setViewName("successResetPassword");
        } else {
            modelAndView.addObject("message","The link is invalid or broken!");
            modelAndView.setViewName("error");
        }
        
        return modelAndView;
    }


----------

This is the project im working at https://github.com/BouraouiMalek/Spring-Security-using-JWT

来源:https://stackoverflow.com/questions/64403483/spring-security-reset-password-with-sending-email-using-jwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!