Merge pull request #137 from Doha2012/master
modify spring security registration
This commit is contained in:
commit
92d28cdb86
|
@ -8,11 +8,14 @@ import org.baeldung.registration.OnRegistrationCompleteEvent;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@PropertySource("classpath:email.properties")
|
||||
public class RegistrationListener implements ApplicationListener<OnRegistrationCompleteEvent> {
|
||||
@Autowired
|
||||
private IUserService service;
|
||||
|
@ -23,6 +26,9 @@ public class RegistrationListener implements ApplicationListener<OnRegistrationC
|
|||
@Autowired
|
||||
private JavaMailSender mailSender;
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
|
@ -44,13 +50,13 @@ public class RegistrationListener implements ApplicationListener<OnRegistrationC
|
|||
private final SimpleMailMessage constructEmailMessage(final OnRegistrationCompleteEvent event, final User user, final String token) {
|
||||
final String recipientAddress = user.getEmail();
|
||||
final String subject = "Registration Confirmation";
|
||||
final String confirmationUrl = "http://" + event.getAppUrl() + "/regitrationConfirm.html?token=" + token;
|
||||
final String confirmationUrl = event.getAppUrl() + "/regitrationConfirm.html?token=" + token;
|
||||
final String message = messages.getMessage("message.regSucc", null, event.getLocale());
|
||||
final SimpleMailMessage email = new SimpleMailMessage();
|
||||
email.setTo(recipientAddress);
|
||||
email.setSubject(subject);
|
||||
email.setText(message + " \r\n" + confirmationUrl);
|
||||
email.setFrom("eugen@baeldung.com");
|
||||
email.setFrom(env.getProperty("support.email"));
|
||||
return email;
|
||||
}
|
||||
|
||||
|
|
|
@ -20,6 +20,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.NoSuchMessageException;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.mail.MailAuthenticationException;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
|
@ -43,6 +45,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
|||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@Controller
|
||||
@PropertySource("classpath:email.properties")
|
||||
public class RegistrationController {
|
||||
private final Logger LOGGER = LoggerFactory.getLogger(getClass());
|
||||
|
||||
|
@ -61,6 +64,9 @@ public class RegistrationController {
|
|||
@Autowired
|
||||
private UserDetailsService userDetailsService;
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
public RegistrationController() {
|
||||
|
||||
}
|
||||
|
@ -97,6 +103,7 @@ public class RegistrationController {
|
|||
|
||||
user.setEnabled(true);
|
||||
userService.saveRegisteredUser(user);
|
||||
model.addAttribute("message", messages.getMessage("message.accountVerified", null, locale));
|
||||
return "redirect:/login.html?lang=" + locale.getLanguage();
|
||||
}
|
||||
|
||||
|
@ -113,7 +120,7 @@ public class RegistrationController {
|
|||
return new ModelAndView("registration", "user", accountDto);
|
||||
}
|
||||
try {
|
||||
final String appUrl = request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
|
||||
final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
|
||||
eventPublisher.publishEvent(new OnRegistrationCompleteEvent(registered, request.getLocale(), appUrl));
|
||||
} catch (final Exception ex) {
|
||||
LOGGER.warn("Unable to register user", ex);
|
||||
|
@ -128,7 +135,7 @@ public class RegistrationController {
|
|||
final VerificationToken newToken = userService.generateNewVerificationToken(existingToken);
|
||||
final User user = userService.getUser(newToken.getToken());
|
||||
try {
|
||||
final String appUrl = request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
|
||||
final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
|
||||
final SimpleMailMessage email = constructResetVerificationTokenEmail(appUrl, request.getLocale(), newToken, user);
|
||||
mailSender.send(email);
|
||||
} catch (final MailAuthenticationException e) {
|
||||
|
@ -147,7 +154,7 @@ public class RegistrationController {
|
|||
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 String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
|
||||
final SimpleMailMessage email = constructResetVerificationTokenEmail(appUrl, request.getLocale(), newToken, user);
|
||||
mailSender.send(email);
|
||||
return new ObjectMapper().writeValueAsString(messages.getMessage("message.resendToken", null, request.getLocale()));
|
||||
|
@ -164,7 +171,7 @@ public class RegistrationController {
|
|||
final String token = UUID.randomUUID().toString();
|
||||
userService.createPasswordResetTokenForUser(user, token);
|
||||
try {
|
||||
final String appUrl = request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
|
||||
final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
|
||||
final SimpleMailMessage email = constructResetTokenEmail(appUrl, request.getLocale(), token, user);
|
||||
mailSender.send(email);
|
||||
} catch (final MailAuthenticationException e) {
|
||||
|
@ -175,7 +182,7 @@ public class RegistrationController {
|
|||
model.addAttribute("message", 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.resetPasswordEmail", null, request.getLocale()));
|
||||
return "redirect:/login.html?lang=" + request.getLocale().getLanguage();
|
||||
}
|
||||
|
||||
|
@ -188,12 +195,11 @@ public class RegistrationController {
|
|||
|
||||
final String token = UUID.randomUUID().toString();
|
||||
userService.createPasswordResetTokenForUser(user, token);
|
||||
final String appUrl = request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
|
||||
final String appUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
|
||||
final SimpleMailMessage email = constructResetTokenEmail(appUrl, request.getLocale(), token, user);
|
||||
email.setFrom("eugen@baeldung.com");
|
||||
mailSender.send(email);
|
||||
|
||||
return new ObjectMapper().writeValueAsString(messages.getMessage("message.resetPassword", null, request.getLocale()));
|
||||
return new ObjectMapper().writeValueAsString(messages.getMessage("message.resetPasswordEmail", null, request.getLocale()));
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/user/changePassword", method = RequestMethod.GET)
|
||||
|
@ -227,7 +233,7 @@ public class RegistrationController {
|
|||
|
||||
final User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
||||
userService.changeUserPassword(user, password);
|
||||
|
||||
model.addAttribute("message", messages.getMessage("message.resetPasswordSuc", null, locale));
|
||||
return "redirect:/login.html?lang=" + locale;
|
||||
}
|
||||
|
||||
|
@ -240,6 +246,7 @@ public class RegistrationController {
|
|||
email.setSubject("Resend Registration Token");
|
||||
email.setText(message + " \r\n" + confirmationUrl);
|
||||
email.setTo(user.getEmail());
|
||||
email.setFrom(env.getProperty("support.email"));
|
||||
return email;
|
||||
}
|
||||
|
||||
|
@ -250,6 +257,7 @@ public class RegistrationController {
|
|||
email.setTo(user.getEmail());
|
||||
email.setSubject("Reset Password");
|
||||
email.setText(message + " \r\n" + url);
|
||||
email.setFrom(env.getProperty("support.email"));
|
||||
return email;
|
||||
}
|
||||
|
||||
|
@ -262,4 +270,4 @@ public class RegistrationController {
|
|||
}
|
||||
return registered;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,9 +54,13 @@ NotEmpty.user.matchingPassword=Required
|
|||
PasswordMatches.user:Password does not match!
|
||||
Email.user.email=Invalid Username (Email)
|
||||
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 an email with a new registration token to your email account
|
||||
message.forgetPassword=Forget Password
|
||||
message.resetPassword=Reset Password
|
||||
message.updatePassword=Update Password
|
||||
message.userNotFound=User Not Found
|
||||
auth.message.blocked=This ip is blocked for 24 hours
|
||||
auth.message.blocked=This ip is blocked for 24 hours
|
||||
message.accountVerified=Your account verified successfully
|
||||
message.resetPasswordSuc=Password reset successfully
|
||||
message.resetYourPassword=Reset your password
|
||||
message.resetPasswordEmail=We will send an email to reset your password
|
|
@ -54,9 +54,13 @@ NotEmpty.user.matchingPassword=Campo obligatrio
|
|||
PasswordMatches.user:Las claves no coinciden!
|
||||
Email.user.email=Email no es valido
|
||||
label.form.resendRegistrationToken=Reenviar mensaje de emergencia
|
||||
message.resendToken=Le enviaremos un mensaje con un nuevo token de registro en su cuenta de correo electrónico
|
||||
message.resendToken=Te enviaremos un correo electrónico con un nuevo token de registro en su cuenta de correo electrónico
|
||||
message.forgetPassword=Olvide la contraseña
|
||||
message.resetPassword=Restablecer contraseña
|
||||
message.updatePassword=Actualizar contraseña
|
||||
message.userNotFound=Usuario no encontrado
|
||||
auth.message.blocked=Esta IP se bloquea durante 24 horas
|
||||
auth.message.blocked=Esta IP se bloquea durante 24 horas
|
||||
message.accountVerified=Su cuenta verificada con éxito
|
||||
message.resetPasswordSuc=Contraseña reajusta correctamente
|
||||
message.resetYourPassword=Restablecer su contraseña
|
||||
message.resetPasswordEmail=Te enviaremos un correo electrónico para restablecer su contraseña
|
|
@ -30,8 +30,13 @@
|
|||
<spring:message code="message.resetPassword"></spring:message>
|
||||
</button>
|
||||
</div>
|
||||
<br> <a href="<c:url value="registration.html" />"><spring:message
|
||||
code="label.form.loginSignUp"></spring:message></a>
|
||||
|
||||
<br>
|
||||
<a href="<c:url value="registration.html" />"><spring:message code="label.form.loginSignUp"></spring:message></a>
|
||||
|
||||
<br>
|
||||
<a href="<c:url value="login.html" />"><spring:message code="label.form.loginLink"></spring:message></a>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
|
@ -17,16 +17,23 @@
|
|||
<div class="container">
|
||||
<div class="span12">
|
||||
<H1>
|
||||
<spring:message code="message.updatePassword"></spring:message>
|
||||
<spring:message code="message.resetYourPassword"></spring:message>
|
||||
</H1>
|
||||
<form:form action="user/savePassword" method="POST" enctype="utf8">
|
||||
<br>
|
||||
|
||||
<tr>
|
||||
<td><label><spring:message code="label.user.password"></spring:message></label></td>
|
||||
<td><input name="password" type="password" value="" /></td>
|
||||
<td><input id="pass" name="password" type="password" value="" /></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><label><spring:message code="label.user.confirmPass"></spring:message></label></td>
|
||||
<td>
|
||||
<input id="passConfirm" type="password" value="" />
|
||||
<span id="error" class="alert alert-error" style="display:none"><spring:message code="PasswordMatches.user"></spring:message></span>
|
||||
</td>
|
||||
</tr>
|
||||
<br><br>
|
||||
<button type="submit">
|
||||
<spring:message code="message.updatePassword"></spring:message>
|
||||
</button>
|
||||
|
@ -35,7 +42,18 @@
|
|||
</div>
|
||||
</div>
|
||||
</sec:authorize>
|
||||
|
||||
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function() {
|
||||
$('form').on('submit', function(e){
|
||||
var valid = $("#pass").val() == $("#passConfirm").val();
|
||||
if(!valid) {
|
||||
e.preventDefault();
|
||||
$("#error").show();
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in New Issue