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.

89 lines
3.6 KiB
Java
Raw Normal View History

2024-01-03 17:59:04 +00:00
package stirling.software.SPDF.config.security;
import java.io.IOException;
import java.util.Optional;
2024-01-03 17:59:04 +00:00
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
2024-01-03 17:59:04 +00:00
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
2024-01-03 17:59:04 +00:00
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import stirling.software.SPDF.model.User;
2024-01-03 17:59:04 +00:00
@Slf4j
2024-01-03 17:59:04 +00:00
public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
private LoginAttemptService loginAttemptService;
2024-01-03 17:59:04 +00:00
private UserService userService;
public CustomAuthenticationFailureHandler(
final LoginAttemptService loginAttemptService, UserService userService) {
2024-01-03 17:59:04 +00:00
this.loginAttemptService = loginAttemptService;
this.userService = userService;
2024-01-03 17:59:04 +00:00
}
@Override
public void onAuthenticationFailure(
HttpServletRequest request,
HttpServletResponse response,
AuthenticationException exception)
throws IOException, ServletException {
if (exception instanceof DisabledException) {
log.error("User is deactivated: ", exception);
getRedirectStrategy().sendRedirect(request, response, "/logout?userIsDisabled=true");
return;
}
String ip = request.getRemoteAddr();
log.error("Failed login attempt from IP: {}", ip);
if (exception instanceof LockedException) {
getRedirectStrategy().sendRedirect(request, response, "/login?error=locked");
return;
}
2024-06-02 11:59:43 +01:00
2024-01-03 17:59:04 +00:00
String username = request.getParameter("username");
2024-06-02 11:42:30 +01:00
Optional<User> optUser = userService.findByUsernameIgnoreCase(username);
2024-06-02 11:59:43 +01:00
if (username != null && optUser.isPresent() && !isDemoUser(optUser)) {
log.info(
"Remaining attempts for user {}: {}",
username,
loginAttemptService.getRemainingAttempts(username));
loginAttemptService.loginFailed(username);
if (loginAttemptService.isBlocked(username) || exception instanceof LockedException) {
getRedirectStrategy().sendRedirect(request, response, "/login?error=locked");
return;
2024-01-03 17:59:04 +00:00
}
}
if (exception instanceof BadCredentialsException
|| exception instanceof UsernameNotFoundException) {
getRedirectStrategy().sendRedirect(request, response, "/login?error=badcredentials");
return;
}
if (exception instanceof InternalAuthenticationServiceException
|| "Password must not be null".equalsIgnoreCase(exception.getMessage())) {
getRedirectStrategy()
.sendRedirect(request, response, "/login?error=oauth2AuthenticationError");
return;
}
2024-01-03 17:59:04 +00:00
super.onAuthenticationFailure(request, response, exception);
}
2024-06-02 11:42:30 +01:00
private boolean isDemoUser(Optional<User> user) {
return user.isPresent()
&& user.get().getAuthorities().stream()
.anyMatch(authority -> "ROLE_DEMO_USER".equals(authority.getAuthority()));
}
2024-01-03 17:59:04 +00:00
}