Set redirection to custom fault tolerance handler using Spring

What is the correct way to set a redirect to a custom AuthenticationFailureHandler in Spring?

Is it possible to call the controller?

The code is as follows:

@Component
public class MyAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request,
            HttpServletResponse response, AuthenticationException exception)
            throws IOException, ServletException {
        super.onAuthenticationFailure(request, response, exception);

        if (exception.getClass().isAssignableFrom(
                CustomUsernameNotFoundException.class)) {

            // TODO Set the redirect

        }
    }

}
+4
source share
4 answers

You call super.onAuthenticationFailurewhich will display the redirect to the configured URL. So the answer has already been made, and you cannot decide to redirect somewhere else.

You can configure it SimpleUrlAuthenticationFailureHandlerto redirect to a single URL and only call the super method if you are not going to redirect yourself.

, AuthenticationFailureHandler , , - , :

if (oneCondition) {
    // redirect to IdP

} else {
    // redirect to registration page
}
+2

-

public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        saveException(request, exception);
        //do your things
        getRedirectStrategy().sendRedirect(request, response, "/page/login?error=Retry");
}
+3

., , , .

Spring

@RequestMapping(value = "/login/failure")
public String loginFailure() {
    String message = "Login Failure!";
    return "redirect:/login?message="+message;
}

, , , xml

Spring Mapping.xml

0

URL.

response.sendRedirect("/redirect");
-1

Source: https://habr.com/ru/post/1538418/


All Articles