minor registration cleanup
This commit is contained in:
parent
14849eb88f
commit
9c2395e61f
|
@ -2,15 +2,12 @@ package org.baeldung.event.listener;
|
|||
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.mail.AuthenticationFailedException;
|
||||
|
||||
import org.baeldung.event.OnRegistrationCompleteEvent;
|
||||
import org.baeldung.persistence.model.User;
|
||||
import org.baeldung.persistence.service.IUserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.mail.MailAuthenticationException;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
@ -34,7 +31,8 @@ public class RegistrationListener implements ApplicationListener<OnRegistrationC
|
|||
private void confirmRegistration(OnRegistrationCompleteEvent event) {
|
||||
User user = event.getUser();
|
||||
String token = UUID.randomUUID().toString();
|
||||
service.addVerificationToken(user, token);
|
||||
service.createVerificationTokenForUser(user, token);
|
||||
|
||||
String recipientAddress = user.getEmail();
|
||||
String subject = "Registration Confirmation";
|
||||
String confirmationUrl = event.getAppUrl() + "/regitrationConfirm.html?token=" + token;
|
||||
|
|
|
@ -1,39 +1,30 @@
|
|||
package org.baeldung.persistence.model;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "firstName")
|
||||
private String firstName;
|
||||
|
||||
@Column(name = "lastName")
|
||||
private String lastName;
|
||||
|
||||
@Column(name = "email")
|
||||
private String email;
|
||||
|
||||
@Column(name = "password")
|
||||
private String password;
|
||||
|
||||
@Column(name = "enabled")
|
||||
private boolean enabled;
|
||||
|
||||
@Column(name = "token_expired")
|
||||
private boolean tokenExpired;
|
||||
|
||||
@OneToOne(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
|
||||
|
@ -44,6 +35,9 @@ public class User {
|
|||
this.enabled = false;
|
||||
this.tokenExpired = false;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
@ -108,6 +102,7 @@ public class User {
|
|||
this.tokenExpired = expired;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
@ -137,4 +132,5 @@ public class User {
|
|||
builder.append("User [firstName=").append(firstName).append("]").append("[lastName=").append(lastName).append("]").append("[username").append(email).append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -3,7 +3,7 @@ package org.baeldung.persistence.model;
|
|||
import java.util.Calendar;
|
||||
import java.sql.Date;
|
||||
import java.sql.Timestamp;
|
||||
import javax.persistence.Column;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
|
@ -11,10 +11,8 @@ import javax.persistence.GenerationType;
|
|||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table
|
||||
public class VerificationToken {
|
||||
|
||||
private static final int EXPIRATION = 60 * 24;
|
||||
|
@ -23,14 +21,12 @@ public class VerificationToken {
|
|||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "token")
|
||||
private String token;
|
||||
|
||||
@OneToOne(targetEntity = User.class, fetch = FetchType.EAGER)
|
||||
@JoinColumn(nullable = false, name = "user_id")
|
||||
private User user;
|
||||
|
||||
@Column(name = "expiry_date")
|
||||
private Date expiryDate;
|
||||
|
||||
public VerificationToken() {
|
||||
|
@ -39,17 +35,21 @@ public class VerificationToken {
|
|||
|
||||
public VerificationToken(String token) {
|
||||
super();
|
||||
|
||||
this.token = token;
|
||||
this.expiryDate = calculateExpiryDate(EXPIRATION);
|
||||
}
|
||||
|
||||
public VerificationToken(String token, User user) {
|
||||
super();
|
||||
|
||||
this.token = token;
|
||||
this.user = user;
|
||||
this.expiryDate = calculateExpiryDate(EXPIRATION);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
|
@ -81,16 +81,41 @@ public class VerificationToken {
|
|||
return new Date(cal.getTime().getTime());
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((expiryDate == null) ? 0 : expiryDate.hashCode());
|
||||
result = prime * result + ((token == null) ? 0 : token.hashCode());
|
||||
result = prime * result + ((user == null) ? 0 : user.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
final VerificationToken verificationToken = (VerificationToken) obj;
|
||||
if (!token.equals(verificationToken.getToken()))
|
||||
VerificationToken other = (VerificationToken) obj;
|
||||
if (expiryDate == null) {
|
||||
if (other.expiryDate != null)
|
||||
return false;
|
||||
} else if (!expiryDate.equals(other.expiryDate))
|
||||
return false;
|
||||
if (token == null) {
|
||||
if (other.token != null)
|
||||
return false;
|
||||
} else if (!token.equals(other.token))
|
||||
return false;
|
||||
if (user == null) {
|
||||
if (other.user != null)
|
||||
return false;
|
||||
} else if (!user.equals(other.user))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -6,15 +6,16 @@ import org.baeldung.validation.service.EmailExistsException;
|
|||
|
||||
public interface IUserService {
|
||||
|
||||
public User registerNewUserAccount(UserDto accountDto) throws EmailExistsException;
|
||||
User registerNewUserAccount(UserDto accountDto) throws EmailExistsException;
|
||||
|
||||
public User getUser(String verificationToken);
|
||||
User getUser(String verificationToken);
|
||||
|
||||
public void saveRegisteredUser(User user);
|
||||
void saveRegisteredUser(User user);
|
||||
|
||||
public void addVerificationToken(User user, String token);
|
||||
void deleteUser(User user);
|
||||
|
||||
public VerificationToken getVerificationToken(String VerificationToken);
|
||||
void createVerificationTokenForUser(User user, String token);
|
||||
|
||||
VerificationToken getVerificationToken(String VerificationToken);
|
||||
|
||||
public void deleteUser(User user);
|
||||
}
|
||||
|
|
|
@ -67,8 +67,9 @@ public class UserService implements IUserService {
|
|||
|
||||
@Transactional
|
||||
@Override
|
||||
public void addVerificationToken(User user, String token) {
|
||||
public void createVerificationTokenForUser(User user, String token) {
|
||||
VerificationToken myToken = new VerificationToken(token, user);
|
||||
tokenRepository.save(myToken);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package org.baeldung.web.controller;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.baeldung.persistence.model.User;
|
||||
import org.baeldung.persistence.model.VerificationToken;
|
||||
import org.baeldung.persistence.service.UserDto;
|
||||
|
@ -56,35 +59,40 @@ public class RegistrationController {
|
|||
|
||||
@RequestMapping(value = "/regitrationConfirm", method = RequestMethod.GET)
|
||||
public String confirmRegistration(WebRequest request, Model model, @RequestParam("token") String token) {
|
||||
Locale locale = request.getLocale();
|
||||
|
||||
VerificationToken verificationToken = service.getVerificationToken(token);
|
||||
if (verificationToken == null) {
|
||||
model.addAttribute("message", messages.getMessage("auth.message.invalidToken", null, request.getLocale()));
|
||||
return "redirect:/badUser.html?lang=" + request.getLocale().getLanguage();
|
||||
String message = messages.getMessage("auth.message.invalidToken", null, locale);
|
||||
model.addAttribute("message", message);
|
||||
return "redirect:/badUser.html?lang=" + locale.getLanguage();
|
||||
}
|
||||
|
||||
User user = verificationToken.getUser();
|
||||
Calendar cal = Calendar.getInstance();
|
||||
if ((verificationToken.getExpiryDate().getTime() - cal.getTime().getTime()) <= 0) {
|
||||
model.addAttribute("message", messages.getMessage("auth.message.expired", null, request.getLocale()));
|
||||
return "redirect:/badUser.html?lang=" + request.getLocale().getLanguage();
|
||||
model.addAttribute("message", messages.getMessage("auth.message.expired", null, locale));
|
||||
return "redirect:/badUser.html?lang=" + locale.getLanguage();
|
||||
}
|
||||
|
||||
user.setEnabled(true);
|
||||
service.saveRegisteredUser(user);
|
||||
return "redirect:/login.html?lang=" + request.getLocale().getLanguage();
|
||||
return "redirect:/login.html?lang=" + locale.getLanguage();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/user/registration", method = RequestMethod.POST)
|
||||
public ModelAndView registerUserAccount(@ModelAttribute("user") @Valid UserDto accountDto, BindingResult result, WebRequest request, Errors errors) {
|
||||
LOGGER.debug("Registering user account with information: {}", accountDto);
|
||||
User registered = new User();
|
||||
String appUrl = request.getContextPath();
|
||||
if (result.hasErrors()) {
|
||||
return new ModelAndView("registration", "user", accountDto);
|
||||
}
|
||||
registered = createUserAccount(accountDto);
|
||||
|
||||
User registered = createUserAccount(accountDto);
|
||||
if (registered == null) {
|
||||
result.rejectValue("email", "message.regError");
|
||||
}
|
||||
try {
|
||||
String appUrl = request.getContextPath();
|
||||
eventPublisher.publishEvent(new OnRegistrationCompleteEvent(registered, request.getLocale(), appUrl));
|
||||
} catch (Exception me) {
|
||||
return new ModelAndView("emailError", "user", accountDto);
|
||||
|
|
Loading…
Reference in New Issue