on 2019 May 13 4:55 PM
Within my website, there is a header that allows a user to login without having to go to the ("/login") URL. This header has its own AuthenticationSuccessHandler, and I need to determine what URL the user was on when the button was pressed.
I am unaware of how I can get the URL and also redirect back to the current page the user signed in on. Primarily the only three cases for this are:
1. If the user is on a product page ("/p/"), they will need to be placed back on the product page.
2. If the user is on a search page ("/search"), they will be placed back on the search page.
3. If none of those cases are met, redirect the user to the homepage. (I'm redirecting the user to the homepage regardless in the Spring-security-mvc.xml)
public class GUIDAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
private GUIDCookieStrategy guidCookieStrategy;
private AuthenticationSuccessHandler authenticationSuccessHandler;
@Override
public void onAuthenticationSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws IOException, ServletException {
String referer = request.getHeader("referer");
if (referer.contains("/p/")) {
response.sendRedirect(referer);
else if (referer.contains("/search")) {
response.sendRedirect(referer);
} else {
getGuidCookieStrategy().setCookie(request,response);
getAuthenticationSuccessHandler().onAuthenticationSuccess(request,response,authentication);
}
}
@Required
public void setGuidCookieStrategy(final GUIDCookieStrategy guidCookieStrategy)
{
this.guidCookieStrategy = guidCookieStrategy;
}
protected AuthenticationSuccessHandler getAuthenticationSuccessHandler()
{
return authenticationSuccessHandler;
}
@Required
public void setAuthenticationSuccessHandler(final AuthenticationSuccessHandler authenticationSuccessHandler)
{
this.authenticationSuccessHandler = authenticationSuccessHandler;
}
}
Could anyone help? I've been able to redirect to the respective pages, however, when I go to a different page, like my-account and logout. Then sign back in, I'm taken to the my-account when I should be redirected to the homepage despite my defaultTargetUrl being set to "/".
Request clarification before answering.
Hi
you can take one variable put it into session when you are in search page ,update the variable with search like that all pages it will applicable ,after login to site check the variable value if empty go to
redirect to home page
thanks, prabhakar
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.