Open Session In View

实战|验证码突破思路 漏洞成因:由于逻辑设计缺陷,可绕过验证,比如直接删除COOKIE或验证码参数可绕过、当验证不通过清空session时,验证码参数值为空时绕过等。测试方法:抓包,删除验证码字段,查看是否可以成功发送抓包,正常流程下,记录验证码后的数据包,替换目标包中内容,直接发送,查看是否可以直接绕过验证码案例:Step1.输入正确账户信息和错误验证码,登录时抓包Step2.删除COOKIEStep3.客户端登陆成功。 阅读详情
使用Spring提供的Open Session In View而引起Write operations are not allowed in read-only mode (FlushMode.NEVER) 错误解决:
    在没有使用Spring提供的Open Session In View情况下,因需要在service(or Dao)层里把session关闭,所以lazy loading 为true的话,要在应用层内把关系集合都初始化,如 company.getEmployees(),否则Hibernate抛session already closed Exception;    Open Session In View提供了一种简便的方法,较好地解决了lazy loading问题.
    它有两种配置方式OpenSessionInViewInterceptor和OpenSessionInViewFilter(具体参看SpringSide),功能相同,只是一个在web.xml配置,另一个在application.xml配置而已。
    Open Session In View在request把session绑定到当前thread期间一直保持hibernate session在open状态,使session在request的整个期间都可以使用,如在View层里PO也可以lazy loading数据,如 ${ company.employees }。当View 层逻辑完成后,才会通过Filter的doFilter方法或Interceptor的postHandle方法自动关闭session。
 
OpenSessionInViewInterceptor配置
<beans>
<bean name="openSessionInViewInterceptor"
class
="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="urlMapping"
class
="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="openSessionInViewInterceptor"/>
</list>
</property>
<property name="mappings">
 ..
</property>
</bean>
 

 
</beans>
 
OpenSessionInViewFilter配置
<web-app>
 

 
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<!-- singleSession默认为true,若设为false则等于没用OpenSessionInView -->
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
 

 
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

 
</web-app>
 
很多人在使用OpenSessionInView过程中提及一个错误:
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations
are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into
FlushMode.AUTO or remove 'readOnly' marker from transaction definition
看看OpenSessionInViewFilter里的几个方法
 
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,FilterChain filterChain)
throws ServletException, IOException {
 SessionFactory sessionFactory = lookupSessionFactory();
 logger.debug("Opening Hibernate Session in OpenSessionInViewFilter");
 Session session = getSession(sessionFactory);
 TransactionSynchronizationManager.bindResource(
  sessionFactory, new SessionHolder(session));
 try {
  filterChain.doFilter(request, response);
 }
 finally {
 TransactionSynchronizationManager.unbindResource(sessionFactory);
 logger.debug("Closing Hibernate Session in OpenSessionInViewFilter");
 closeSession(session, sessionFactory);
 }
}
 
 
protected Session getSession(SessionFactory sessionFactory)
throws DataAccessResourceFailureException {
 Session session = SessionFactoryUtils.getSession(sessionFactory, true);
 session.setFlushMode(FlushMode.NEVER);
 return session;
}

protected void closeSession(Session session, SessionFactory sessionFactory)
throws CleanupFailureDataAccessException {
 SessionFactoryUtils.closeSessionIfNecessary(session, sessionFactory);
}
 
     可以看到OpenSessionInViewFilter在getSession的时候,会把获取回来的session的flush mode 设为FlushMode.NEVER。然后把该sessionFactory绑定到 TransactionSynchronizationManager,使request的整个过程都使用同一个session,在请求过后再接除该 sessionFactory的绑定,最后closeSessionIfNecessary根据该 session是否已和transaction绑定来决定是否关闭session。在这个过程中,若HibernateTemplate 发现自当前session有不是readOnly的transaction,就会获取到FlushMode.AUTO Session,使方法拥有写权限。
 
public static void closeSessionIfNecessary(Session session, SessionFactory sessionFactory)
throws CleanupFailureDataAccessException {
 
if (session == null ||
TransactionSynchronizationManager.hasResource(sessionFactory)) {
return;
}
logger.debug("Closing Hibernate session");
try {
session.close();
}
catch (JDBCException ex) {
// SQLException underneath
throw new CleanupFailureDataAccessException("Could not close Hibernate session", ex.getSQLException());
}catch (HibernateException ex) {
throw new CleanupFailureDataAccessException("Could not close Hibernate session", ex);
}
}
 
    也即是,如果有不是readOnly的transaction就可以由Flush.NEVER转为Flush.AUTO,拥有 insert,update,delete操作权限,如果没有transaction,并且没有另外人为地设flush model的话,则doFilter的整个过程都是Flush.NEVER。所以受transaction保护的方法有写权限,没受保护的则没有。
采用spring的事务声明,使方法受transaction控制
 
 
<bean id="baseTransaction"
class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract
="true">
<property name="transactionManager" ref="transactionManager"/>
<property name="proxyTargetClass" value="true"/>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
 
<bean id="userService" parent="baseTransaction">
 
<property name="target">
<bean class="com.phopesoft.security.service.impl.UserServiceImpl"/>
</property>
</bean>
 
