package org.podcastpedia.web.util;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
* Created by matad on 09.09.15.
*/
public class MyCustomLoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
public MyCustomLoginSuccessHandler(String defaultTargetUrl) {
setDefaultTargetUrl(defaultTargetUrl);
}
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
HttpSession session = request.getSession();
session.setMaxInactiveInterval(60*60);//set the session timeout to 60 minutes
if (session != null) {
String redirectUrl = (String) session.getAttribute("url_prior_login");
if (redirectUrl != null && !redirectUrl.contains("users/password-reset")
&& !redirectUrl.contains("users/registration")
&& !redirectUrl.contains("login/custom_login")) {
if(redirectUrl.equals("http://localhost:8080/")) redirectUrl ="http://localhost:8080/users/homepage";
if(redirectUrl.equals("https://localhost:8443/")) redirectUrl ="http://localhost:8443/users/homepage";
if(redirectUrl.equals("https://www.podcastpedia.org/")) redirectUrl ="https://www.podcastpedia.org/users/homepage";
// we do not forget to clean this attribute from session
session.removeAttribute("url_prior_login");
// then we redirect
getRedirectStrategy().sendRedirect(request, response, redirectUrl);
} else {
super.onAuthenticationSuccess(request, response, authentication);
}
} else {
super.onAuthenticationSuccess(request, response, authentication);
}
}
}