2
我試圖在preRenderView監聽器方法中使用authenticate(),以便有條件地觸發認證,具體取決於頁面中的視圖參數。我嘗試添加一個簡單的方法:在JSF 2.0中使用HttpServletRequest authenticate()的問題preRenderView監聽器
@Named
@RequestScoped
public class PermissionBean implements java.io.Serializable {
public void preRender() {
System.out.println("IN PRERENDER");
HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
HttpServletResponse response = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
try {
request.authenticate(response);
} catch (Exception e) { // may throw ServletException or IOException
e.printStackTrace();
}
}
在認證方法本身並不拋出異常,它觸發重定向到Login.xhtml預期。但是,我得到我的服務器日誌中,我得到這個異常:
enter code here
INFO: IN PRERENDER
FINEST: GET /Login.xhtml previous[3]
INFO: Exception when handling error trying to reset the response.
java.lang.NullPointerException
at com.sun.faces.facelets.tag.jsf.core.DeclarativeSystemEventListener.processEvent(EventHandler.java:126)
at javax.faces.component.UIComponent$ComponentSystemEventListenerAdapter.processEvent(UIComponent.java:2508)
at javax.faces.event.SystemEvent.processListener(SystemEvent.java:106)
at com.sun.faces.application.ApplicationImpl.processListeners(ApplicationImpl.java:2129)
at com.sun.faces.application.ApplicationImpl.invokeComponentListenersFor(ApplicationImpl.java:2077)
at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:286)
at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:244)
at javax.faces.application.ApplicationWrapper.publishEvent(ApplicationWrapper.java:670)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:108)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
所以我的請求沒有被重定向到Login.xhtml。
我的問題是 - 這是應該在JSF管理的bean中工作,還是隻在JSF請求生命週期外是合法的?我嘗試從WebFilter調用authenticate(),並且它按照exptected的方式工作。
謝謝你,艾倫
工作 - 非常感謝你! – Ellen
不客氣。 – BalusC
我很尷尬地說,我沒有正確測試 - 我只是意識到我忘了刪除我創建的ServletFilter,所以它是ServletFilter進行身份驗證,而不是我的偵聽器方法。 當我刪除了ServletFilter時,在調用authenticate之後,對responseComplete()的調用失敗,因爲當前的FacesContext爲null。 我不確定這個協議 - 我應該打開一個新的問題嗎? – Ellen