对 于上例,则以save,add,update,remove开头的方法拥有可写的事务,如果当前有某个方法,如命名为importExcel(),则因没 有transaction而没有写权限,这时若方法内有insert,update,delete操作的话,则需要手动设置flush model为Flush.AUTO,如
session.setFlushMode(FlushMode.AUTO);
session.save(user);
session.flush();
      尽 管Open Session In View看起来还不错,其实副作用不少。看回上面OpenSessionInViewFilter的doFilterInternal方法代码,这个方法 实际上是被父类的doFilter调用的,因此,我们可以大约了解的OpenSessionInViewFilter调用流程: request(请求)->open session并开始transaction->controller->View(Jsp)->结束transaction并 close session.
     一切看起来很正确,尤其是在本地开发测试的时候没出现问题,但试想下如果流程中的某一步被阻塞的话,那在这期间connection就一直被占用而不释 放。最有可能被阻塞的就是在写Jsp这步,一方面可能是页面内容大,response.write的时间长,另一方面可能是网速慢,服务器与用户间传输时 间久。当大量这样的情况出现时,就有连接池连接不足,造成页面假死现象。
Open Session In View是个双刃剑,放在公网上内容多流量大的网站请慎用。
什么是无人机巡检?如何实现无人机自动巡检? 复亚智能的无人机自动巡检系统通过高度自动化的技术,实现了无人机的高效巡检作业。无人机自动机场支持无人机的自动起降和电池更换,自动飞行算法确保无人机在复杂环境中的精准飞行和拍摄,而云端识别诊断和AI感知算法平台则实现图像的实时传输和智能分析。复亚智能,作为无人机技术的创新者和推动者,致力于开发先进的无人机自动巡检解决方案,助力各行各业实现智能化、自动化的巡检工作,引领智能巡检的未来。利用人工智能和机器学习算法,无人机可以被训练来识别特定的模式和异常,如结构损坏、热泄漏等,从而提高巡检的准确性和效率。 阅读详情

相关推荐

初学树莓派——(九)DHT11数据传入ONENET

目录 前言 DHT11实现本地温湿度监测 DHT11.py 接线图 效果图 温湿度数据上传至ONENET 新建产品 新建设备 ONENET.py 测试 Shell脚本配置 效果图 前言 本文分部分实现温湿度上传至OneNet云平台,本地数据监测,数据上传 新建一个文件夹,文件夹内包含DHT11.py、ONENET.py、tmp_data.txt、hum_data.txt DHT11实现本地温湿度监测 DHT11.py import RPi.GPIO as GPIO

会点灯的大力水手的博客 2612

OpenSessionInView

Spring 为我们提供了一个叫做 OpenSessionInViewFilter 的过滤器,他是标准的 Servlet Filter 所以我们把它按照规范配置到 web.xml 中方可使用。使用中我们必须配合使用 Spring 的 HibernateDaoSupport 来进行开发,也就是说,我们的dao层的类都要继承于 HibernateDaoSupport,从中由 Spring 来控制 Hi

韩迎宾的博客 702

FANUC机器人电弧跟踪功能深度解析:从电流采样原理到ROBOGUIDE仿真避坑指南

本文深度解析FANUC机器人电弧跟踪技术,从电流采样原理到ROBOGUIDE仿真避坑指南,详细介绍了电流波形分析、实时补偿机制及现场调试技巧。针对工业焊接中的常见问题,提供从底层原理到高阶应用的解决方案,帮助工程师优化自动化焊接质量。

weixin_31785177的博客 551

org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

<filter> <filter-name>osivFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> </filter> <filter-mapping> <filter-name>osivFilter</filt

专栏 3288

卷积操作复杂度公式推导

复杂度Hout⋅Wout⋅Cout⋅Cin⋅k2\text{复杂度} = H_{\text{out}} \cdot W_{\text{out}} \cdot C_{\text{out}} \cdot C_{\text{in}} \cdot k^2复杂度Hout​⋅Wout​⋅Cout​⋅Cin​⋅k2该公式反映了卷积核作用于每个像素时的局部操作和跨通道的计算需求。卷积计算复杂度与输入尺寸线性相关。

qq_41037856的博客 1306

Difftest踩坑笔记(一)

Difftest是差分测试框架,有什么用呢?高性能CPU的设计需要做复杂的测试,传统的测试方法运行较慢,而且出了问题很难定位,Difftest就是为了解决这两个问题。

http://www.icfgblog.com/ 4196

OpenSessionInViewFilter 的配置及替代方案

Spring 为我们提供了一个叫做 OpenSessionInViewFilter 的过滤器,他是标准的 Servlet Filter 所以我们把它按照规范配置到 web.xml 中方可使用。使用中我们必须配合使用 Spring 的 HibernateDaoSupport 来进行开发,也就是说,我们的dao层的类都要继承于 HibernateDaoSupport,从中由 Spring 来控制 Hi...

不积跬步 无以至千里 不积小流 无以成江海 467

Android GNSS 模块分析(三)JNI 层

Android GNSS;定位服务;GPS;NMEA协议;LocationManagerService;导航;

