集成shiro 出现org.hibernate.HibernateException: No Session found for current thread

问题描述

在Realm中,方法doGetAuthenticationInfo,通过sysUserService查询,执行正常方法doGetAuthorizationInfo,也是通过sysUserService查询,结果会报错:org.hibernate.HibernateException:NoSessionfoundforcurrentthread同个类中,为什么一个正常,一个会报session找不到请各位大侠帮忙指点!web.xml<filter><filter-name>shiroFilter</filter-name><filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class></filter><filter-mapping><filter-name>shiroFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><filter>

applicationContext-shiro.xml<beanid="securityManager"class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><propertyname="realm"ref="myRealm"/><propertyname="cacheManager"ref="shiroCacheManager"/></bean><beanid="myRealm"class="com.bkbk.module.admin.Realm.MyRealm"><propertyname="sysUserService"ref="sysUserService"/></bean><beanid="shiroFilter"class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"><propertyname="securityManager"ref="securityManager"/><propertyname="loginUrl"value="/admin/sys_user!login.action"/><propertyname="successUrl"value="/admin/process_definition!findPage.action"/><propertyname="unauthorizedUrl"value="/403.do"/><propertyname="filters"><map><entrykey="authenticationFilter"value-ref="authenticationFilter"/></map></property><propertyname="filterChainDefinitions"><value>/admin/logout.jsp=logout/admin/work!**=perms["admin:work"]</value></property></bean><beanid="shiroCacheManager"class="org.apache.shiro.cache.ehcache.EhCacheManager"><propertyname="cacheManagerConfigFile"value="classpath:ehcache.xml"/></bean><beanid="lifecycleBeanPostProcessor"class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/><beanclass="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"depends-on="lifecycleBeanPostProcessor"><propertyname="proxyTargetClass"value="true"/></bean><beanclass="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"><propertyname="securityManager"ref="securityManager"/></bean>

