{fbmip:fixed type="top" id="mipfixed" dataSlide="header-fixed-slide" class="fb-header-fixed"}
{fbview:mainmenu menuItemCode='$menuItemCode'/}
{/fbmip:fixed}
{fbmip:img fit='cover' src="$banner2['img_url']" alt="$banner2['title']"/}

Spring Security Registration - 重新发送验证电子邮件

  • 发布时间:
  • 浏览:153
  • 来源:平步科技官网

1.概述

在本教程中 - 我们将继续正在进行Spring Security系列注册,并查看在用户有机会激活其帐户之前,如果用户已过期,则会重新发送验证链接。

2.重新发送验证链接

首先,让我们看看当用户请求另一个验证链接时,如果前一个验证链接已过期,会发生什么。

首先 - 我们将使用新的expireDate重置现有令牌。我们将向用户发送一封新的电子邮件,其中包含新的链接/令牌:

@RequestMapping(value = "/user/resendRegistrationToken", method = RequestMethod.GET)

@ResponseBody

public GenericResponse resendRegistrationToken(

  HttpServletRequest request, @RequestParam("token") String existingToken) {

    VerificationToken newToken = userService.generateNewVerificationToken(existingToken);

     

    User user = userService.getUser(newToken.getToken());

    String appUrl = 

      "http://" + request.getServerName() + 

      ":" + request.getServerPort() + 

      request.getContextPath();

    SimpleMailMessage email = 

      constructResendVerificationTokenEmail(appUrl, request.getLocale(), newToken, user);

    mailSender.send(email);

 

    return new GenericResponse(

      messages.getMessage("message.resendToken", null, request.getLocale()));

}

用于实际构建用户获取的电子邮件消息的实用程序 - constructResendVerificationTokenEmail():

private SimpleMailMessage constructResendVerificationTokenEmail

  (String contextPath, Locale locale, VerificationToken newToken, User user) {

    String confirmationUrl = 

      contextPath + "/regitrationConfirm.html?token=" + newToken.getToken();

    String message = messages.getMessage("message.resendToken", null, locale);

    SimpleMailMessage email = new SimpleMailMessage();

    email.setSubject("Resend Registration Token");

    email.setText(message + " rn" + confirmationUrl);

    email.setFrom(env.getProperty("support.email"));

    email.setTo(user.getEmail());

    return email;

}

我们还需要修改现有的注册功能 - 在模型上添加一些关于令牌到期的新信息:

@RequestMapping(value = "/regitrationConfirm", method = RequestMethod.GET)

public String confirmRegistration(

  Locale locale, Model model, @RequestParam("token") String token) {

    VerificationToken verificationToken = userService.getVerificationToken(token);

    if (verificationToken == null) {

        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, locale));

        model.addAttribute("expired", true);

        model.addAttribute("token", token);

        return "redirect:/badUser.html?lang=" + locale.getLanguage();

    }

 

    user.setEnabled(true);

    userService.saveRegisteredUser(user);

    model.addAttribute("message", messages.getMessage("message.accountVerified", null, locale));

    return "redirect:/login.html?lang=" + locale.getLanguage();

}