Search code examples
jsfhttp-redirectjsf-2webspherebasic-authentication

How to redirect to request page after basic athentication on websphere


I'm trying to login the user using the HttpServletRequest.login method. I have setup my web.xml, created a login.xhtml, and mapped the action of the login button to my backing bean method called performLogin

The problem is to get the URL from where the user was redirected. Ie. he attempted to go to index.xhtml, but did not have a session, so is redirected to login.xhtml. I want to get the url he requested in the first place, so I try reading the RequestDispatcher.FORWARD_REQUEST_URI from the request map as described by balusC here: JSF 2.0 : How to redirect to the protected page after using HttpServletRequest.login

This does not work when using websphere, I guess because it does not forward, but redirect the user to the login page. However, since Websphere itself is able to do the correct forwarding when using the built in j_security_check action in the http-form, this must be possible to accomplish!

So, my question is basically; how can I get hold of this uri in order to forward the user to the correct page when logged in successfully, while running on websphere?


Solution

  • To get the url where you were redirected from on websphere, you can read the cookie named WASReqURL. The uri you get here includes hostname, port and context path, so I remove these in my method:

    private String getRedirectUrl() {
        Map<String, Object> cookies = FacesContext.getCurrentInstance().getExternalContext().getRequestCookieMap();
    
        if (cookies.containsKey(WAS_REDIRECT_COOKIE_NAME)) {
            Cookie cookie = (Cookie) cookies.get(WAS_REDIRECT_COOKIE_NAME);
    
            String url = cookie.getValue();
    
            String context = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
    
            if (url != null && url.contains(context)) {
                url = url.substring(url.indexOf(context) + context.length() + 1);
            }
    
            return url;
        }
        return null;
    }