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

com.github.mkopylec.recaptcha.security.login.LoginFailuresCountingHandler Maven / Gradle / Ivy

There is a newer version: 2.3.1
Show newest version
package com.github.mkopylec.recaptcha.security.login;

import com.github.mkopylec.recaptcha.RecaptchaProperties;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;

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

public class LoginFailuresCountingHandler extends SimpleUrlAuthenticationFailureHandler {

    protected final LoginFailuresManager failuresManager;

    public LoginFailuresCountingHandler(LoginFailuresManager failuresManager, RecaptchaProperties recaptcha, RecaptchaAwareRedirectStrategy redirectStrategy) {
        this.failuresManager = failuresManager;
        setDefaultFailureUrl(recaptcha.getSecurity().getFailureUrl());
        setRedirectStrategy(redirectStrategy);
    }

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

    @Override
    public void setRedirectStrategy(RedirectStrategy redirectStrategy) {
        if (!RecaptchaAwareRedirectStrategy.class.isAssignableFrom(redirectStrategy.getClass())) {
            throw new IllegalArgumentException("Invalid redirect strategy. Redirect strategy must be an instance of " + RecaptchaAwareRedirectStrategy.class.getName() + " but is " + redirectStrategy);
        }
        super.setRedirectStrategy(redirectStrategy);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy