2023-12-24 17:12:32 +00:00
|
|
|
package stirling.software.SPDF.config.security;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
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 org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
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;
|
2023-12-24 17:12:32 +00:00
|
|
|
|
|
|
|
@Component
|
|
|
|
public class CustomAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private LoginAttemptService loginAttemptService;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
|
2023-12-28 13:50:31 +00:00
|
|
|
String username = request.getParameter("username");
|
2023-12-24 17:12:32 +00:00
|
|
|
loginAttemptService.loginSucceeded(username);
|
2023-12-28 13:50:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Get the saved request
|
|
|
|
HttpSession session = request.getSession(false);
|
|
|
|
SavedRequest savedRequest = session != null ? (SavedRequest) session.getAttribute("SPRING_SECURITY_SAVED_REQUEST") : null;
|
|
|
|
if (savedRequest != null && !isStaticResource(savedRequest)) {
|
|
|
|
// Redirect to the original destination
|
|
|
|
super.onAuthenticationSuccess(request, response, authentication);
|
|
|
|
} else {
|
|
|
|
// Redirect to the root URL (considering context path)
|
|
|
|
getRedirectStrategy().sendRedirect(request, response, "/");
|
|
|
|
}
|
|
|
|
|
|
|
|
//super.onAuthenticationSuccess(request, response, authentication);
|
2023-12-24 17:12:32 +00:00
|
|
|
}
|
2023-12-28 13:50:31 +00:00
|
|
|
|
|
|
|
private boolean isStaticResource(SavedRequest savedRequest) {
|
|
|
|
String requestURI = savedRequest.getRedirectUrl();
|
|
|
|
return requestURI.startsWith("/css/")
|
|
|
|
|| requestURI.startsWith("/js/")
|
|
|
|
|| requestURI.startsWith("/images/")
|
|
|
|
|| requestURI.startsWith("/public/")
|
|
|
|
|| requestURI.startsWith("/pdfjs/")
|
|
|
|
|| requestURI.endsWith(".svg");
|
|
|
|
}
|
|
|
|
|
2023-12-24 17:12:32 +00:00
|
|
|
}
|