{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 - 重新发送验证电子邮件(2)

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

书接上文,继续重新发送验证电子邮件的解释。

3.异常处理程序

在某些条件下,先前的功能是抛出异常; 需要处理这些异常,我们将使用自定义异常处理程序执行此操作:

@ControllerAdvice

public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

 

    @Autowired

    private MessageSource messages;

 

    @ExceptionHandler({ UserNotFoundException.class })

    public ResponseEntity<Object> handleUserNotFound(RuntimeException ex, WebRequest request) {

        logger.error("404 Status Code", ex);

        GenericResponse bodyOfResponse = new GenericResponse(

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

         

        return handleExceptionInternal(

          ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);

    }

 

    @ExceptionHandler({ MailAuthenticationException.class })

    public ResponseEntity<Object> handleMail(RuntimeException ex, WebRequest request) {

        logger.error("500 Status Code", ex);

        GenericResponse bodyOfResponse = new GenericResponse(

          messages.getMessage(

            "message.email.config.error", null, request.getLocale()), "MailError");

         

        return handleExceptionInternal(

          ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);

    }

 

    @ExceptionHandler({ Exception.class })

    public ResponseEntity<Object> handleInternal(RuntimeException ex, WebRequest request) {

        logger.error("500 Status Code", ex);

        GenericResponse bodyOfResponse = new GenericResponse(

          messages.getMessage(

            "message.error", null, request.getLocale()), "InternalError");

         

        return handleExceptionInternal(

          ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);

    }

}

注意:

我们使用@ControllerAdvice批注来处理整个应用程序中的异常

我们使用一个简单的对象GenericResponse来发送响应:

public class GenericResponse {

    private String message;

    private String error;

 

    public GenericResponse(String message) {

        super();

        this.message = message;

    }

 

    public GenericResponse(String message, String error) {

        super();

        this.message = message;

        this.error = error;

    }

}

4.修改badUser.html

我们现在修改badUser.html,方法是让用户只有在其令牌过期时才能获得新的VerificationToken:

<html>

<head>

<title th:text="#{label.badUser.title}">bad user</title>

</head>

<body>

<h1 th:text="${param.message[0]}">error</h1>

<br>

<a th:href="@{/user/registration}" th:text="#{label.form.loginSignUp}">

  signup</a>

 

<div th:if="${param.expired[0]}">

<h1 th:text="#{label.form.resendRegistrationToken}">resend</h1>

<button onclick="resendToken()"

  th:text="#{label.form.resendRegistrationToken}">resend</button>

  

<script src="jquery.min.js"></script>

<script type="text/javascript">

 

var serverContext = [[@{/}]];

 

function resendToken(){

    $.get(serverContext + "user/resendRegistrationToken?token=" + token, 

      function(data){

            window.location.href = 

              serverContext +"login.html?message=" + data.message;

    })

    .fail(function(data) {

        if(data.responseJSON.error.indexOf("MailError") > -1) {

            window.location.href = serverContext + "emailError.html";

        }

        else {

            window.location.href = 

              serverContext + "login.html?message=" + data.responseJSON.message;

        }

    });

}

</script>

</div>

</body>

</html>

请注意,我们在这里使用了一些非常基本的javascript和JQuery来处理“/ user / resendRegistrationToken”的响应,并根据它重定向用户。