![](/img/trans.png)
[英]Spring Security and External Application for Authentication (Single-sign-on)
[英]Single-Sign-On and Spring - beans get initiated after first JSF lifecycle e.g. the index page
为了使自己更加清楚:我们不再使用单一登录即登录页面,因此用户名/角色已经可用。 现在,角色为Beer的用户想要访问页面啤酒,而角色为LoudFart的用户想要访问页面LoudFart-首次进入应用程序
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;
}
}
可悲的现实是,spring没有为欢迎页面添加任何bean
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)
在第一次使用nullpointer调用之后,usersession存在,并且可以再次工作:-),但是仍然很糟糕
顺便说一句,它位于JBoss 4.2.2上,我尝试了两种方法org.springframework.web.context.ContextLoaderListener和org.springframework.web.context.ContextLoaderServlet
有其他方法吗? 尝试不同的配置? 多谢你们
它比我想象的要容易得多:(只要确保在要获取的bean的构造函数中没有nullpointer即可;-))
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;
}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.