MyRealm.javapublicclassMyRealmextendsAuthorizingRealmimplementsRealm,InitializingBean{privateSysUserServicesysUserService;publicMyRealm(){super();}/***认证信息*/@OverrideprotectedAuthenticationInfodoGetAuthenticationInfo(AuthenticationTokenauthcToken)throwsAuthenticationException{UsernamePasswordTokentoken=(UsernamePasswordToken)authcToken;StringuserName=token.getUsername();if(userName!=null&&!"".equals(userName)){、//这个地方查询,执行正常SysUseruser=sysUserService.login(token.getUsername(),String.valueOf(token.getPassword()));if(user!=null)returnnewSimpleAuthenticationInfo(user.getUserName(),user.getPassword(),getName());}returnnull;}/***授权信息*/@OverrideprotectedAuthorizationInfodoGetAuthorizationInfo(PrincipalCollectionprincipals){Stringusername=SecurityUtils.getSubject().getPrincipals().fromRealm(getName()).toString();if(username!=null){SimpleAuthorizationInfoinfo=newSimpleAuthorizationInfo();//这个地方去查询,结果在dao会报错:org.hibernate.HibernateException:NoSessionfoundforcurrentthreadinfo.addRoles(sysUserService.findRoleByUser(user));returninfo;}returnnull;}publicSysUserServicegetSysUserService(){returnsysUserService;}publicvoidsetSysUserService(SysUserServicesysUserService){this.sysUserService=sysUserService;}@OverridepublicvoidafterPropertiesSet()throwsException{Assert.notNull(sysUserService);//TODOAuto-generatedmethodstub}@OverridepublicStringgetName(){returngetClass().getName();}}

解决方案

本帖最后由 Jayron 于 2014-02-13 14:55:40 编辑
解决方案二:
SysUserService贴出来看看吧
解决方案三:
hibernate配置文件,没有配置绑定当前线程的session
解决方案四:
引用1楼song_1990的回复:

SysUserService贴出来看看吧

第一个是正常的,第二个会报nosessionSysUserService.javapublicSysUserlogin(StringuserName,Stringpassword){SysUseruser=null;List<Object[]>list=sessionFactory.getCurrentSession().createSQLQuery("selectid_,password_,user_name_from`user_`where`user_name_`='"+userName+"'and`password_`='"+password+"'").list();for(Object[]o:list){user=newSysUser();user.setId(((BigInteger)o[0]).longValue());user.setPassword((String)o[1]);user.setUserName((String)o[2]);}returnuser;}publicList<String>findResourceByUser(SysUseruser){returnsessionFactory.getCurrentSession().createSQLQuery("selectdistinct(url_)fromresource_").list();}

解决方案五:
引用1楼song_1990的回复:

SysUserService贴出来看看吧

如果用isPermitted()主动去触发MyRealmk中的doGetAuthorizationInfo(),会正常返回结果。但如果是通过访问去触发MyRealm中的doGetAuthorizationInfo(),会出现nosession的错。很奇怪
解决方案六:
引用2楼MK_Code的回复:

hibernate配置文件,没有配置绑定当前线程的session

如果用isPermitted()主动去触发MyRealmk中的doGetAuthorizationInfo(),会正常返回结果。但如果是通过访问去触发MyRealm中的doGetAuthorizationInfo(),会出现nosession的错。应该都有绑定,会不会是因为session已经关闭?还是?
解决方案七:
<filter><filter-name>shiroFilter</filter-name><filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class><init-param><param-name>targetFilterLifecycle</param-name><param-value>true</param-value></init-param></filter>这样试试
解决方案八:
引用6楼song_1990的回复:

<filter><filter-name>shiroFilter</filter-name><filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class><init-param><param-name>targetFilterLifecycle</param-name><param-value>true</param-value></init-param></filter>这样试试

引用6楼song_1990的回复:

<filter><filter-name>shiroFilter</filter-name><filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class><init-param><param-name>targetFilterLifecycle</param-name><param-value>true</param-value></init-param></filter>这样试试

还是不行org.hibernate.HibernateException:NoSessionfoundforcurrentthread谢谢song_1990的提醒,应该是session生命周期的问题。只是很奇怪登录的方法doGetAuthorizationInfo是正常,获取权限信息的方法doGetAuthorizationInfo却有这个错
解决方案九:
我也遇见了怎么解决啊大哥
解决方案十:
用hibernate3下南就改成".hibernate4"

<filter><filter-name>hibernateFilter</filter-name><filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class></filter><filter-mapping><filter-name>hibernateFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>

解决方案十一:
有可能是你并没有成功登陆。

时间: 2024-12-03 14:23:36

集成shiro 出现org.hibernate.HibernateException: No Session found for current thread的相关文章

Spring事务总是报org.hibernate.HibernateException: No Session found for current thread

问题描述 今天搞了一下spring mvc的注解,弄到数据库的时候,向数据库中存数据没办法存入成功,看一下错误代码是org.hibernate.HibernateException: No Session found for current thread,网上找了一些方法一直没解决,哪位帮忙看一下,代码如下 applicationContext-db.xml代码: <?xml version="1.0" encoding="UTF-8"?> <be

org.hibernate.HibernateException: No Session found for current thread

问题描述 hibernate:4.1.6spring:4.0.1 工程启动没错,进入getAllUser页面获得数据库用户信息时报如下错误:  严重: Servlet.service() for servlet springMVC threw exceptionorg.hibernate.HibernateException: No Session found for current threadat org.springframework.orm.hibernate4.SpringSessio

错误异常:org.hibernate.HibernateException: No Session found for current thread,求助

问题描述 HTTPStatus500-NoSessionfoundforcurrentthreadtypeExceptionreportmessageNoSessionfoundforcurrentthreaddescriptionTheserverencounteredaninternalerrorthatpreventeditfromfulfillingthisrequest.exceptionorg.hibernate.HibernateException:NoSessionfoundfo

org.hibernate.HibernateException: No Session found for current thread[附web.xml]

问题描述 hibernate:4.1.6spring:4.0.1工程启动没错,进入getAllUser页面获得数据库用户信息时报如下错误:严重:Servlet.service()forservletspringMVCthrewexceptionorg.hibernate.HibernateException:NoSessionfoundforcurrentthreadatorg.springframework.orm.hibernate4.SpringSessionContext.current

spring MVC No Session found for current thread

项目使用springframework 3.2.3.RELEASE,hibernate 4.2.2.Final,使用spring MVC 项目名:exam3 使用数据库:MySQL 和数据库交互时报错: Xml代码   严重: Servlet.service() for servlet [spring2] in context with path [/exam3] threw exception [Request processing failed; nested exception is or

ssh整合-使用spring3.X+Hibernate4.X来做项目,报异常 No Session found for current thread

问题描述 使用spring3.X+Hibernate4.X来做项目,报异常 No Session found for current thread org.hibernate.HibernateException: No Session found for current thread at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) at

spring3.2+hibernate4.2 整合 spring security报No Session found for current thread

问题描述 public class SpringSecurityMetaDataSource implements FilterInvocationSecurityMetadataSource {//@Resource(name = "resourceDao")private ResourceDao resourceDao;private static Map<String, Collection<ConfigAttribute>> resourceMap =

Spring3.2+Hibernate4.1集成的时候提示找不到Session

问题描述 大家好, 我今天在整合spring3+hibernate的时候老是被提示找不到session  我获取session的方式是sessionFactory.getCurrentSession()控制台报的错误:严重: Servlet.service() for servlet springmvc threw exceptionorg.hibernate.HibernateException: No Session found for current threadat org.spring

Hibernate中的session的save方法。

问题描述 Hibernate中的session的save方法. 我正在学习hibernate框架,然后在做一个插入的时候遇到了一个问题.通过跟踪我发现在执行session.save(obj);方法时,程序就不动了.并且在获取session时还打印出下面红色的信息. 请问一个是什么原因以及怎么解决呢? public int insertClient(Client client) { // TODO Auto-generated method stub //HibernateUtil.closeSe