2023-12-24 17:12:32 +00:00
|
|
|
package stirling.software.SPDF.config.security;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
|
2023-12-28 13:50:31 +00:00
|
|
|
import org.springframework.security.web.savedrequest.SavedRequest;
|
2023-12-24 17:12:32 +00:00
|
|
|
|
|
|
|
import jakarta.servlet.ServletException;
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
2023-12-28 13:50:31 +00:00
|
|
|
import jakarta.servlet.http.HttpSession;
|
2024-08-16 12:57:37 +02:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2023-12-29 20:48:21 +00:00
|
|
|
import stirling.software.SPDF.utils.RequestUriUtils;
|
2023-12-24 17:12:32 +00:00
|
|
|
|
2024-08-16 12:57:37 +02:00
|
|
|
@Slf4j
|
2023-12-24 17:12:32 +00:00
|
|
|
public class CustomAuthenticationSuccessHandler
|
|
|
|
extends SavedRequestAwareAuthenticationSuccessHandler {
|
2024-05-18 23:47:05 +02:00
|
|
|
|
|
|
|
private LoginAttemptService loginAttemptService;
|
2024-08-16 12:57:37 +02:00
|
|
|
private UserService userService;
|
2024-05-18 23:47:05 +02:00
|
|
|
|
2024-08-16 12:57:37 +02:00
|
|
|
public CustomAuthenticationSuccessHandler(
|
|
|
|
LoginAttemptService loginAttemptService, UserService userService) {
|
2024-05-18 23:47:05 +02:00
|
|
|
this.loginAttemptService = loginAttemptService;
|
2024-08-16 12:57:37 +02:00
|
|
|
this.userService = userService;
|
2024-05-18 23:47:05 +02:00
|
|
|
}
|
2023-12-24 17:12:32 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAuthenticationSuccess(
|
|
|
|
HttpServletRequest request, HttpServletResponse response, Authentication authentication)
|
|
|
|
throws ServletException, IOException {
|
2024-05-18 23:47:05 +02:00
|
|
|
|
|
|
|
String userName = request.getParameter("username");
|
2024-08-16 12:57:37 +02:00
|
|
|
if (userService.isUserDisabled(userName)) {
|
|
|
|
getRedirectStrategy().sendRedirect(request, response, "/logout?userIsDisabled=true");
|
|
|
|
return;
|
|
|
|
}
|
2024-05-18 23:47:05 +02:00
|
|
|
loginAttemptService.loginSucceeded(userName);
|
|
|
|
|
2023-12-28 13:50:31 +00:00
|
|
|
// Get the saved request
|
|
|
|
HttpSession session = request.getSession(false);
|
|
|
|
SavedRequest savedRequest =
|
2024-05-18 23:47:05 +02:00
|
|
|
(session != null)
|
2023-12-28 13:50:31 +00:00
|
|
|
? (SavedRequest) session.getAttribute("SPRING_SECURITY_SAVED_REQUEST")
|
|
|
|
: null;
|
2024-05-18 23:47:05 +02:00
|
|
|
|
2023-12-29 20:48:21 +00:00
|
|
|
if (savedRequest != null
|
2024-06-12 21:36:18 +02:00
|
|
|
&& !RequestUriUtils.isStaticResource(
|
|
|
|
request.getContextPath(), savedRequest.getRedirectUrl())) {
|
2023-12-28 13:50:31 +00:00
|
|
|
// Redirect to the original destination
|
|
|
|
super.onAuthenticationSuccess(request, response, authentication);
|
|
|
|
} else {
|
|
|
|
// Redirect to the root URL (considering context path)
|
|
|
|
getRedirectStrategy().sendRedirect(request, response, "/");
|
|
|
|
}
|
2023-12-29 20:48:21 +00:00
|
|
|
|
2023-12-28 13:50:31 +00:00
|
|
|
// super.onAuthenticationSuccess(request, response, authentication);
|
2023-12-30 19:11:27 +00:00
|
|
|
}
|
2023-12-24 17:12:32 +00:00
|
|
|
}
|