i was able to resolve it by writing my own custom AuthenticationSuccessHandler and LOgoutAuthenticationSuccessHandler classes by extending SimpleUrlAuthenticationSuccessHandler and SimpleUrlLogoutSuccessHandler.
i need logotsuccesshandler because i wanted to user to stay in the same page he was in when he clicked logout(because logout is in all the pages.)
for UsernamePasswordAuthenticationFilter set bean property name="authenticationSuccessHandler" as your CustomAuthSuccessHandle
Code:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;
public class CustomAuthSuccessHandler extends
SimpleUrlAuthenticationSuccessHandler {
private RequestCache requestCache = new HttpSessionRequestCache();
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
SavedRequest savedRequest = requestCache.getRequest(request, response);
if (savedRequest != null) {
// Use the DefaultSavedRequest URL
String targetUrl = savedRequest.getRedirectUrl();
logger.debug("Redirecting to DefaultSavedRequest Url: " + targetUrl);
getRedirectStrategy().sendRedirect(request, response, targetUrl);
}
String redirectUrl=request.getHeader("Referer");
getRedirectStrategy().sendRedirect(request, response, redirectUrl);
}
}
here i have used requestCache because when an unauthorized user access a authorized page, spring will redirect him to login page and after success login, our Customauthsuccesshandler will redirect to the page which he initially wanted to access.
For LogoutFilter set bean property name="logoutSuccessHandler" as CustomLogoutSuccessHandler
Code:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;
public class CustomLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {
@Override
public void onLogoutSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
String redirectUrl=request.getHeader("Referer");
getRedirectStrategy().sendRedirect(request, response, redirectUrl);
}
}
Regards,
Sunil