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; package org.baeldung.web.controller;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Locale; import java.util.Locale;
import java.util.UUID; import java.util.UUID;
@ -20,6 +18,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
import org.springframework.mail.MailAuthenticationException;
import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
@ -85,9 +84,6 @@ public class RegistrationController {
final User user = verificationToken.getUser(); final User user = verificationToken.getUser();
final Calendar cal = Calendar.getInstance(); 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) { if ((verificationToken.getExpiryDate().getTime() - cal.getTime().getTime()) <= 0) {
model.addAttribute("message", messages.getMessage("auth.message.expired", null, locale)); model.addAttribute("message", messages.getMessage("auth.message.expired", null, locale));
@ -129,9 +125,13 @@ public class RegistrationController {
try { try {
final SimpleMailMessage email = constructResetVerificationTokenEmail(request.getContextPath(), request.getLocale(), newToken, user); final SimpleMailMessage email = constructResetVerificationTokenEmail(request.getContextPath(), request.getLocale(), newToken, user);
mailSender.send(email); mailSender.send(email);
} catch (final Exception e) { } catch (final MailAuthenticationException e) {
// MailException LOGGER.debug("MailAuthenticationException");
return "redirect:/emailError.html?lang=" + locale.getLanguage(); 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)); model.addAttribute("message", messages.getMessage("message.resendToken", null, locale));
return "redirect:/login.html?lang=" + locale.getLanguage(); 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) { public String resetPassword(final WebRequest request, final Model model, @RequestParam("email") final String userEmail) {
final User user = userService.findUserByEmail(userEmail); final User user = userService.findUserByEmail(userEmail);
if (user == null) { 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(); return "redirect:/login.html?lang=" + request.getLocale().getLanguage();
} }
final String token = UUID.randomUUID().toString(); final String token = UUID.randomUUID().toString();
userService.createPasswordResetTokenForUser(user, token); userService.createPasswordResetTokenForUser(user, token);
try { try {
final String url = request.getContextPath() + "/user/changePassword?id=" + user.getId() + "&token=" + token; final SimpleMailMessage email = constructResetTokenEmail(request.getContextPath(), request.getLocale(), token, user);
final String message = messages.getMessage("message.resetPassword", null, request.getLocale()); LOGGER.debug(email.getText());
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());
mailSender.send(email); mailSender.send(email);
System.out.println(email.getText()); } catch (final MailAuthenticationException e) {
} catch (final Exception e) { LOGGER.debug("MailAuthenticationException");
return "redirect:/emailError.html?lang=" + request.getLocale().getLanguage(); 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())); model.addAttribute("message", messages.getMessage("message.resetPassword", null, request.getLocale()));
return "redirect:/login.html?lang=" + request.getLocale().getLanguage(); return "redirect:/login.html?lang=" + request.getLocale().getLanguage();
@ -173,8 +172,6 @@ public class RegistrationController {
if (passToken == null || user.getId() != id) { if (passToken == null || user.getId() != id) {
final String message = messages.getMessage("auth.message.invalidToken", null, locale); final String message = messages.getMessage("auth.message.invalidToken", null, locale);
model.addAttribute("message", message); model.addAttribute("message", message);
System.out.println(id);
System.out.println(passToken);
return "redirect:/login.html?lang=" + locale.getLanguage(); return "redirect:/login.html?lang=" + locale.getLanguage();
} }
@ -214,6 +211,16 @@ public class RegistrationController {
return email; 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) { private User createUserAccount(final UserDto accountDto) {
User registered = null; User registered = null;
try { try {
@ -223,5 +230,4 @@ public class RegistrationController {
} }
return registered; 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.resendToken=We will send you a message with a new registration token to your email account.
message.forgetPassword=Forget Password message.forgetPassword=Forget Password
message.resetPassword=Reset 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> <title><spring:message code="message.resetPassword"></spring:message></title>
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<div class="span12"> <div class="span12">
<H1> <h1>
<spring:message code="message.resetPassword"></spring:message> <spring:message code="message.resetPassword"></spring:message>
</H1> </h1>
<form:form action="user/resetPassword" method="POST" enctype="utf8"> <form:form action="user/resetPassword" method="POST" enctype="utf8">
<br> <br>
<tr> <tr>
<td><label><spring:message code="label.user.email"></spring:message></label></td> <td><label><spring:message code="label.user.email"></spring:message></label></td>
<td><input name="email" type="email" value="" /></td> <td><input name="email" type="email" value="" /></td>
</tr> </tr>
<button type="submit"> <button type="submit">
<spring:message code="message.resetPassword"></spring:message> <spring:message code="message.resetPassword"></spring:message>
</button> </button>
</form:form> </form:form>
<br> <a href="<c:url value="registration.html" />"><spring:message <br> <a href="<c:url value="registration.html" />"><spring:message
code="label.form.loginSignUp"></spring:message></a> code="label.form.loginSignUp"></spring:message></a>
</div> </div>
</div> </div>
</body> </body>
</html> </html>

View File

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