diff --git a/src/main/java/stirling/software/SPDF/config/security/CustomAuthenticationSuccessHandler.java b/src/main/java/stirling/software/SPDF/config/security/CustomAuthenticationSuccessHandler.java index 61d5a3ce..314d24fe 100644 --- a/src/main/java/stirling/software/SPDF/config/security/CustomAuthenticationSuccessHandler.java +++ b/src/main/java/stirling/software/SPDF/config/security/CustomAuthenticationSuccessHandler.java @@ -45,7 +45,15 @@ public class CustomAuthenticationSuccessHandler ? (SavedRequest) session.getAttribute("SPRING_SECURITY_SAVED_REQUEST") : null; - if (savedRequest != null + // Check for the stored previous page URL + String previousPageUrl = (session != null) ? (String) session.getAttribute("PREVIOUS_PAGE_URL") : null; + + if (previousPageUrl != null) { + // Redirect to the stored previous page URL + getRedirectStrategy().sendRedirect(request, response, previousPageUrl); + // Remove the stored previous page URL from the session + session.removeAttribute("PREVIOUS_PAGE_URL"); + } else if (savedRequest != null && !RequestUriUtils.isStaticResource( request.getContextPath(), savedRequest.getRedirectUrl())) { // Redirect to the original destination diff --git a/src/main/java/stirling/software/SPDF/controller/web/AccountWebController.java b/src/main/java/stirling/software/SPDF/controller/web/AccountWebController.java index 2f6b5042..93f02bc2 100644 --- a/src/main/java/stirling/software/SPDF/controller/web/AccountWebController.java +++ b/src/main/java/stirling/software/SPDF/controller/web/AccountWebController.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpSession; import lombok.extern.slf4j.Slf4j; import stirling.software.SPDF.config.security.session.SessionPersistentRegistry; import stirling.software.SPDF.model.*; @@ -49,6 +50,13 @@ public class AccountWebController { return "redirect:/"; } + // Store the previous page URL in the session before redirecting to the login page + HttpSession session = request.getSession(); + String referer = request.getHeader("Referer"); + if (referer != null && !referer.contains("/login")) { + session.setAttribute("PREVIOUS_PAGE_URL", referer); + } + Map providerList = new HashMap<>(); OAUTH2 oauth = applicationProperties.getSecurity().getOAUTH2();