问题描述
在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>
解决方案十一:
有可能是你并没有成功登陆。