开发者

Single-Sign-On and Spring - beans get initiated after first JSF lifecycle e.g. the index page

开发者 https://www.devze.com 2023-02-19 12:23 出处:网络
To make myself a bit clear: We are using a single sign on, the login page, is thus no longer required and username / role is already available. Now a user with role beer wants to reach page beer where

To make myself a bit clear: We are using a single sign on, the login page, is thus no longer required and username / role is already available. Now a user with role beer wants to reach page beer whereas a user with role loudFart wants to reach page loudFart - the first time they enter the application

public class DebugPhaseListener implements PhaseListener {

    public void beforePhase(PhaseEvent event) {

        String path=FacesContext.getCurrentInstance().getExternalContext().getRequestServletPath();
        if("/index.jsf".equals(path)) {
            FacesContext fc = FacesContext.getCurrentInstance();
            String toOutcome =  (UserSession) context.getExternalContext().getSessionMap().get("userSession").getWelcomePage(); 

            System.out.println("redirecting "+ toOutcome);
            fc.getApplication().getNavigationHandler().handleNavigation(fc, null,toOutcome);
        }

    }

    public void afterPhase(PhaseEvent event) {

    }

    public PhaseId getPhaseId() {
        return PhaseId.RESTORE_VIEW;
    }
}

The sad reality is, spring does not init any beans for the welcome page

19:19:59,828 INFO  [STDOUT] RESTORE_VIEW 1 <--- before phase //this is true even for render response phase
19:19:59,830 WARN  [lifecycle] phase(RESTORE_VIEW 1,com.sun.faces.context.FacesContextImpl@197cf78) threw exception: java.lang.NullPointerException null
jp.funny.something.util.DebugPhaseListener.beforePhase(DebugPhaseListener.java:25)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:222)

After the first call with the nullpointer, the usersession exists and it works again :-) but that开发者_运维知识库 still sucks balls

Btw this sits on JBoss 4.2.2 and I tried both methods org.springframework.web.context.ContextLoaderListener and org.springframework.web.context.ContextLoaderServlet

Is there a different way to do it? Different configuration to try out? Thanks guys


It's a lot easier than I thought: (just make sure to not have a nullpointer in the constructor of the bean you are trying to get ;-) )

public class SomeListener implements PhaseListener{ public void beforePhase(PhaseEvent event) { 
        FacesContext fc = FacesContext.getCurrentInstance();
        String path=fc.getExternalContext().getRequestServletPath();
        if(LOGIN_PATH.equals(path)) {
            if(logger.isDebugEnabled())
                logger.debug("Redirecting away from " + LOGIN_PATH+ " page");
            FacesContext ctx = FacesContext.getCurrentInstance();
            UserSession u= UserSession.getInstance(ctx);
            if(u==null) {
                WebApplicationContext wac = FacesContextUtils.getWebApplicationContext(fc);
                u =(MyUserSession)wac.getBean("myUserSession");
            }
            String toOutcome =  u.login();
            if(logger.isDebugEnabled())
                logger.debug("redirecting "+ toOutcome);
            fc.getApplication().getNavigationHandler().handleNavigation(fc, null,toOutcome);
        }
    }
    public PhaseId getPhaseId() {
        return PhaseId.RESTORE_VIEW;
    }}
0

精彩评论

暂无评论...
验证码 换一张
取 消