ViewExpiredException发生时,如何恢复View视图

Mojarra JSF ViewState 反序列化漏洞复现 JavaServer Faces,新一代的Java Web应用技术标准,吸收了很多Java Servlet以及其他的Web应用框架的特性。JSF为Web应用开发定义了一个事件驱动的、基于组件的模型。其中最常用的是Sun(现在的Oracle)发布的Mojarra和Apache发布的MyFacesJavaServerFaces(JSF)概念在几年前就已经引入,现在主要在J2EE中使用。 阅读详情
javax.faces.application.ViewExpiredException: View could not be restored
ViewExpiredException发生时,View视图无法恢复




1. web.xml 中, STATE_SAVING_METHOD 设置为 server


1.5 当用户从一个view中<h:form>表单中使用HTTP POST方法(<h:commandLink>, <h:commandButton> 或者 <f:ajax>)
请求服务时,Session中的相应的view state不存在,此时ViewExpiredException被抛出。 


2. <h:form> 表单中隐藏的变量 javax.faces.ViewState 是一个ID,它与Session中serialized view state 相互对应,
    当Session由于某种原因(Server 或 Client 端的time out , 或者 浏览器中Session cookie 不存在了,
或者Server端的HttpSession#invalidate()被呼叫,或者Server端的bug,导致 session cookie被认定为WildFly), 
导致Session中的serialized view state 不存在时,客户将得到这个exception,ViewExpiredException 将被抛出。
  
3. JSF的views的数量有一个上限,当这个上限被触及,最少浏览的view(least recently used view )将被失效。参考
 com.sun.faces.numberOfViewsInSession vs com.sun.faces.numberOfLogicalViews.


4.当STATE_SAVING_METHOD设定为 client时,javax.faces.ViewState中隐藏的输入量没有值,当Session expires,客户端无法获取
ViewExpiredException.这种情形也会发生在 Cluster的环境下("ERROR: MAC did not verify" is symptomatic),或者当
Client端实现了特制的state timeout的情况,或者Server端重新启动导致AES密钥重新生成的情况时。


避免在page navigation时出现ViewExpiredException


1. In order to avoid ViewExpiredException when e.g. navigating back after logout when the state 
saving is set to server, only redirecting the POST request after logout is not sufficient. 
You also need to instruct the browser to not cache the dynamic JSF pages, otherwise the browser 
may show them from the cache instead of requesting a fresh one from the server when you send 
a GET request on it (e.g. by back button).
比如,在STATE_SAVING_METHOD 设置为 server时,当用户logout后,点击navigating back,
为了避免出现ViewExpiredException,实施POST请求的redirect是没有效果的。你必须还要
设置browser对dynamic JSF页面不实施cache缓存,否则,当你发送一个GET请求(例如back button)后,
browser将显示cache中的缓存页面,而不从server中刷取新的页面。




2. The javax.faces.ViewState hidden field of the cached page may contain a view state ID value which 
is not valid anymore in the current session. If you're (ab)using POST (command links/buttons) instead 
of GET (regular links/buttons) for page-to-page navigation, and click such a command link/button on the 
cached page, then this will in turn fail with a ViewExpiredException.


Cached page中的javax.faces.ViewState的隐藏变量中包含着一个view state ID,这个ID在当前的session中不再有效。
如果你用POST(command links/buttons)代替GET (regular links/buttons) 实施page 的navigation,比如点击 
cached page 中的command link/button,这时由于ViewExpiredException的原因页面跳转将失败。


To fire a redirect after logout in JSF 2.0, either add <redirect /> to the <navigation-case> in question 
(if any), or add ?faces-redirect=true to the outcome value.


为了在JSF2.0中,在用户logout后触发一个redirect,可以在涉及的<navigation-case>中添加<redirect />,或者在outcome
中添加?faces-redirect=true。


例如<h:commandButton value="Logout" action="logout?faces-redirect=true" />  


或者


public String logout() {
    // ...
    return "index?faces-redirect=true";
}


3. To instruct the browser to not cache the dynamic JSF pages, create a Filter which is mapped on the servlet
 name of the FacesServlet and adds the needed response headers to disable the browser cache. E.g.


为了将browser设置为不cache动态JSF页面,可以依据FacesServlet的servlet创建一个Filter, 添加相应的response headers 以便关闭
browser的cache属性。
 


@WebFilter(servletNames={"Faces Servlet"}) // Must match <servlet-name> of your FacesServlet.
public class NoCacheFilter implements Filter {


    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;


        if (!req.getRequestURI().startsWith(req.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) { // Skip JSF resources (CSS/JS/Images/etc)
            res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
            res.setHeader("Pragma", "no-cache"); // HTTP 1.0.
            res.setDateHeader("Expires", 0); // Proxies.
        }


        chain.doFilter(request, response);
    }


    // ...
}




Avoiding ViewExpiredException on page refresh
页面刷新过程中避免ViewExpiredException


In order to avoid ViewExpiredException when refreshing the current page when the state saving is set to server, 
you not only need to make sure you are performing page-to-page navigation exclusively by GET (regular links/buttons), 
but you also need to make sure that you are exclusively using ajax to submit the forms. If you're submitting the 
form synchronously (non-ajax) anyway, then you'd best either make the view stateless (see later section), or to 
send a redirect after POST (see previous section).


1. 在STATE_SAVING_METHOD 设置为 server情况下,为了避免页面刷新过程中发生ViewExpiredException,
你不但要确保页面跳转不使用GET (regular links/buttons),而且还要确保forms的请求不使用ajax方式。如果你提交同步(非ajax)请求,
你最好将view设置为stateless,或者在POST后发送一个redirect(见上一节) 


Having a ViewExpiredException on page refresh is in default configuration a very rare case. It can only happen when the limit 
on the amount of views JSF will store in the session is hit. So, it will only happen when you've manually set that limit way 
too low, or that you're continuously creating new views in background (e.g. by a badly implemented poll). 
See also com.sun.faces.numberOfViewsInSession vs com.sun.faces.numberOfLogicalViews. Another cause is having duplicate 
JSF libraries in runtime classpath conflicting each other. The correct procedure to install JSF is outlined in our JSF wiki page.


2.缺省配置情况下,在页面刷新时发生ViewExpiredException意外是很罕见的。当Session中JSF view的数量达到上限值时才会发生。因此,
只有当手工设置这个上限值过低时,或者在后台不断地创建新的views时(比如,错误的实现poll). 另一个原因是在runtime的classpath 存在重复
的JSF libraries,彼此相关干扰。




Handling ViewExpiredException
处理ViewExpiredException




When you want to handle an unavoidable ViewExpiredException after a POST action on an arbitrary page which was already opened
 in some browser tab/window while you're logged out in another tab/window, then you'd like to specify an error-page for that 
in web.xml which goes to a "Your session is timed out" page. E.g.


1.当你想在一个浏览器中的tab/window打开任意的页面,产生一个POST请求后,处理无法避免的ViewExpiredException时,你最好在web.xml中
声明一个error-page,跳转到一个"Your session is timed out"页面。例如:




<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/WEB-INF/errorpages/expired.xhtml</location>
</error-page>


2.Use if necessary a meta refresh header in the error page in case you intend to actually redirect further to home or login page.
如果需要,在error page中使用meta refresh header,这样你可以跳转到你希望的home或login页面。


<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Session expired</title>
        <meta http-equiv="refresh" content="0;url=#{request.contextPath}/login.xhtml" />
    </head>
    <body>
        <h1>Session expired</h1>
        <h3>You will be redirected to login page</h3>
        <p><a href="#{request.contextPath}/login.xhtml">Click here if redirect didn't work or when you're impatient</a>.</p>
    </body>
</html>




(the 0 in content represents the amount of seconds before redirect, 0 thus means "redirect immediately", you can use e.g. 3 to let the browser wait 3 seconds with the redirect)


Note that handling exceptions during ajax requests requires a special ExceptionHandler. See also Session timeout and 
ViewExpiredException handling on JSF/PrimeFaces ajax request. You can find a live example at OmniFaces FullAjaxExceptionHandler 
showcase page (this also covers non-ajax requests).


3.注意的是,在ajax请求时,处理exceptions需要一个特定的ExceptionHandler.可以参考JSF/PrimeFaces ajax request中对ViewExpiredException的处理方法。
也可以找个一个 OmniFaces中的FullAjaxExceptionHandler的例子(也包括了non-ajax requests)。




Also note that your "general" error page should be mapped on <error-code> of 500 instead of an <exception-type> of e.g. java.lang.Exception 
or java.lang.Throwable, otherwise all exceptions wrapped in ServletException such as ViewExpiredException would still end up in 
the general error page. See also ViewExpiredException shown in java.lang.Throwable error-page in web.xml.


4.注意的是,你的“通用”error page需要和 500的<error-code> 做匹配,而不是<exception-type>设置为java.lang.Exception 或者
java.lang.Throwable。此外,所有的ServletException,比如 ViewExpiredException都要在通用的error page中设置。可以参看在web.xml中的
java.lang.Throwable error-page中的ViewExpiredException 。


<error-page>
    <error-code>500</error-code>
    <location>/WEB-INF/errorpages/general.xhtml</location>
</error-page>




Stateless views
无状态views


A completely different alternative is to run JSF views in stateless mode. This way nothing of JSF state will 
be saved and the views will never expire, but just be rebuilt from scratch on every request. You can turn on 
stateless views by setting the transient attribute of <f:view> to true:
另一种方法是将JSF view运行在无状态模式。这种方式下,JSF state将不做存储,因此views也不会过期,但是会因为request
重新构建。你可以将<f:view>的短暂状态设置为true:


<f:view transient="true">


</f:view>


This way the javax.faces.ViewState hidden field will get a fixed value of "stateless" in Mojarra 
(have not checked MyFaces at this point). Note that this feature was introduced in Mojarra 2.1.19 and 2.2.0 
and is not available in older versions.


The consequence is that you cannot use view scoped beans anymore. They will now behave like request scoped 
beans. One of the disadvantages is that you have to track the state yourself by fiddling with hidden inputs 
and/or loose request parameters. Mainly those forms with input fields with rendered, readonly or disabled 
attributes which are controlled by ajax events will be affected.


这样做法的效果是,你不能再使用view scoped beans。这些bean不能够再以request scoped beans的形式运行。这样做的
劣势是你无法依据隐藏的inputs和/或者松散的request parameters轻松地追踪state状态。这些表单forms,其输入变量的渲染
、只读或关闭的属性,这些由ajax事件改变的属性的表单将会受到影响。


Note that the <f:view> does not necessarily need to be unique throughout the view and/or reside in the master
 template only. It's also completely legit to redeclare and nest it in a template client. 
It basically "extends" the parent <f:view> then. E.g. in master template:


注意的是,<f:view>在view的生命周期中,和/或者以master template方式构建,不是必须要保持唯一的。原则上它可以
“extends”父类<f:view>。例如,在master template中:


<f:view contentType="text/html">
    <ui:insert name="content" />
</f:view>
and in template client:


<ui:define name="content">
    <f:view transient="true">
        <h:form>...</h:form>
    </f:view>
</f:view>


You can even wrap the <f:view> in a <c:if> to make it conditional. Note that it would apply on the entire 
view, not only on the nested contents, such as the <h:form> in above example.


您可以在<c:if>中嵌入<f:view>,可以做些条件设定。注意的是,它将在view中一直有效,包括在嵌套的内容中,
例如在上面例子中<h:form>...</h:form>的部分也有效。









永磁同步电机无传感器控制:HFI与ESMO技术详解 高频注入(HFI)和增强型滑模观测器(ESMO)是现代永磁同步电机(PMSM)无传感器控制的核心技术。HFI通过注入高频信号利用电机凸极效应获取转子位置,而ESMO则通过改进传统滑模观测器,有效抑制抖振并提升动态响应。这两种技术的结合特别适用于零低速区域的位置估计,在工业伺服、电动汽车等领域有广泛应用。方波高频注入作为实现方案之一,具有计算量小、实现简单的特点,配合自适应滤波等信号处理技术,可以在保证精度的同时降低系统成本。 阅读详情

相关推荐

在AVP28335(兼容德州仪器TMS320-28335) 上实现ModbusSlaveRTU-485源码及注释。

今天先把题目先给出来, 在近两天会把源码 (含详细注释 )及部署、测试结果给出来, 希望能给大家帮助。(全免费 我有点缺德,断了别人的一个财路)。

FOOLCODE的专栏 642

javax.faces.application.ViewExpiredException与no saved view state could be found的解决方案

Caused by: javax.faces.application.ViewExpiredException: /UserManage/UserOpHist.facesNo saved view state could be found for the view identifier: /UserManage/UserOpHist.faces

Steven的专栏 3282

Holtek BS45F3833芯片深度剖析:如何用高集成MCU重塑超声波雾化市场格局?

本文深度剖析Holtek BS45F3833芯片如何通过高集成MCU技术重塑超声波雾化市场。该芯片集成了PWM直驱、电容检水、触摸控制等核心功能,显著提升雾化精度和效率,同时降低BOM成本和开发难度。文章通过实测数据和实战案例,展示了其在香薰机、车载设备及工业加湿器等场景的创新应用,为开发者提供了一站式解决方案。

weixin_29192211的博客 415

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

后台页面以JSF编写,当客户端的session失效后,再去点击JSF页面中的操作时,会要求用户再次输入用户名、密码,然后重定向到之前点击操作的页面,在此重定向时产生了异常: javax.faces.application.ViewExpiredException: viewId:/jsf/profile/editProfileRedirect.xhtml - View /jsf/profil

Seven's Rainbow Paradise 2266

避坑指南:将大华官方Java SDK无缝迁移到SpringBoot 2.x项目的完整流程

本文详细介绍了如何将大华官方Java SDK无缝迁移到SpringBoot 2.x项目,重点解决内存管理、连接池设计和异步处理等核心问题。通过设备连接池化、异步非阻塞接口改造和内存管理优化,实现刷卡、刷脸等认证功能的高效整合,提升Web服务的稳定性和性能。

weixin_42557830的博客 418

IAR编译相同工程生产hex文件checksum不一致问题

IAR Checksum

xqw19891201的博客 1384

JSF的ViewExpiredException异常处理 转

视图没有存储的异常 javax.faces.application.ViewExpiredException: viewId:/pages/gardeninfo/envRiskEditForm.faces - View /pages/gardeninfo/envRiskEditForm.faces could not be restored. 对于用户来说是不懂的,那只要在重写生命周期便可处理...

yuzhi2217的专栏 295

airodump-ng、aircrack-ng使用教程

wifi破解、airodump-ng、aircrack-ng脚本

ssmbyss的博客 2233

jsf 中 ViewExpiredException 的处理

在jsf 中,存在 session 的地方,一般会去使用session 去保存数据或者其他的事情,但是当session time out or forced to be invalidate  , 就会抛出这样的 ViewExpiredException 异常。 如果是 web server == sun app server  then solution is ::   in web

北冥有鱼 其名为鲲 化而为鸟 其名为鹏 3729

独家开源|RK3588 平台 YOLOv5 Android 源码级解析:NPU 量化、多线程与目标跟踪实战

本文将带你完成一个完整的AI视觉项目:在搭载RK3588芯片的Android开发板上运行实时目标检测与跟踪应用。实战目标:成功运行官方YOLOV5摄像头实时检测案例技术深度:深入理解RKNN开发框架和源码实现核心技能:掌握NPU推理、多线程并发、目标跟踪等关键技术。

m0_71589036的博客 194

【人工智能】Cursor模型被封?两步教你破解

Cursor模型全线被禁,主流模型如Claude、GPT-4无法访问,仅剩Deepseek等基础模型可用。官方证实限制源于上游API区域政策,提供三种"无效"解决方案引发用户不满。社区发现有效解决方式:需同时开启全局代理和将Cursor网络设置改为HTTP/1.1协议,详细步骤包括修改用户设置和完全重启程序。另有SOCKS5代理配置方案供高级用户选择。技术原理推测HTTP/1.1可绕过区域检测。用户面临继续使用或转投其他工具的选择,社区交流受限但仍有讨论。

奶爸的编程之路,也就一周冷个三天 2202

JavaWEB开发中的部分常见异常

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34384915的博客 350

【单智能体】AI 数据分析智能体案例讲解(附完整源码)

本案例将引导您构建一个基于 AI 的数据分析智能体,让用户能够通过自然语言查询来分析 CSV 和 Excel 文件中的数据,无需掌握 SQL 知识。该智能体使用 Agno Agent 框架集成 OpenAI GPT-4o 模型,并利用 DuckDB 提供高效的数据处理能力。

rengang66的博客 239

Restricting the Number of Views in a Session

      &lt;context-param&gt;        &lt;param-name&gt;com.sun.faces.numberOfViewsInSession&lt;/param-name&gt;        &lt;param-value&gt;15&lt;/param-value&gt;    &lt;/context-param&gt;    &lt;context...

板三的梦 192

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

could not be restored

转 http://hi.baidu.com/h2opo/blog/item/95c6c2fdd8f0c940d7887d64.html avax.servlet.ServletException: viewId:/login.jsp - View /login.jsp could not be restored. javax.faces.webapp.FacesServlet.service(F...

supremeYing 576

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

javax.faces.application.ViewExpiredException: viewId xxx- View xxx could not be restored.

项目中老出现这样的异常。都是在session过期之后的自动跳转时出现,检查代码,有一块跳转 在sessioni == null情况下仍然调用FilterChain.doFilter..。加上判断。 另外将RequestDispatcher 修改为response.sendRedirect.... 问题解决~~ 转载于:https://www.cnblogs.com/lostyue/archiv...

weixin_30526593的博客 245

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

android.view.InflateException 膨胀(充气)异常 的解决

解决异常:android.view.InflateException: Binary XML file line #8: Error inflating class com.match.txtview 现象:在xml布局文件中调用自己写的view时报了这个错误,试了好多办法都不行,我发现继承TextView时少写了一个构造方法public TxtWordView(Context context, ...

qq_43603312的博客 1989

用户重新登录的异常处理

用户退出WEB,然后在登录界面停留(用户名 密码已经输入),此时重启WEB的服务,然后登录以后,就会有javax.faces.application.viewexpiredexception viewid /index.xhtml view could not be restored异常报出, 说的是/index.xhtml不能被加载。 解决的办法参照了 [url]http:/...

iteye_15870的博客 356

java not available_java.security.NoSuchAlgorithmException: SHA1PRNG SecureRandom not available

SEVERE: Cannot find message associated with key managerBase.digestjava.security.NoSuchAlgorithmException: MD5 MessageDigest not availableat sun.security.jca.GetInstance.getInstance(Unknown Source)at j...

weixin_36325338的博客 812

Java安全之Mojarra JSF反序列化

JavaServer Faces,新一代的Java Web应用技术标准,吸收了很多JavaServlet以及其他的Web应用框架的特性。JSF为Web应用开发定义了一个事件驱动的、基于组件的模型。其中常用的是Sun(现在的Oracle)发布的Mojarra和Apache发布的MyFacesJavaServerFaces(JSF)概念在几年前就已经引入,现在主要在J2EE中使用JSF 和类似的 Web 技术之间的区别在于 JSF 使用。

dzqxwzoe的博客 1668

解决问题(六)——jsf ViewExpiredException

javax.servlet.ServletException: viewId:/login.jsp - View /login.jsp could not be restored. javax.faces.webapp.FacesServlet.service(FacesServlet.java:249) root cause javax.faces.application.ViewExpired

适己而忘人者,人之所弃;克己而立人者,众之所戴。 3333

YII Framework学习教程-YII的V-view的Exception-2011-11-17

当我们访问http://www.localyii.com/testwebap/index.php?r=site/coxxxxxxxntacxt,当这个action不存在的时候,该如何显示。默认会显示一大堆堆栈信息,对用户是极不友的,yii中提供了一种方法来处理这种问题。   例如  /yii_dev/testwebap/protected/controllers/SiteController.

梦想摆渡的专栏 1617

关于viewstate保护问题

<br />常用保护viewstate有两种方式: <br />1、防篡改 <br />防篡改,使用散列代码. <br />在页面顶部加入如下代码:Page EnableViewStateMAC=true <br />asp.net就会自动的在viewstate中追加一个散列码,<br />在页面回传时,服务器根据回传的viewstate生成一个散列码,<br />再与回传的散列码相比较,如果不对,则丢弃该viewstate,同时控件将恢复初试状态. <br />(默认情况下asp.net是通过SHA1算法

五十三参 547

在 JavaServer Faces (JSF) 框架中,javax.faces.view 包提供了与视图相关的核心类和接口

以上只是JSF框架中一部分常用包和类的概述。要深入了解这些类和接口的功能和使用方法,建议查阅JSF的官方文档和教程。Application:代表JSF应用程序的上下文,提供了应用程序范围内的服务,如创建和管理转换器、验证器、监听器、导航处理程序等。Lifecycle:代表JSF生命周期的接口,定义了处理请求的各个阶段(如恢复视图、应用请求值、验证和更新模型值、渲染响应等)。ListDataModel、ArrayDataModel等:DataModel的实现类,用于处理列表和数组类型的数据。

[Blog][Domain] programb.blog.youkuaiyun.com 422

vulhub漏洞复现39_Mojarra

vulhub漏洞复现练习篇

weixin_44193247的博客 1026

精通 JavaServerFace 2.2(五)

通常,JSF 应用程序的性能与 CPU 内存、序列化/反序列化任务和网络带宽直接相关。当这些变量开始成为头痛的来源,或者出现或类型的错误时,就是了解 JSF 管理视图状态功能及其如何精细调整以提高性能的时候了。因此,在本章中,我们将讨论 JSF 保存视图状态(JSF 的部分保存视图状态功能、JSF 在服务器/客户端保存视图状态、逻辑视图和物理视图等)以及 JSF 2.2 无状态视图。首先,你必须知道 JSF 使用/ API 在请求之间保存和恢复视图状态。JSF 在其生命周期中这样做,视图状态在请求结束时保存

龙哥盟 560
上一篇: RFC3415- SNMP VACM 中文版(1) SNMPv3 中文版
WebNMS
博客等级 码龄18年 2粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值