Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Security 3 Get Initially Requested URL

I need to modify my spring security login page based on where the user came from. My client wants the styles different between the two. If you come from appcontextroot/test vs appcontextroot/choose. I tried to do the below but the String url=savedRequest.getRedirectUrl(); is equal to the spring login page already and not the initial page requested by the user. Any ideas?

ExternalContext externalContext = FacesUtils.getExternalContext();
    HttpServletRequest request = (HttpServletRequest)externalContext.getRequest();
    HttpSession session = request.getSession(false);
    if(session != null) {
        SavedRequest savedRequest = new DefaultSavedRequest(request, new PortResolverImpl());
        String url=savedRequest.getRedirectUrl();
    } 
like image 510
c12 Avatar asked Mar 22 '11 09:03

c12


2 Answers

You need to extract SavedRequest from the session, not to create a new one:

SavedRequest savedRequest = 
    new HttpSessionRequestCache().getRequest(request, response);
like image 193
axtavt Avatar answered Sep 26 '22 03:09

axtavt


SavedRequest savedRequest =
    (SavedRequest)session.getAttribute("SPRING_SECURITY_SAVED_REQUEST");
// ...check for null...
String targetUrl = savedRequest.getRedirectUrl();

Ugly but working, if you don't have the HttpServletResponse available (e.g. in case you use org.springframework.social.connect.web.SignInAdapter).

Tested with Spring Security 3.1.0.RC2.

like image 38
weekens Avatar answered Sep 26 '22 03:09

weekens