You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Yee CN <ye...@streamyx.com> on 2005/11/07 04:53:28 UTC

Cannot do getRequestDispatcher("/login.jsf").forward() with JSF?

Hi,

 

I am trying to write a very simple security filter to redirect the user to
the login page if he/she is not login, something like the following:

 

            public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain)

                                    throws IOException, ServletException {

 

                        if (!authenticated((HttpServletRequest)req)) {

 
((HttpServletRequest)req).getRequestDispatcher("/login.jsf").forward(req,
res);

                        } else {

                                    chain.doFilter(req, res);

                        }

            }

 

But I seems to be having problems with
getRequestDispatcher("/login.jsf").forward().

The login.jsf got forwarded to OK, however the JSF engine seems to get very
confused afterwards. The URL shown in the browser window is of the following
form:

 

http://localhost:8080/myapp/login.jsf;jsessionid=46D86B99BE29C1F112F1C8BD3CB
A6D6D

 

and nothing works till I logon via the login page directly.

 

Anyway around this?

 

Many thanks in advance.

 

Yee


Re: Cannot do getRequestDispatcher("/login.jsf").forward() with JSF?

Posted by Francesco Consumi <co...@istitutodeglinnocenti.it>.
Quoting Yee CN <ye...@streamyx.com>:


>
> Anyway around this?
>
>
>
I wrote a custom tag, that controls if user has got the required level 
to acces page, as this:

<user:access currentUser="${mb.currentUser}" pageLevel="2" />

The code of tag follows:

    public int doStartTag() throws JspException {
        JspWriter out = pageContext.getOut();
        try {
           HttpServletRequest req = (HttpServletRequest) 
pageContext.getRequest();
           pageContext.getSession().setAttribute("origPage", 
StringUtils.replace(req.getServletPath(), "jsp", "jsf"));
           loginPage = req.getContextPath() + 
pageContext.getServletContext().getInitParameter("loginPage");
           invLevelPage = req.getContextPath() + 
pageContext.getServletContext().getInitParameter("invLevelPage");
           HttpServletResponse resp = (HttpServletResponse) 
pageContext.getResponse();
           if(!currentUser.isValid) {
             resp.sendRedirect(loginPage);
             return SKIP_BODY;
          }
           if(!currentUser.inLevel(Integer.parseInt(pageLevel))) 
resp.sendRedirect(invLevelPage);
        } catch (IOException e){throw new JspException(e.getMessage());}
        return SKIP_BODY;
    }



currentUser is a class initialized with login page data and a level 
list for user read from DB. loginPage and "invLevelPage" are variables 
set in web.xml.

-- 
Francesco Consumi
Ufficio Sistemi informativi
Istituto degli Innocenti
Piazza SS.Annunziata, 12
50122 Firenze
consumi at istitutodeglinnocenti.it
Tel. +39 055 2037320
ICQ# 12516133



Re: Cannot do getRequestDispatcher("/login.jsf").forward() with JSF?

Posted by Michael Ageeb <mi...@acm.org>.
I 've made the same filter before

and i used redirection when user is not authentic

HttpServletResponse hres = (HttpServletResponse) response;
hres.sendRedirect("/login.jsf");


On 11/7/05, Yee CN <ye...@streamyx.com> wrote:
>
>  I solved the problem using sendRedirect(). However forward() was
> apparently used successfully in
> http://www.javaworld.com/javaworld/jw-07-2004/jw-0719-jsf.html so I am
> wondering whether this problem is specific to myfaces.
>
>  Regards,
>
> Yee
>
>   ------------------------------
>
> *From:* Yee CN [mailto:yeecn@streamyx.com]
> *Sent:* Monday, 7 November 2005 11:53 AM
> *To:* 'MyFaces Discussion'
> *Subject:* Cannot do getRequestDispatcher("/login.jsf").forward() with
> JSF?
>
>  Hi,
>
>  I am trying to write a very simple security filter to redirect the user
> to the login page if he/she is not login, something like the following:
>
>  public void doFilter(ServletRequest req, ServletResponse res, FilterChain
> chain)
>
> throws IOException, ServletException {
>
>  if (!authenticated((HttpServletRequest)req)) {
>
> ((HttpServletRequest)req).getRequestDispatcher("/login.jsf").forward(req,
> res);
>
> } else {
>
> chain.doFilter(req, res);
>
> }
>
> }
>
>  But I seems to be having problems with
> getRequestDispatcher("/login.jsf").forward().
>
> The login.jsf got forwarded to OK, however the JSF engine seems to get
> very confused afterwards. The URL shown in the browser window is of the
> following form:
>
>
> http://localhost:8080/myapp/login.jsf;jsessionid=46D86B99BE29C1F112F1C8BD3CBA6D6D
>
>  and nothing works till I logon via the login page directly.
>
>  Anyway around this?
>
>  Many thanks in advance.
>
>  Yee
>



--
Michael Ageeb Fakhry
Software Developer OpenCraft
Home Phone: +2 03 5053707
Business Phone: +2 02 3363848 / 110
Mobile Phone: +2 010 1336546
Business E-mail : michael.ageeb@open-craft.com

RE: Cannot do getRequestDispatcher("/login.jsf").forward() with JSF?

Posted by Yee CN <ye...@streamyx.com>.
I solved the problem using sendRedirect(). However forward() was apparently
used successfully in
http://www.javaworld.com/javaworld/jw-07-2004/jw-0719-jsf.html so I am
wondering whether this problem is specific to myfaces.

 

Regards,

Yee

 

  _____  

From: Yee CN [mailto:yeecn@streamyx.com] 
Sent: Monday, 7 November 2005 11:53 AM
To: 'MyFaces Discussion'
Subject: Cannot do getRequestDispatcher("/login.jsf").forward() with JSF?

 

Hi,

 

I am trying to write a very simple security filter to redirect the user to
the login page if he/she is not login, something like the following:

 

            public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain)

                                    throws IOException, ServletException {

 

                        if (!authenticated((HttpServletRequest)req)) {

 
((HttpServletRequest)req).getRequestDispatcher("/login.jsf").forward(req,
res);

                        } else {

                                    chain.doFilter(req, res);

                        }

            }

 

But I seems to be having problems with
getRequestDispatcher("/login.jsf").forward().

The login.jsf got forwarded to OK, however the JSF engine seems to get very
confused afterwards. The URL shown in the browser window is of the following
form:

 

http://localhost:8080/myapp/login.jsf;jsessionid=46D86B99BE29C1F112F1C8BD3CB
A6D6D

 

and nothing works till I logon via the login page directly.

 

Anyway around this?

 

Many thanks in advance.

 

Yee