Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

404 with response.sendRedirect

I am using spring security login mechanism for my application and tested everything.Things were working fine.I have the following use case

  1. If customer is not logged in , application will redirect customer to the login page.
  2. On successful login, application will redirect customer back to same page from where they were redirected to the login page

this is the Java code used to redirect user to his original location

final SavedRequest savedRequest = this.requestCache.getRequest(request, response);
targetUrl = savedRequest.getRedirectUrl();
getRedirectStrategy().sendRedirect(request, response, targetUrl);

RedirectionStrategy being used here is DefaultRedirectStrategy, things were working fine. Application is now deployed on the Pre Production server and now this seems not working and I am getting 404 error.

When customer is being redirected to the home page,targetUrl is coming out as "/", I have a Spring controller named with this mapping

@RequestMapping("/")
public class HomePageController{  // home page code }

my application's current Pre-Prod urs is prepd-www.mysite.com so when sendredirect come in to action, webpage URL is getting changed to prepd-www.mysite.com/prepd-www.mysite.com

I am not sure what is causing this issue. is it because of the proxy server settings ? Can any one suggest me about the possible root cause of this issue?

I have already tried it on all local machines and well on our QA but everything is working perfectly fine.

Current setup for the environment where this is happening is

  1. We have 4 app server
  2. We have one load balancer which is redirecting traffic to one of the app server.
like image 312
Umesh Awasthi Avatar asked Nov 21 '14 08:11

Umesh Awasthi


3 Answers

Just a wild guess since you could not give the reverse proxy configuration, nor the exact URL used in pre prod and in developpement.

You say you are using DefaultRedirectStrategy from Spring security. This strategy has an option (contextRelative) that prepends the ServletContext path to the URL. If in your developpement system you were using the root context, that is if you were accessing home page at (for example) : http://localhost:8080/ the serlet context was empty.

But if now in preprod, the servlet context is no longer root but is say /myApp once translated by apache reverse proxy, when you redirect you get an URL of /myApp/myApp that could be translated back to what you gave.

You could try to control whether you have contextRelative as true in DefaultRedirectStrategy and if yes if you can set if to false and also control if you redirect to absolute or relative URLs.

like image 137
Serge Ballesta Avatar answered Sep 25 '22 23:09

Serge Ballesta


If you are using apache in front check rewrite rule and redirect rules of apache config. Best way would be to ssh tunnel directly to application server(by skipping apache) and test. If it's working that means your application config is fine and it needs to be fixed in apache.

like image 39
Chirag Shah Avatar answered Sep 24 '22 23:09

Chirag Shah


Are you using in preproduction tomcat or another application server?, normally if your war is calling foo and your commit to tomcat, the path for this war is

   http://localhost:8080/foo/ 

So if you are using servlet you need specify in your web.xml that the main path is foo/*

like image 27
paul Avatar answered Sep 22 '22 23:09

paul