OpenSessionInViewFilter要怎么配置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenSessionInViewFilter要怎么配置相关的知识,希望对你有一定的参考价值。
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
配置在了web.xml最上面,可是没起作用啊
运行时还是提示could not initialize proxy - no Session
我一直就有事物啊:
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<tx:advice id="transTxAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" read-only="false" rollback-for="Exception" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor pointcut="execution(* com.zjm.study.dao.*.*(..))" advice-ref="transTxAdvice" />
<aop:advisor pointcut="execution(* com.zjm.study.action.*.*(..))" advice-ref="transTxAdvice" />
</aop:config>
何时期lazy loading数据。
如果是singleSession=false的话,就不会在每次的整个request的过程中使用同一个hibernate session,而是每个数据访问都会产生各自的seesion,等于没有Open Session in View.
OpenSessionInViewFilter默认是不会对session 进行flush的,并且flush mode 是 never
代码:
protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException
Session session = SessionFactoryUtils.getSession(sessionFactory, true);
session.setFlushMode(FlushMode.NEVER);
return session;
看getSession的方式就知道,把flush mode 设为FlushMode.NEVER,这样就算是commit的时候也不会session flush,
如果想在完成request过程中更新数据的话, 那就需要先把flush model设为FlushMode.AUTO,再在更新完数据后flush.
.
OpenSessionInView默认的FlushMode为
代码:
FlushMode.NEVER
::::::::::解决方法::::::::::::
可以采用spring的事务声明解决,示例代码如下:
<!-- 声明一个 Hibernate 3 的 事务管理器供代理类自动管理事务用-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<aop:config>
<!-- 切入点指明了在执行com.ssh2.manager包中的所有方法时产生事务拦截操作 -->
<aop:pointcut id="daoMethods" expression="execution(* com.ssh2.manager.*.*(..))"/>
<!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods"/>
</aop:config>
<!-- 事务通知操作,使用的事务管理器引用自transactionManager -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 指定哪些方法需要加入事务 -->
<tx:method name="getPageTable*" propagation="REQUIRED"/>
<tx:method name="getTotalRecodes" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> 参考技术A protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException
Session session = SessionFactoryUtils.getSession(sessionFactory, true);
session.setFlushMode(FlushMode.NEVER);
return session;
看getSession的方式就知道,把flush mode 设为FlushMode.NEVER,这样就算是commit的时候也不会session flush,
如果想在完成request过程中更新数据的话, 那就需要先把flush model设为FlushMode.AUTO,再在更新完数据后flush.
.
OpenSessionInView默认的FlushMode为
代码:
FlushMode.NEVER
::::::::::解决方法::::::::::::
可以采用spring的事务声明解决,示例代码如下:
<!-- 声明一个 Hibernate 3 的 事务管理器供代理类自动管理事务用-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<aop:config>
<!-- 切入点指明了在执行com.ssh2.manager包中的所有方法时产生事务拦截操作 -->
<aop:pointcut id="daoMethods" expression="execution(* com.ssh2.manager.*.*(..))"/>
<!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods"/>
</aop:config>
<!-- 事务通知操作,使用的事务管理器引用自transactionManager -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 指定哪些方法需要加入事务 -->
<tx:method name="getPageTable*" propagation="REQUIRED"/>
<tx:method name="getTotalRecodes" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> 参考技术B DDDDDDDDDDDDDDD
带有执行器和自定义安全性的 Spring Boot OpenSessionInViewFilter
【中文标题】带有执行器和自定义安全性的 Spring Boot OpenSessionInViewFilter【英文标题】:Spring boot OpenSessionInViewFilter with actuator and custom Security 【发布时间】:2016-02-10 20:55:40 【问题描述】:我正在使用 spring boot 来创建 RESTful Web 服务。为了安全起见,我通过扩展 AbstractUserDetailsAuthenticationProvider 来使用 BasicAuthenticationFilter 和自定义 AuthenticationProvider。我在控制器类中使用带有@Secured 注释的方法级安全性。一切都很好,除了我想让弹簧执行器也能正常工作。
执行器的问题是我在尝试验证请求时遇到休眠异常,因为没有打开会话。因此,当我转到http://localhost:8081/info 时,我得到了一个休眠异常。
似乎执行器正在使用与应用程序的其余部分不同的过滤器链。如果是这样的话,我相信这是有充分理由的。但是,我不知道如何注册我的 OpenSessionInViewFilter 以便它成为每个过滤器链中的第一个过滤器。我的 OpenSessionInViewFilter 是通过我的 @Configuration 类之一中的 FilterRegistrationBean 注册的。
Spring boot 很棒,但引擎盖下有很多魔法,我还没有弄清楚如何深入研究他们的代码,看看它从哪里开始并跟进。
这是我的一些配置。我试图包括我认为重要的内容。如果您还想看其他作品,请告诉我。
感谢任何帮助。
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
@Override
protected void configure(HttpSecurity http) throws Exception
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.authorizeRequests().anyRequest().fullyAuthenticated()
.and().httpBasic().authenticationDetailsSource(new WebAuthenticationDetailsSource()
@Override
public WebAuthenticationDetails buildDetails(HttpServletRequest context)
return new ThirdPartyAuthenticationDetails(context);
)
.and().csrf().disable();
@Configuration
public class WebSecurityConfiguration extends GlobalAuthenticationConfigurerAdapter
@Autowired
private DataSource dataSource;
@Autowired
@Qualifier("thirdPartySystemDAO")
private EntityDAO<ThirdPartySystem> thirdPartySystemDAO;
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception
auth.authenticationProvider(authenticationProvider());
@Bean
public AuthenticationProvider authenticationProvider()
ThirdPartyAuthenticationProvider result = new ThirdPartyAuthenticationProvider();
result.setSecurityService(thirdPartySecurityService());
return result;
@Bean
public ThirdPartySecurityService thirdPartySecurityService()
ThirdPartySecurityServiceImpl result = new ThirdPartySecurityServiceImpl();
result.setJdbcTemplate(new JdbcTemplate(dataSource));
result.setThirdPartySystemDAO(thirdPartySystemDAO);
return result;
这是尝试访问执行器的 /info 端点时的堆栈跟踪。
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134) ~[spring-orm-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988) ~[hibernate-core-4.2.2.Final.jar:4.2.2.Final]
at com.redi.dao.hibernate.HibernateDAO.getCurrentSession(HibernateDAO.java:495) ~[redi-dao-4.3.0.jar:4.3.0]
at com.redi.dao.hibernate.HibernateDAO.createQuery(HibernateDAO.java:460) ~[redi-dao-4.3.0.jar:4.3.0]
at com.redi.dao.hibernate.HibernateDAO.findOneWhere(HibernateDAO.java:58) ~[redi-dao-4.3.0.jar:4.3.0]
at com.mycompany.DefaultVerifiService.getInstitution(DefaultVerifiService.java:349) ~[classes/:na]
at com.mycompany.thirdparty.ws.security.ThirdPartyAuthenticationProvider.retrieveUser(ThirdPartyAuthenticationProvider.java:36) ~[classes/:na]
at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:143) ~[spring-security-core-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:167) ~[spring-security-core-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:192) ~[spring-security-core-4.0.3.RELEASE.jar:4.0.3.RELEASE]
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:168) ~[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) ~[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110) ~[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) ~[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57) ~[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) ~[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) ~[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) ~[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50) ~[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) ~[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) ~[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) ~[spring-security-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.15.jar:8.0.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.15.jar:8.0.15]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) ~[tomcat-embed-core-8.0.15.jar:8.0.15]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [tomcat-embed-core-8.0.15.jar:8.0.15]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) [tomcat-embed-core-8.0.15.jar:8.0.15]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) [tomcat-embed-core-8.0.15.jar:8.0.15]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.0.15.jar:8.0.15]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [tomcat-embed-core-8.0.15.jar:8.0.15]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537) [tomcat-embed-core-8.0.15.jar:8.0.15]
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085) [tomcat-embed-core-8.0.15.jar:8.0.15]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658) [tomcat-embed-core-8.0.15.jar:8.0.15]
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222) [tomcat-embed-core-8.0.15.jar:8.0.15]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556) [tomcat-embed-core-8.0.15.jar:8.0.15]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513) [tomcat-embed-core-8.0.15.jar:8.0.15]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_51]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_51]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.0.15.jar:8.0.15]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_51]
【问题讨论】:
【参考方案1】:注册 OpenSessionInViewFilter 并将以下代码放在您的配置类中:
@Bean
public FilterRegistrationBean registerOpenEntityManagerInViewFilterBean()
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
OpenEntityManagerInViewFilter filter = new OpenEntityManagerInViewFilter();
registrationBean.setFilter(filter);
registrationBean.setOrder(5);
return registrationBean;
您必须记住...如果您使用的是 Hibernate,请声明 OpenSessionInViewFilter。但如果您使用的是 JPA,请声明 OpenEntityManagerInViewFilter。
祝你好运。
【讨论】:
感谢您的回答,但我已经解决了这个问题。不记得这是一年多前的事了。 嗨,为什么bean的顺序是“5”?以上是关于OpenSessionInViewFilter要怎么配置的主要内容,如果未能解决你的问题,请参考以下文章
Hibernate OpenSessionInViewFilter 过早关闭会话?
使用 Spring Boot 和 Data JPA,尽管有 OpenSessionInViewFilter,但仍会出现 LazyInitializationException
ssh中org.springframework.orm.hibernate4.support.OpenSessionInViewFilter的作用及配置
是否可以在 Spring 应用程序上下文中配置 OpenSessionInViewFilter 以便 context:property-placeholder 可用?