Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm developing a Java Spring Boot Web App and am currently trying to implement a "Forgot Password?" feature. As part of the feature, the user clicks on "Forgot Password?" link which takes them to forgotPassword.jsp. There, the user must enter their email address for the app so that a verification email can then be sent to their email account and they can then click on the link in the email to then create a new password. Once they click on the link in the email, they are redirected to resetPassword.jsp. On this page, I want there to be two fields, one which has a readonly, auto-filled field showing the user's email address, and the next input field which will allow the user to enter their new password.

My issue, though, is that I am for some reason currently unable to use the email that was provided in forgotPassword.jsp, on resetPassword.jsp. Right now, it says that in the resetPassword() controller method seen below that user.getEmail() is null, and so I can't provide a new password for the current account. I've pasted below the forgotPassword.jsp, resetPassword.jsp pages, as well as the relevant controller methods. I must have something wrong with the controller method parameters and/or the modelAttribute(s) in resetPassword(), but I'm not sure where. Any help would be appreciated, thanks.


What I have tried:

forgotPassword.jsp

<div id="editAccount">

    <div class="row parentRow">

        <div class="parentCol">

            <div class="panel panel-default"
                style="flex-direction: row; min-width: 60%;">

                <div style="text-align: center;">
                    <div
                        style="padding-top: 15%; font-size: x-large; color: black; padding-bottom: 3%;">Reset Password</div>
                </div>

                <div style="flex-grow: 1;">

                    <form:form modelAttribute="user" method="post">

                        <%-- <div class="errors">
                            <form:errors path="plainPassword" />
                        </div> --%>

                        <div class="input-group">
                            <form:input type="text" name="email" path="email"
                                placeholder="Email Address"
                                style="margin-top: 8%; min-width: 100%;" />
                        </div>

                        <div style="margin-top: 8%; text-align: center; min-width: 100%;">
                        
                            <input type="submit" name="submit" value="Reset"
                                class="suit_and_tie" /> 
                            <input type="reset" name="clear"
                                value="Clear" class="suit_and_tie" />
                                
                        </div>

                    </form:form>

                </div>

            </div>

        </div>

    </div>
    
</div>



resetPassword.jsp

<div id="editAccount">

    <div class="row parentRow">

        <div class="parentCol">

            <div class="panel panel-default"
                style="flex-direction: row; min-width: 60%;">

                <div style="text-align: center;">
                    <div
                        style="padding-top: 15%; font-size: x-large; color: black; padding-bottom: 3%;">Enter New Password</div>
                </div>

                <div style="flex-grow: 1;">

                    <form:form modelAttribute="user">

                        <div class="errors">
                            <form:errors path="plainPassword" />
                        </div>
                        
                        <div class="input-group">
                            <form:input type="text" name="email" path="email"
                                value="${user.email}" readonly
                                style="margin-top: 8%; min-width: 100%;" />
                        </div>

                        <div class="input-group">
                            <form:input type="password" name="newPassword" path="plainPassword"
                                placeholder="New Password" method="post"
                                style="margin-top: 8%; min-width: 100%;" />
                        </div>

                        <div style="margin-top: 8%; text-align: center; min-width: 100%;">
                        
                            <input type="submit" name="submit" value="Submit"
                                class="suit_and_tie" /> 
                            <input type="reset" name="clear"
                                value="Clear" class="suit_and_tie" />
                                
                        </div>

                    </form:form>

                </div>

            </div>

        </div>

    </div>

</div>


controller methods:

// Display the form
    @RequestMapping(value="/forgotPassword", method=RequestMethod.GET)
    public ModelAndView displayResetPassword(ModelAndView modelAndView, SiteUser user) {
        modelAndView.getModel().put("user", user);
        modelAndView.setViewName("app.forgotPassword");
        return modelAndView;
    }

    // Receive the address and send an email
    @RequestMapping(value="/forgotPassword", method=RequestMethod.POST)
    public ModelAndView forgotUserPassword(ModelAndView modelAndView, @ModelAttribute(value="user") SiteUser user) {
        SiteUser existingUser = userRepo.findByEmail(user.getEmail());
        
        System.out.println(user.getEmail());
        
        if (existingUser != null) {
            
            String token = userService.createEmailVerificationTokenForgotPassword(existingUser).toString();

            emailService.sendVerificationEmailForgotPassword(user.getEmail(), token);
            modelAndView.setViewName("redirect:/verifyEmail");

        } else {
            modelAndView.setViewName("redirect:/invalidUser");
        }
        return modelAndView;
    }
    
    
    @RequestMapping(value="/confirmReset", method= {RequestMethod.GET, RequestMethod.POST})
    ModelAndView confirmReset(ModelAndView modelAndView, @RequestParam("t") String tokenString, BindingResult result) {
        
        ForgotPasswordToken token = userService.getForgotPasswordToken(tokenString);
        
        if(token == null) {
            modelAndView.setViewName("redirect:/invalidUser");
            userService.deleteToken(token);
            return modelAndView;
        }
        
        Date expiryDate = token.getExpiryDate();
        
        if(expiryDate.before(new Date())) {
            modelAndView.setViewName("redirect:/expiredToken");
            userService.deleteToken(token);
            return modelAndView;
        }
        
        SiteUser user = token.getUser();
        
        if (user == null) {
            modelAndView.setViewName("redirect:/invalidUser");
            userService.deleteToken(token);
            return modelAndView;
        }
        
        userService.deleteToken(token);
        user.setEnabled(true);
        userService.save(user);
//      modelAndView.addObject("user", user);
//      modelAndView.addObject("email", user.getEmail());
        
        
        modelAndView.setViewName("redirect:/resetPassword");
        return modelAndView;
    }
    
    @RequestMapping(value="/resetPassword", method=RequestMethod.GET)
    public ModelAndView resetPassword(ModelAndView modelAndView, @ModelAttribute(value="user") SiteUser user) {
        
        //SiteUser user = userRepo.findByEmail(user.getEmail());
        modelAndView.getModel().put("user", user);
        System.out.println("here");
        System.out.println(user.getEmail());
        
        if (user.getEmail() != null)
        {
            SiteUser tokenUser = userRepo.findByEmail(user.getEmail());
            tokenUser.setPlainPassword((user.getPassword()));
            userRepo.save(tokenUser);
            modelAndView.setViewName("redirect:/passwordReset");
        } 
        
        else {
            modelAndView.setViewName("redirect:/invalidLink");
        }
        
        return modelAndView;
    }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900