All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.iintelligas.config.CustomAuthFailureHandler Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
package com.github.iintelligas.config;

import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Help link https://stackoverflow.com/questions/43838639/spring-security-custom-authentication-failure-handler-redirect-with-parameter
 */
@Component
public class CustomAuthFailureHandler extends SimpleUrlAuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
                                        AuthenticationException exception) throws IOException, ServletException {
        if(exception instanceof BadCredentialsException)
        {
            getRedirectStrategy().sendRedirect(request, response, "/login?msg=1");
        }
        else if(exception instanceof InternalAuthenticationServiceException)
        {
            getRedirectStrategy().sendRedirect(request, response, "/login?msg=2");
        }
        else
        {
            getRedirectStrategy().sendRedirect(request, response, "/login?msg=3");
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy