modify reset password
This commit is contained in:
parent
899a319e0e
commit
54bf8b0e1c
|
@ -1,6 +1,5 @@
|
|||
package org.baeldung.persistence.model;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
|
@ -74,7 +73,7 @@ public class PasswordResetToken {
|
|||
|
||||
private Date calculateExpiryDate(int expiryTimeInMinutes) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(new Timestamp(cal.getTime().getTime()));
|
||||
cal.setTimeInMillis(new Date().getTime());
|
||||
cal.add(Calendar.MINUTE, expiryTimeInMinutes);
|
||||
return new Date(cal.getTime().getTime());
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ import javax.persistence.OneToOne;
|
|||
@Entity
|
||||
public class VerificationToken {
|
||||
|
||||
private static final int EXPIRATION = 2;// 60 * 24;
|
||||
private static final int EXPIRATION = 60 * 24;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
|
|
|
@ -19,7 +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.context.NoSuchMessageException;
|
||||
import org.springframework.mail.MailAuthenticationException;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
|
@ -39,6 +39,9 @@ import org.springframework.web.bind.annotation.RequestParam;
|
|||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@Controller
|
||||
public class RegistrationController {
|
||||
private final Logger LOGGER = LoggerFactory.getLogger(getClass());
|
||||
|
@ -140,15 +143,14 @@ public class RegistrationController {
|
|||
return "redirect:/login.html?lang=" + locale.getLanguage();
|
||||
}
|
||||
|
||||
@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) {
|
||||
@RequestMapping(value = "/user/resendRegistrationToken2", method = RequestMethod.GET)
|
||||
public @ResponseBody String resendRegistrationToken2(final HttpServletRequest request, final Model model, @RequestParam("token") final String existingToken) throws JsonProcessingException, NoSuchMessageException {
|
||||
final VerificationToken newToken = userService.generateNewVerificationToken(existingToken);
|
||||
final User user = userService.getUser(newToken.getToken());
|
||||
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());
|
||||
return new ObjectMapper().writeValueAsString(messages.getMessage("message.resendToken", null, request.getLocale()));
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/user/resetPassword", method = RequestMethod.POST)
|
||||
|
@ -177,6 +179,22 @@ public class RegistrationController {
|
|||
return "redirect:/login.html?lang=" + request.getLocale().getLanguage();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/user/resetPassword2", method = RequestMethod.POST)
|
||||
public @ResponseBody String resetPassword2(final HttpServletRequest request, final Model model, @RequestParam("email") final String userEmail) throws JsonProcessingException, NoSuchMessageException {
|
||||
final User user = userService.findUserByEmail(userEmail);
|
||||
if (user == null) {
|
||||
return new ObjectMapper().writeValueAsString(messages.getMessage("message.userNotFound", null, request.getLocale()));
|
||||
}
|
||||
|
||||
final String token = UUID.randomUUID().toString();
|
||||
userService.createPasswordResetTokenForUser(user, token);
|
||||
final String appUrl = request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
|
||||
final SimpleMailMessage email = constructResetTokenEmail(appUrl, request.getLocale(), token, user);
|
||||
mailSender.send(email);
|
||||
|
||||
return new ObjectMapper().writeValueAsString(messages.getMessage("message.resetPassword", 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) {
|
||||
final Locale locale = request.getLocale();
|
||||
|
|
|
@ -23,13 +23,13 @@ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionH
|
|||
@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());
|
||||
return new ObjectMapper().writeValueAsString("MailError");
|
||||
}
|
||||
|
||||
@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());
|
||||
return new ObjectMapper().writeValueAsString("InternalError");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -32,15 +32,15 @@ code="label.form.loginSignUp"></spring:message></a>
|
|||
<script type="text/javascript">
|
||||
function resendToken(){
|
||||
$.get("<c:url value="/user/resendRegistrationToken2"><c:param name="token" value="${param.token}"/></c:url>", function(data){
|
||||
if(data.indexOf("MailAuthenticationException") > -1)
|
||||
if(data.indexOf("MailError") > -1)
|
||||
{
|
||||
window.location.href = "<c:url value="/emailError.html"></c:url>";
|
||||
}
|
||||
else if(data.indexOf("Exception") > -1){
|
||||
window.location.href = "<c:url value="/login"><c:param name="message" value="Error"/></c:url>";
|
||||
else if(data.indexOf("InternalError") > -1){
|
||||
window.location.href = "<c:url value="/login.html"><c:param name="message" value="Error Occurred"/></c:url>";
|
||||
}
|
||||
else{
|
||||
window.location.href = "<c:url value="/login"></c:url>" + "message=" + data;
|
||||
window.location.href = "<c:url value="/login.html"></c:url>" + "?message=" + data;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -18,22 +18,41 @@
|
|||
<h1>
|
||||
<spring:message code="message.resetPassword"></spring:message>
|
||||
</h1>
|
||||
<form:form action="user/resetPassword" method="POST" enctype="utf8">
|
||||
<div>
|
||||
<br>
|
||||
|
||||
<tr>
|
||||
<td><label><spring:message code="label.user.email"></spring:message></label></td>
|
||||
<td><input name="email" type="email" value="" /></td>
|
||||
<td><input id="email" name="email" type="email" value="" /></td>
|
||||
</tr>
|
||||
|
||||
<button type="submit">
|
||||
<button type="submit" onclick="resetPass()">
|
||||
<spring:message code="message.resetPassword"></spring:message>
|
||||
</button>
|
||||
</form:form>
|
||||
</div>
|
||||
<br> <a href="<c:url value="registration.html" />"><spring:message
|
||||
code="label.form.loginSignUp"></spring:message></a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
|
||||
<script type="text/javascript">
|
||||
function resetPass(){
|
||||
var email = $("#email").val();
|
||||
$.post("<c:url value="/user/resetPassword2"></c:url>",{email: email} ,function(data){
|
||||
if(data.indexOf("MailError") > -1)
|
||||
{
|
||||
window.location.href = "<c:url value="/emailError.html"></c:url>";
|
||||
}
|
||||
else if(data.indexOf("InternalError") > -1){
|
||||
window.location.href = "<c:url value="/login.html"><c:param name="message" value="Error Occurred"/></c:url>";
|
||||
}
|
||||
else{
|
||||
window.location.href = "<c:url value="/login.html"></c:url>" + "?message=" + data;
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -57,7 +57,7 @@
|
|||
</head>
|
||||
<body>
|
||||
<c:if test="${param.message != null}">
|
||||
<div class="alert alert-error">
|
||||
<div class="alert alert-info">
|
||||
${param.message}
|
||||
</div>
|
||||
</c:if>
|
||||
|
|
Loading…
Reference in New Issue