Search code examples
javajsfhttp-redirectauthenticationjaas

Retrieve requested URL before JAAS redirect it


Is that possible to retrieve the user's requested URL before the JAAS redirect it?

my web.xml is like this:

<login-config>
    <auth-method>FORM</auth-method>
    <form-login-config>
        <form-login-page>/login.jsf</form-login-page>
        <form-error-page>/login.jsf</form-error-page>
    </form-login-config>
</login-config>

So, when I'm not logged and try to go at ex. www.blah.com/myApp/users.jsf then the app redirect me to login.jsf but the url still the same that the user requested.

then I fill the login form and click Login, that goes to my backbean where a call my loginModule and callbackHandler ex.:

public String actionLogin(){
   //do my login and stuff
   return "page that user requested";
}

I tried many ways to get the user's requested page. request.getRequestURL()

all returned me : www.blah.com/myApp/login.jsf

any way to get the requested page? www.blah.com/myApp/users.jsf

regards


Solution

  • The login page is internally opened by a server-side forward by RequestDispatcher#forward(). This thus means that the initially requested page is available as a request attribute with the name as specified in RequestDispatcher.FORWARD_REQUEST_URI constant. In JSF terms, that's thus available as follows:

    String originalURI = (String) externalContext.getRequestMap().get(RequestDispatcher.FORWARD_REQUEST_URI);
    String originalQuery = (String) externalContext.getRequestMap().get(RequestDispatcher.FORWARD_QUERY_STRING);
    
    if (originalQuery != null) {
        originalURI+= "?" + originalQuery;
    }
    

    (keep in mind to have a fallback URL for the case it returns null, i.e. when it's been opened directly without hitting a restricted URL first)

    The best place to collect it would be the (post)constructor of a @ViewScoped bean associated with the login page.

    See also: