2019-10-31 20:43:47 -05:00
|
|
|
package com.baeldung.security;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
2023-02-07 15:39:57 +00:00
|
|
|
import jakarta.servlet.ServletException;
|
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
2019-10-31 20:43:47 -05:00
|
|
|
|
|
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
|
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
|
|
|
|
|
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
|
|
|
|
|
import org.springframework.security.web.savedrequest.RequestCache;
|
|
|
|
|
import org.springframework.security.web.savedrequest.SavedRequest;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
public class MySavedRequestAwareAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
|
|
|
|
|
|
|
|
|
|
private RequestCache requestCache = new HttpSessionRequestCache();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onAuthenticationSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws ServletException, IOException {
|
|
|
|
|
final SavedRequest savedRequest = requestCache.getRequest(request, response);
|
|
|
|
|
|
|
|
|
|
if (savedRequest == null) {
|
|
|
|
|
super.onAuthenticationSuccess(request, response, authentication);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
final String targetUrlParameter = getTargetUrlParameter();
|
|
|
|
|
if (isAlwaysUseDefaultTargetUrl() || (targetUrlParameter != null && StringUtils.hasText(request.getParameter(targetUrlParameter)))) {
|
|
|
|
|
requestCache.removeRequest(request, response);
|
|
|
|
|
super.onAuthenticationSuccess(request, response, authentication);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clearAuthenticationAttributes(request);
|
|
|
|
|
|
|
|
|
|
// Use the DefaultSavedRequest URL
|
|
|
|
|
// final String targetUrl = savedRequest.getRedirectUrl();
|
|
|
|
|
// logger.debug("Redirecting to DefaultSavedRequest Url: " + targetUrl);
|
|
|
|
|
// getRedirectStrategy().sendRedirect(request, response, targetUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setRequestCache(final RequestCache requestCache) {
|
|
|
|
|
this.requestCache = requestCache;
|
|
|
|
|
}
|
|
|
|
|
}
|