意外错误转发或重定向到登录页面[重复]
Posted
技术标签:
【中文标题】意外错误转发或重定向到登录页面[重复]【英文标题】:Unexpected error forwarding or redirecting to login page [duplicate] 【发布时间】:2013-05-01 06:23:04 【问题描述】:我有一个在 GlassFish 3.1.2.2 上运行的 JSF 应用程序,使用 Mojarra 2.1.21 和 OmniFaces 1.4.1 来处理 Ajax 错误。该应用程序具有受保护区域和公共区域,使用基于表单的身份验证(在 JSF 表单上)和编程登录。
当用户单击 受保护页面 上的命令按钮时,我总是会得到一个空白页面,该命令按钮由底层 ServletException: Unexpected error forwarding or redirecting to login page 引起。 >
如果我错了,请纠正我,但从日志来看,这似乎是由于 Web 容器转发到 login.xhtml 但仍在尝试恢复旧视图 - 最终得到一个 ViewExpiredException,它包装成一个 ServletException,因此无法匹配和显示 web.xml 中定义的错误页面。这是一个例外:
[#|2013-05-07T16:51:12.175+0800|WARNING|glassfish3.1.2|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=105;_ThreadName=Thread-2;|ApplicationDispatcher[] PWC1231: Servlet.service() for servlet Faces Servlet threw exception
javax.faces.application.ViewExpiredException: viewId:/login.xhtml - View /login.xhtml could not be restored.
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:205)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:116)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:809)
...
|#]
[#|2013-05-07T16:51:12.176+0800|WARNING|glassfish3.1.2|org.apache.catalina.authenticator.FormAuthenticator|_ThreadID=105;_ThreadName=Thread-2;|Unexpected error forwarding or redirecting to login page
javax.servlet.ServletException: viewId:/login.xhtml - View /login.xhtml could not be restored.
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:809)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:671)
...
Caused by: javax.faces.application.ViewExpiredException: viewId:/login.xhtml - View /login.xhtml could not be restored.
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:205)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:116)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
... 32 more
|#]
我已经尝试了几个小时来捕捉它,但无济于事:
在登录页面配置中添加faces-redirect=true
无效,因为它不是结果
添加 servlet 过滤器以捕获 rootCause 不起作用,因为容器管理的安全性在过滤器之前启动,如另一篇文章所述<form-login-config> <form-login-page>/login.xhtml?faces-redirect=true</form-login-page> <form-error-page>/login.xhtml?faces-redirect=true</form-error-page> </form-login-config>
使@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException try chain.doFilter(request, response); catch (ServletException e) Throwable rootCause = e.getRootCause(); if (rootCause instanceof ViewExpiredException) // This is true for any FacesException. throw (ViewExpiredException) rootCause; // Throw wrapped ViewExpiredException instead of ServletException. else if (rootCause instanceof RuntimeException) // This is true for any FacesException. throw (RuntimeException) rootCause; // Throw wrapped RuntimeException instead of ServletException. else throw e;
login.xhtml
无状态(通过<f:view transient = "true">
)没有帮助,因为Faces Servlet 试图恢复的是过期页面的视图。
从 *** 上的众多帖子中,我知道有几个选项:
将登录页面设为非 JSF 页面,但我真的很想要一些 登录页面上的动态数据 使用自定义错误处理程序,它 工作,但它覆盖了 OmniFaces 的 FullAjaxException 处理程序并且没有 不再尊重 web.xml 中的错误页面声明我想知道的是有没有其他方法可以优雅地处理这个异常?允许转发到登录页面或显示错误页面。 (FullAjaxExceptionHandler 的非 Ajax 等价物将是完美的!)
非常感谢。
我的 web.xml:
> <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
<!-- See annotation declared servlets, converters, and filters in faces and servlet folders -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
1
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/WEB-INF/errorpages/expired.xhtml</location>
</error-page>
<error-page>
<exception-type>java.lang.RuntimeException</exception-type>
<location>/WEB-INF/errorpages/error.xhtml</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/errorpages/error.xhtml</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/errorpages/404.xhtml</location>
</error-page>
<!-- SECURITY -->
<security-constraint>
<display-name>AdminPagesConstraint</display-name>
<web-resource-collection>
<web-resource-name>AdminResource</web-resource-name>
<description/>
<url-pattern>/admin/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<description/>
<role-name>ADMIN</role-name>
</auth-constraint>
</security-constraint>
<security-constraint>
<display-name>CustomerPagesConstraint</display-name>
<web-resource-collection>
<web-resource-name>CustomerResource</web-resource-name>
<description/>
<url-pattern>/customer/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<description/>
<role-name>CUSTOMER</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>App_Realm</realm-name>
<form-login-config>
<form-login-page>/login.xhtml</form-login-page>
<form-error-page>/login.xhtml</form-error-page><!-- no use for programmatic login -->
</form-login-config>
</login-config>
<security-role>
<description/>
<role-name>CUSTOMER</role-name>
</security-role>
<security-role>
<description/>
<role-name>ADMIN</role-name>
</security-role>
</web-app>
【问题讨论】:
【参考方案1】:在 web.xml 中包含以下 sn-p。它对我有用
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/login.xhtml?faces-redirect=true</location>
</error-page>
但是注释掉
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/errorpages/error.xhtml</location>
</error-page>
【讨论】:
以上是关于意外错误转发或重定向到登录页面[重复]的主要内容,如果未能解决你的问题,请参考以下文章