Rewrite Call to a CDI Bean

Splash Forums Rewrite Users Rewrite Call to a CDI Bean

This topic contains 2 replies, has 2 voices, and was last updated by  Christian Kaltepoth 7 months, 1 week ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #27306

    andersonjunqueira
    Participant

    My situation is: I need to call a method to safelly logout from my webapp. I have to clear a cookie and invalidate the session.

    I tried to do it like this:

                .perform(
                    new HttpOperation() {
                        @Override
                        public void performHttp(HttpServletRewrite event, EvaluationContext context) {
                            SegurancaSSO sso = new SegurancaSSO();
                            sso.logout(credenciais.getClientId(), event.getRequest(), event.getResponse());
                            event.getRequest().getSession().invalidate();
                        }
                    }
    

    the sso.logout(…) line clears some cookies and session attributes followed by a session invalidate.

    It did not work, my cookies and oauth2 sso token was not cleared. So I tried:

                  .perform(PhaseAction.retrieveFrom(El.retrievalMethod("#{logoutMB.logout}"))
                  .after(PhaseId.RESTORE_VIEW)
                  .and(Redirect.permanent(hubURL)))

    But it do not call my CDI bean method (It uses ExternalContext from FacesContext to clear the cookies and the session), although it redirects the page correctly.

    So I configured as:

                  .perform(Invoke.binding(El.retrievalMethod("#{logoutMB.logout}"))
                  .and(Redirect.permanent(hubURL)))

    It does call my CDI bean method, but, I can’t seem to get FacesContext ’cause FacesContext.getCurrentInstance() returns null.

    Any thoughts?

    #27308

    andersonjunqueira
    Participant

    Sorry about it. I figured out what was happening.
    The cookie was being created with a path = “/restrito” and I was trying to read from “/”.
    The code wasn’t finding it.

    BTW the Rewrite rule final definition was:

            cb.addRule()
                .when(Direction.isInbound().and(Path.matches("/restrito/logout")))
                .perform(
                    new HttpOperation() {
                        @Override
                        public void performHttp(HttpServletRewrite event, EvaluationContext context) {
                            Cookie[] cookies = event.getRequest().getCookies();
                            for (int i = 0; i < cookies.length; i++) {
                                cookies[i].setMaxAge(0);
                                event.getResponse().addCookie(cookies[i]);
                            }
                            event.getRequest().getSession().invalidate();
                        }
                    }
                .and(Forward.to("/logout.jsf")));

    Thank you

    #27310

    Hey, thanks for sharing the solution.

    If you want to use FacesContext, you will always have to use “PhaseAction”. If you don’t, your code will be executed very early in the request processing lifecycle, even before JSF starts its processing. “PhaseAction” basically just queues your operation and executes it within the faces lifecycle.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

Comments are closed.