Merge pull request #130 from Doha2012/master

modify spring reset
This commit is contained in:
Eugen 2015-02-08 01:53:32 +02:00
commit 8a15a50227
4 changed files with 52 additions and 44 deletions

View File

@ -1,7 +1,5 @@
package org.baeldung.web.controller;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.UUID;
@ -20,6 +18,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.mail.MailAuthenticationException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.access.prepost.PreAuthorize;
@ -85,9 +84,6 @@ public class RegistrationController {
final User user = verificationToken.getUser();
final Calendar cal = Calendar.getInstance();
final DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
System.out.println(df.format(verificationToken.getExpiryDate()));
System.out.println(df.format(cal.getTime()));
if ((verificationToken.getExpiryDate().getTime() - cal.getTime().getTime()) <= 0) {
model.addAttribute("message", messages.getMessage("auth.message.expired", null, locale));
@ -129,9 +125,13 @@ public class RegistrationController {
try {
final SimpleMailMessage email = constructResetVerificationTokenEmail(request.getContextPath(), request.getLocale(), newToken, user);
mailSender.send(email);
} catch (final Exception e) {
// MailException
} catch (final MailAuthenticationException e) {
LOGGER.debug("MailAuthenticationException");
return "redirect:/emailError.html?lang=" + locale.getLanguage();
} catch (final Exception e) {
LOGGER.debug(e.getLocalizedMessage());
model.addAttribute("error", 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();
@ -141,24 +141,23 @@ public class RegistrationController {
public String resetPassword(final WebRequest request, final Model model, @RequestParam("email") final String userEmail) {
final User user = userService.findUserByEmail(userEmail);
if (user == null) {
model.addAttribute("message", messages.getMessage("auth.message.expired", null, request.getLocale()));
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);
try {
final String url = request.getContextPath() + "/user/changePassword?id=" + user.getId() + "&token=" + token;
final String message = messages.getMessage("message.resetPassword", null, request.getLocale());
final SimpleMailMessage email = new SimpleMailMessage();
email.setTo(user.getEmail());
email.setSubject("Reset Password");
email.setText(message + " \r\n" + "http://localhost:8080" + url);
System.out.println(email.getText());
final SimpleMailMessage email = constructResetTokenEmail(request.getContextPath(), request.getLocale(), token, user);
LOGGER.debug(email.getText());
mailSender.send(email);
System.out.println(email.getText());
} catch (final Exception e) {
} catch (final MailAuthenticationException e) {
LOGGER.debug("MailAuthenticationException");
return "redirect:/emailError.html?lang=" + request.getLocale().getLanguage();
} catch (final Exception e) {
LOGGER.debug(e.getLocalizedMessage());
model.addAttribute("error", e.getLocalizedMessage());
return "redirect:/login.html?lang=" + request.getLocale().getLanguage();
}
model.addAttribute("message", messages.getMessage("message.resetPassword", null, request.getLocale()));
return "redirect:/login.html?lang=" + request.getLocale().getLanguage();
@ -173,8 +172,6 @@ public class RegistrationController {
if (passToken == null || user.getId() != id) {
final String message = messages.getMessage("auth.message.invalidToken", null, locale);
model.addAttribute("message", message);
System.out.println(id);
System.out.println(passToken);
return "redirect:/login.html?lang=" + locale.getLanguage();
}
@ -214,6 +211,16 @@ public class RegistrationController {
return email;
}
private final SimpleMailMessage constructResetTokenEmail(final String contextPath, final Locale locale, final String token, final User user) {
final String url = contextPath + "/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());
email.setSubject("Reset Password");
email.setText(message + " \r\n" + "http://localhost:8080" + url);
return email;
}
private User createUserAccount(final UserDto accountDto) {
User registered = null;
try {
@ -223,5 +230,4 @@ public class RegistrationController {
}
return registered;
}
}

View File

@ -57,4 +57,5 @@ label.form.resendRegistrationToken=Re-send Token
message.resendToken=We will send you a message with a new registration token to your email account.
message.forgetPassword=Forget Password
message.resetPassword=Reset Password
message.updatePassword=Update Password
message.updatePassword=Update Password
message.userNotFound=User Not Found

View File

@ -13,27 +13,27 @@
<title><spring:message code="message.resetPassword"></spring:message></title>
</head>
<body>
<div class="container">
<div class="span12">
<H1>
<spring:message code="message.resetPassword"></spring:message>
</H1>
<form:form action="user/resetPassword" method="POST" enctype="utf8">
<br>
<tr>
<td><label><spring:message code="label.user.email"></spring:message></label></td>
<td><input name="email" type="email" value="" /></td>
</tr>
<button type="submit">
<spring:message code="message.resetPassword"></spring:message>
</button>
</form:form>
<br> <a href="<c:url value="registration.html" />"><spring:message
code="label.form.loginSignUp"></spring:message></a>
</div>
</div>
<div class="container">
<div class="span12">
<h1>
<spring:message code="message.resetPassword"></spring:message>
</h1>
<form:form action="user/resetPassword" method="POST" enctype="utf8">
<br>
<tr>
<td><label><spring:message code="label.user.email"></spring:message></label></td>
<td><input name="email" type="email" value="" /></td>
</tr>
<button type="submit">
<spring:message code="message.resetPassword"></spring:message>
</button>
</form:form>
<br> <a href="<c:url value="registration.html" />"><spring:message
code="label.form.loginSignUp"></spring:message></a>
</div>
</div>
</body>
</html>

View File

@ -25,8 +25,6 @@
<div class="alert alert-error">
<!-- <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}"/> -->
<spring:message code="message.badCredentials"></spring:message>
<a href="<c:url value="/forgetPassword.html" />"><spring:message
code="message.forgetPassword"></spring:message></a>
</div>
</c:otherwise>
</c:choose>
@ -89,6 +87,9 @@ code="message.forgetPassword"></spring:message></a>
<br> Current Locale : ${pageContext.response.locale} <br> <a
href="<c:url value="/user/registration" />"><spring:message
code="label.form.loginSignUp"></spring:message></a>
<br><br><br>
<a href="<c:url value="/forgetPassword.html" />"><spring:message
code="message.resetPassword"></spring:message></a>
</div>
</div>
</body>