From 628a293299976f357d54f0d57b0cc0a572cb5bf9 Mon Sep 17 00:00:00 2001 From: DOHA Date: Mon, 16 Feb 2015 19:46:35 +0200 Subject: [PATCH] modify registration api --- ...er.java => OldRegistrationController.java} | 80 ++++++++++++------- .../controller/RegistrationController.java | 64 ++++++--------- .../src/main/webapp/WEB-INF/view/badUser.jsp | 6 +- .../webapp/WEB-INF/view/forgetPassword.jsp | 6 +- .../webapp/WEB-INF/view/updatePassword.jsp | 2 +- 5 files changed, 86 insertions(+), 72 deletions(-) rename spring-security-login-and-registration/src/main/java/org/baeldung/web/controller/{Registration2Controller.java => OldRegistrationController.java} (69%) diff --git a/spring-security-login-and-registration/src/main/java/org/baeldung/web/controller/Registration2Controller.java b/spring-security-login-and-registration/src/main/java/org/baeldung/web/controller/OldRegistrationController.java similarity index 69% rename from spring-security-login-and-registration/src/main/java/org/baeldung/web/controller/Registration2Controller.java rename to spring-security-login-and-registration/src/main/java/org/baeldung/web/controller/OldRegistrationController.java index 8d702d669e..fbf29277e5 100644 --- a/spring-security-login-and-registration/src/main/java/org/baeldung/web/controller/Registration2Controller.java +++ b/spring-security-login-and-registration/src/main/java/org/baeldung/web/controller/OldRegistrationController.java @@ -14,14 +14,13 @@ import org.baeldung.persistence.service.IUserService; import org.baeldung.persistence.service.UserDto; import org.baeldung.registration.OnRegistrationCompleteEvent; import org.baeldung.validation.EmailExistsException; -import org.baeldung.web.error.UserNotFoundException; -import org.baeldung.web.util.GenericResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.MessageSource; import org.springframework.core.env.Environment; +import org.springframework.mail.MailAuthenticationException; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.security.access.prepost.PreAuthorize; @@ -37,11 +36,11 @@ import org.springframework.web.bind.annotation.ModelAttribute; 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.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; @Controller -public class Registration2Controller { +@RequestMapping(value = "/old") +public class OldRegistrationController { private final Logger LOGGER = LoggerFactory.getLogger(getClass()); @Autowired @@ -62,13 +61,13 @@ public class Registration2Controller { @Autowired private Environment env; - public Registration2Controller() { + public OldRegistrationController() { } // API - @RequestMapping(value = "/user/registration2", method = RequestMethod.GET) + @RequestMapping(value = "/user/registration", method = RequestMethod.GET) public String showRegistrationForm(final HttpServletRequest request, final Model model) { LOGGER.debug("Rendering registration page."); final UserDto accountDto = new UserDto(); @@ -76,7 +75,7 @@ public class Registration2Controller { return "registration"; } - @RequestMapping(value = "/regitrationConfirm2", method = RequestMethod.GET) + @RequestMapping(value = "/regitrationConfirm", method = RequestMethod.GET) public String confirmRegistration(final HttpServletRequest request, final Model model, @RequestParam("token") final String token) { final Locale locale = request.getLocale(); @@ -102,7 +101,7 @@ public class Registration2Controller { return "redirect:/login.html?lang=" + locale.getLanguage(); } - @RequestMapping(value = "/user/registration2", method = RequestMethod.POST) + @RequestMapping(value = "/user/registration", method = RequestMethod.POST) public ModelAndView registerUserAccount(@ModelAttribute("user") @Valid final UserDto accountDto, final BindingResult result, final HttpServletRequest request, final Errors errors) { LOGGER.debug("Registering user account with information: {}", accountDto); if (result.hasErrors()) { @@ -124,34 +123,54 @@ public class Registration2Controller { return new ModelAndView("successRegister", "user", accountDto); } - @RequestMapping(value = "/user/resendRegistrationToken2", method = RequestMethod.GET) - public @ResponseBody GenericResponse resendRegistrationToken2(final HttpServletRequest request, @RequestParam("token") final String existingToken) { + @RequestMapping(value = "/user/resendRegistrationToken", method = RequestMethod.GET) + public String resendRegistrationToken(final HttpServletRequest request, final Model model, @RequestParam("token") final String existingToken) { + final Locale locale = request.getLocale(); final VerificationToken newToken = userService.generateNewVerificationToken(existingToken); final User user = userService.getUser(newToken.getToken()); - final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); - final SimpleMailMessage email = constructResetVerificationTokenEmail(appUrl, request.getLocale(), newToken, user); - mailSender.send(email); - return new GenericResponse(messages.getMessage("message.resendToken", null, request.getLocale())); + try { + final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); + final SimpleMailMessage email = constructResetVerificationTokenEmail(appUrl, request.getLocale(), newToken, user); + mailSender.send(email); + } catch (final MailAuthenticationException e) { + LOGGER.debug("MailAuthenticationException", e); + return "redirect:/emailError.html?lang=" + locale.getLanguage(); + } catch (final Exception e) { + LOGGER.debug(e.getLocalizedMessage(), e); + model.addAttribute("message", e.getLocalizedMessage()); + return "redirect:/login.html?lang=" + locale.getLanguage(); + } + model.addAttribute("message", messages.getMessage("message.resendToken", null, locale)); + return "redirect:/login.html?lang=" + locale.getLanguage(); } - @RequestMapping(value = "/user/resetPassword2", method = RequestMethod.POST) - public @ResponseBody GenericResponse resetPassword2(final HttpServletRequest request, @RequestParam("email") final String userEmail) { + @RequestMapping(value = "/user/resetPassword", method = RequestMethod.POST) + public String resetPassword(final HttpServletRequest request, final Model model, @RequestParam("email") final String userEmail) { final User user = userService.findUserByEmail(userEmail); if (user == null) { - throw new UserNotFoundException(); + model.addAttribute("message", messages.getMessage("message.userNotFound", null, request.getLocale())); + return "redirect:/login.html?lang=" + request.getLocale().getLanguage(); } final String token = UUID.randomUUID().toString(); userService.createPasswordResetTokenForUser(user, token); - final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); - final SimpleMailMessage email = constructResetTokenEmail(appUrl, request.getLocale(), token, user); - mailSender.send(email); - - return new GenericResponse(messages.getMessage("message.resetPasswordEmail", null, request.getLocale())); + try { + final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); + final SimpleMailMessage email = constructResetTokenEmail(appUrl, request.getLocale(), token, user); + mailSender.send(email); + } catch (final MailAuthenticationException e) { + LOGGER.debug("MailAuthenticationException", e); + return "redirect:/emailError.html?lang=" + request.getLocale().getLanguage(); + } catch (final Exception e) { + LOGGER.debug(e.getLocalizedMessage(), e); + model.addAttribute("message", e.getLocalizedMessage()); + return "redirect:/login.html?lang=" + request.getLocale().getLanguage(); + } + model.addAttribute("message", messages.getMessage("message.resetPasswordEmail", null, request.getLocale())); + return "redirect:/login.html?lang=" + request.getLocale().getLanguage(); } - // ==== will not be changed this is used as the confirmation url of reset password token - @RequestMapping(value = "/user/changePassword2", method = RequestMethod.GET) + @RequestMapping(value = "/user/changePassword", method = RequestMethod.GET) public String changePassword(final HttpServletRequest request, final Model model, @RequestParam("id") final long id, @RequestParam("token") final String token) { final Locale locale = request.getLocale(); @@ -175,18 +194,21 @@ public class Registration2Controller { return "redirect:/updatePassword.html?lang=" + locale.getLanguage(); } - @RequestMapping(value = "/user/savePassword2", method = RequestMethod.POST) + @RequestMapping(value = "/user/savePassword", method = RequestMethod.POST) @PreAuthorize("hasRole('READ_PRIVILEGE')") - public @ResponseBody GenericResponse savePassword(final HttpServletRequest request, @RequestParam("password") final String password) { + public String savePassword(final HttpServletRequest request, final Model model, @RequestParam("password") final String password) { + final Locale locale = request.getLocale(); + final User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); userService.changeUserPassword(user, password); - return new GenericResponse(messages.getMessage("message.resetPasswordSuc", null, request.getLocale())); + model.addAttribute("message", messages.getMessage("message.resetPasswordSuc", null, locale)); + return "redirect:/login.html?lang=" + locale; } // NON-API private final SimpleMailMessage constructResetVerificationTokenEmail(final String contextPath, final Locale locale, final VerificationToken newToken, final User user) { - final String confirmationUrl = contextPath + "/regitrationConfirm.html?token=" + newToken.getToken(); + final String confirmationUrl = contextPath + "/old/regitrationConfirm.html?token=" + newToken.getToken(); final String message = messages.getMessage("message.resendToken", null, locale); final SimpleMailMessage email = new SimpleMailMessage(); email.setSubject("Resend Registration Token"); @@ -197,7 +219,7 @@ public class Registration2Controller { } private final SimpleMailMessage constructResetTokenEmail(final String contextPath, final Locale locale, final String token, final User user) { - final String url = contextPath + "/user/changePassword2?id=" + user.getId() + "&token=" + token; + final String url = contextPath + "/old/user/changePassword?id=" + user.getId() + "&token=" + token; final String message = messages.getMessage("message.resetPassword", null, locale); final SimpleMailMessage email = new SimpleMailMessage(); email.setTo(user.getEmail()); diff --git a/spring-security-login-and-registration/src/main/java/org/baeldung/web/controller/RegistrationController.java b/spring-security-login-and-registration/src/main/java/org/baeldung/web/controller/RegistrationController.java index 5879e5ddb3..89ba0c00df 100644 --- a/spring-security-login-and-registration/src/main/java/org/baeldung/web/controller/RegistrationController.java +++ b/spring-security-login-and-registration/src/main/java/org/baeldung/web/controller/RegistrationController.java @@ -14,13 +14,14 @@ import org.baeldung.persistence.service.IUserService; import org.baeldung.persistence.service.UserDto; import org.baeldung.registration.OnRegistrationCompleteEvent; import org.baeldung.validation.EmailExistsException; +import org.baeldung.web.error.UserNotFoundException; +import org.baeldung.web.util.GenericResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.MessageSource; import org.springframework.core.env.Environment; -import org.springframework.mail.MailAuthenticationException; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.security.access.prepost.PreAuthorize; @@ -36,6 +37,7 @@ import org.springframework.web.bind.annotation.ModelAttribute; 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.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; @Controller @@ -123,54 +125,38 @@ public class RegistrationController { } @RequestMapping(value = "/user/resendRegistrationToken", method = RequestMethod.GET) - public String resendRegistrationToken(final HttpServletRequest request, final Model model, @RequestParam("token") final String existingToken) { - final Locale locale = request.getLocale(); + @ResponseBody + public GenericResponse resendRegistrationToken(final HttpServletRequest request, @RequestParam("token") final String existingToken) { final VerificationToken newToken = userService.generateNewVerificationToken(existingToken); final User user = userService.getUser(newToken.getToken()); - try { - final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); - final SimpleMailMessage email = constructResetVerificationTokenEmail(appUrl, request.getLocale(), newToken, user); - mailSender.send(email); - } catch (final MailAuthenticationException e) { - LOGGER.debug("MailAuthenticationException", e); - return "redirect:/emailError.html?lang=" + locale.getLanguage(); - } catch (final Exception e) { - LOGGER.debug(e.getLocalizedMessage(), e); - model.addAttribute("message", e.getLocalizedMessage()); - return "redirect:/login.html?lang=" + locale.getLanguage(); - } - model.addAttribute("message", messages.getMessage("message.resendToken", null, locale)); - return "redirect:/login.html?lang=" + locale.getLanguage(); + final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); + final SimpleMailMessage email = constructResetVerificationTokenEmail(appUrl, request.getLocale(), newToken, user); + mailSender.send(email); + + return new GenericResponse(messages.getMessage("message.resendToken", null, request.getLocale())); } + // Reset password + @RequestMapping(value = "/user/resetPassword", method = RequestMethod.POST) - public String resetPassword(final HttpServletRequest request, final Model model, @RequestParam("email") final String userEmail) { + @ResponseBody + public GenericResponse resetPassword(final HttpServletRequest request, @RequestParam("email") final String userEmail) { final User user = userService.findUserByEmail(userEmail); if (user == null) { - model.addAttribute("message", messages.getMessage("message.userNotFound", null, request.getLocale())); - return "redirect:/login.html?lang=" + request.getLocale().getLanguage(); + throw new UserNotFoundException(); } final String token = UUID.randomUUID().toString(); userService.createPasswordResetTokenForUser(user, token); - try { - final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); - final SimpleMailMessage email = constructResetTokenEmail(appUrl, request.getLocale(), token, user); - mailSender.send(email); - } catch (final MailAuthenticationException e) { - LOGGER.debug("MailAuthenticationException", e); - return "redirect:/emailError.html?lang=" + request.getLocale().getLanguage(); - } catch (final Exception e) { - LOGGER.debug(e.getLocalizedMessage(), e); - model.addAttribute("message", e.getLocalizedMessage()); - return "redirect:/login.html?lang=" + request.getLocale().getLanguage(); - } - model.addAttribute("message", messages.getMessage("message.resetPasswordEmail", null, request.getLocale())); - return "redirect:/login.html?lang=" + request.getLocale().getLanguage(); + final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); + final SimpleMailMessage email = constructResetTokenEmail(appUrl, request.getLocale(), token, user); + mailSender.send(email); + + return new GenericResponse(messages.getMessage("message.resetPasswordEmail", null, request.getLocale())); } @RequestMapping(value = "/user/changePassword", method = RequestMethod.GET) - public String changePassword(final HttpServletRequest request, final Model model, @RequestParam("id") final long id, @RequestParam("token") final String token) { + public String showChangePasswordPage(final HttpServletRequest request, final Model model, @RequestParam("id") final long id, @RequestParam("token") final String token) { final Locale locale = request.getLocale(); final PasswordResetToken passToken = userService.getPasswordResetToken(token); @@ -195,13 +181,11 @@ public class RegistrationController { @RequestMapping(value = "/user/savePassword", method = RequestMethod.POST) @PreAuthorize("hasRole('READ_PRIVILEGE')") - public String savePassword(final HttpServletRequest request, final Model model, @RequestParam("password") final String password) { - final Locale locale = request.getLocale(); - + @ResponseBody + public GenericResponse savePassword(final HttpServletRequest request, @RequestParam("password") final String password) { final User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); userService.changeUserPassword(user, password); - model.addAttribute("message", messages.getMessage("message.resetPasswordSuc", null, locale)); - return "redirect:/login.html?lang=" + locale; + return new GenericResponse(messages.getMessage("message.resetPasswordSuc", null, request.getLocale())); } // NON-API diff --git a/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/badUser.jsp b/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/badUser.jsp index 919bcaeb69..4ce72a5226 100644 --- a/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/badUser.jsp +++ b/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/badUser.jsp @@ -31,7 +31,7 @@ code="label.form.loginSignUp"> diff --git a/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/forgetPassword.jsp b/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/forgetPassword.jsp index c30ed63ab7..90fec55d9b 100644 --- a/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/forgetPassword.jsp +++ b/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/forgetPassword.jsp @@ -43,7 +43,7 @@ diff --git a/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/updatePassword.jsp b/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/updatePassword.jsp index 61c17cd749..7f11576a4d 100644 --- a/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/updatePassword.jsp +++ b/spring-security-login-and-registration/src/main/webapp/WEB-INF/view/updatePassword.jsp @@ -51,7 +51,7 @@ function savePass(){ $("#error").show(); return; } - $.post("",{password: pass} ,function(data){ + $.post("",{password: pass} ,function(data){ window.location.href = "" + "?message="+data.message; }) .fail(function(data) {