gpt4 book ai didi

jsf - 意外错误转发或重定向到登录页面

转载 作者:行者123 更新时间:2023-12-03 08:00:53 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





javax.faces.application.ViewExpiredException: View could not be restored

(10 个回答)


2年前关闭。




我有一个 JSF 应用程序在 GlassFish 3.1.2.2 上运行,带有 Mojarra 2.1.21 和 OmniFaces 1.4.1 来处理 Ajax 错误。该应用程序具有 protected 区域和公共(public)区域,使用基于表单的身份验证(在 JSF 表单上)和编程登录。

当用户单击 上的命令按钮时,我总是会得到一个空白页面 protected 页面 由基础 引起的过期ServletException:意外错误转发或重定向到登录页面。

如果我错了,请纠正我,但从日志来看,这似乎是由于 Web 容器转发到 login.xhtml 但仍在尝试恢复旧 View - 最终得到一个 ViewExpiredException,它包装成一个 ServletException,所以没有错误-web.xml 中定义的页面可以匹配和显示。这是一个异常(exception):

    [#|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登录页面配置无效,因为它不是结果

  •  <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>

  • 添加 servlet 过滤器以捕获 rootCause 不起作用,因为容器管理的安全性在过滤器之前启动,如另一篇文章
  • 中所述

    @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 试图恢复的是过期页面的 View 。

  • 从 StackOverflow 上的众多帖子中,我知道有几个选项:
  • 使登录页面成为非 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>

    最佳答案

    在 web.xml 中包含以下代码段。它对我有用

    <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>

    关于jsf - 意外错误转发或重定向到登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16416662/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com