package de.champonthis.buntspecht.security.handler;

import java.io.IOException;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class FormAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {

    private String failureUrl;

    public FormAuthenticationFailureHandler(String failureUrl) {
        super(failureUrl);
        this.failureUrl = failureUrl;
    }

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
            AuthenticationException exception) throws IOException, ServletException {
        setDefaultFailureUrl(failureUrl + "?error&username=" + request.getParameter("username"));
        super.onAuthenticationFailure(request, response, exception);
        setDefaultFailureUrl(failureUrl);
    }
}
