Stirling-PDF/src/main/java/stirling/software/SPDF/config/security/CustomAuthenticationFailureHandler.java

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

50 lines
1.9 KiB
Java
Raw Normal View History

2023-08-13 01:14:30 +01:00
package stirling.software.SPDF.config.security;
import java.io.IOException;
2023-12-24 17:12:32 +00:00
import org.springframework.beans.factory.annotation.Autowired;
2023-08-27 00:39:22 +01:00
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.LockedException;
2023-08-13 01:14:30 +01:00
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
2023-12-24 17:12:32 +00:00
import org.springframework.stereotype.Component;
2023-12-16 19:30:47 +00:00
2023-08-13 01:14:30 +01:00
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
2023-12-30 19:11:27 +00:00
2023-12-24 17:12:32 +00:00
@Component
2023-08-13 01:14:30 +01:00
public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
2023-12-30 19:11:27 +00:00
2023-12-24 17:12:32 +00:00
@Autowired private final LoginAttemptService loginAttemptService;
2023-08-13 01:14:30 +01:00
2023-12-24 17:12:32 +00:00
@Autowired
public CustomAuthenticationFailureHandler(LoginAttemptService loginAttemptService) {
this.loginAttemptService = loginAttemptService;
}
2023-12-30 19:11:27 +00:00
2023-08-13 01:14:30 +01:00
@Override
public void onAuthenticationFailure(
HttpServletRequest request,
HttpServletResponse response,
AuthenticationException exception)
throws IOException, ServletException {
2023-12-16 18:18:00 +00:00
String ip = request.getRemoteAddr();
logger.error("Failed login attempt from IP: " + ip);
2023-12-30 19:11:27 +00:00
2023-12-24 17:12:32 +00:00
String username = request.getParameter("username");
if (loginAttemptService.loginAttemptCheck(username)) {
2023-08-13 01:14:30 +01:00
setDefaultFailureUrl("/login?error=locked");
2023-12-30 19:11:27 +00:00
2023-12-24 17:12:32 +00:00
} else {
if (exception.getClass().isAssignableFrom(BadCredentialsException.class)) {
setDefaultFailureUrl("/login?error=badcredentials");
} else if (exception.getClass().isAssignableFrom(LockedException.class)) {
setDefaultFailureUrl("/login?error=locked");
}
2023-08-13 01:14:30 +01:00
}
2023-12-30 19:11:27 +00:00
2023-08-13 01:14:30 +01:00
super.onAuthenticationFailure(request, response, exception);
}
}