Yang_Mao_Shan的博客 5156

使用OpenSessionInViewFilter解决lazy failed问题时的一个现象

使用web.xml配置filter的方式:          hibernateFilter       org.springframework.orm.hibernate3.support.OpenSessionInViewFilter   hibernateFilter*.do 但是,还是出现一下异常:org.springframework.beans.factory.No

我的世界我的梦 2195

保姆级教程:在Ubuntu 22.04上从零安装SUMO并运行第一个交通仿真

本教程详细介绍了在Ubuntu 22.04系统上从零安装SUMO交通仿真软件并运行第一个仿真的完整流程。涵盖环境准备、PPA源安装、核心组件验证、首次仿真案例运行以及进阶配置技巧,特别针对netedit路网编辑器和地图格式转换等实用工具进行重点讲解,帮助用户快速掌握SUMO在交通仿真领域的应用。

weixin_30546189的博客 422

MATLAB应用实战系列(四十三)-基于MATLAB的光伏并网系统仿真设计

在Matlab中建立光伏电站接入系统模型,包括光伏发电逆变器及负荷模型等,仿真分析接入点处的电能质量,实现高品质并网运行。 壹 构建太阳能电池模型。 初始化参数如下图所示。 贰 构建MPPT控制模型。 具体封装如下所示: 通过PWM算法实现DC-DC稳步输出最大点。 叁 三、构建直流短距离输电线路。 仿真模型如下图: 通过电容来均衡线路电压,使得线路电压保持可靠性。 肆 通过逆变器变换,使得直流线路转换成...

wenyusuran的专栏 3169

Spring Data JPA 之 Session 的 open-in-view 对事务的影响

当我们使⽤ Spring Boot 加 JPA 的时候,会发现 Spring 帮我们新增了⼀个 spring.jpa.open-in-view 的配置,但是 Hibernate 本身却没有这个配置,不过其⼜是和 Hibernate 中的 Session 相关的,因此还是很重要的内容,所以这⼀讲我们来学习⼀下。

曾小二的博客 2702

32位快速加法器设计

32位快速加法器设计

好好学习,天天向上 3290

hibernate open session in view 抛出异常解决方法

在使用open-session-in-view的时候,如果使用不当,有可能抛出两种异常1,NonUniqueObjectException2,在配合spring使用的时候会可能会抛出org.springframework.dao.InvalidDataAccessApiUsageException先说1,这个异常的抛出原因和解决办法见这里:javaeye上有了很好的事例:http:/...

deso96240的博客 176

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

Open Session in View

我们在使用Hibernate的lazy load来优化性能的时候,只要Session关闭后再试图访问未被载入的对象时,就会出现异常。通常使用在事务之内来访问数据是适合的,但是有时候我们需要强制载入这些数据,例如在Web视图中访问这些模型对象。 在业务层强制载入这些数据,通常不是很好的解决方案,因为不同的视图在使用业务方法的时候,需要的数据通常不一样,这样业务方法可能绑定到特定的控...

fuliang 159

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Hibernate Open-Session-In-View Pattern

用hibernate,继续写代码来处理关联关系很繁,考虑用关联映射。一用就是臭名昭著的 LazyInitializationException,于是参考hibernate的best practice使用Open-Session-In-View方式。 http://www.jroller.com/cardsharp/entry/open_session_in_view_pattern

挣扎·前行 1630

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

hibernate的 open session in view

http://fuliang.iteye.com/blog/146965 我们在使用Hibernate的lazy load来优化性能的时候,只要Session关闭后再试图访问未被载入的对象时,就会出现异常。通常使用在事务之内来访问数据是适合的,但是有时候我们需要强制载入这些数据,例如在Web视图中访问这些模型对象。     在业务层强制载入这些数据,通常不是很好的解决方案,因为不同

caixiexin的专栏 3784

Open Session In View 探讨

    在没有使用Spring提供的Open Session In View情况下,因需要在service(or Dao)层里把session关闭,所以lazy loading 为true的话,要在应用层内把关系集合都初始化,如 company.getEmployees(),否则Hibernate抛session already closed Exception;    Open Session

cac blog 2250

open session in view

在使用hibernate时 在业务层关闭session 但是在表现层要调用 造成懒加载异常 解决方案:          1.lazy = false 不常用          2.Hibernate.initialize();          3.open session in view 在表现层开启和关闭session   open session ...

海子 162

8.Hibernate Open Session In View模式

是什么在Open Session In View模式    hibernate中使用load方法时,并未把数据真正获取时就关闭了session,当我们真正想获取数据时会迫使load加载数据,而此时 session已关闭,所以就会出现异常。 比较典型的是在MVC模式中,我们在M层调用持久层获取数据时(持久层用的是load方法加载数据),当这一调用结束时,session随之关闭,而我们 希望在V层使

柠檬需要糖 1770

open session in view 理解

一、配置        在没有使用Spring提供的Open Session In View情况下,因需要在service(or Dao)层里把session关闭,所以lazy loading 为true的话,要在应用层内把关系集合都初始化,如 company.getEm

a154832918的专栏 5756
github_28458131
博客等级 码龄11年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值