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 e324fed271..72730aa23e 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
@@ -19,6 +19,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.MessageSource;
+import org.springframework.http.MediaType;
import org.springframework.mail.MailAuthenticationException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
@@ -35,6 +36,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
@@ -121,7 +123,6 @@ public class RegistrationController {
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());
try {
final String appUrl = request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
@@ -135,32 +136,19 @@ public class RegistrationController {
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/resendRegistrationToken2", method = RequestMethod.GET)
- public String resendRegistrationToken2(final HttpServletRequest request, final Model model, @RequestParam("token") final String existingToken) {
- final Locale locale = request.getLocale();
+ @RequestMapping(value = "/user/resendRegistrationToken2", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
+ public @ResponseBody String resendRegistrationToken2(final HttpServletRequest request, final Model model, @RequestParam("token") final String existingToken) {
final VerificationToken newToken = userService.generateNewVerificationToken(existingToken);
-
final User user = userService.getUser(newToken.getToken());
- try {
- final String appUrl = 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 = request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
+ final SimpleMailMessage email = constructResetVerificationTokenEmail(appUrl, request.getLocale(), newToken, user);
+ System.out.println(email.getText());
+ mailSender.send(email);
+ return messages.getMessage("message.resendToken", null, request.getLocale());
}
@RequestMapping(value = "/user/resetPassword", method = RequestMethod.POST)
diff --git a/spring-security-login-and-registration/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java b/spring-security-login-and-registration/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java
new file mode 100644
index 0000000000..2ef260c580
--- /dev/null
+++ b/spring-security-login-and-registration/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java
@@ -0,0 +1,35 @@
+package org.baeldung.web.error;
+
+import org.springframework.mail.MailAuthenticationException;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.context.request.WebRequest;
+import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+@ControllerAdvice
+public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
+
+ public RestResponseEntityExceptionHandler() {
+ super();
+ }
+
+ // API
+
+ // 500
+ @ExceptionHandler({ MailAuthenticationException.class })
+ public @ResponseBody String handleMail(final RuntimeException ex, final WebRequest request) throws JsonProcessingException {
+ logger.error("500 Status Code", ex);
+ return new ObjectMapper().writeValueAsString(ex.getClass().toString());
+ }
+
+ @ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class })
+ public @ResponseBody String handleInternal(final RuntimeException ex, final WebRequest request) throws JsonProcessingException {
+ logger.error("500 Status Code", ex);
+ return new ObjectMapper().writeValueAsString(ex.getClass().toString());
+ }
+
+}
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 ecde61988f..cc5d606ab2 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
@@ -23,12 +23,28 @@ code="label.form.loginSignUp">
${label.form.resendRegistrationToken}
-
-
- ">
+
-
+
+
+
+
+