在Realm中,
方法doGetAuthenticationInfo登录校验,通过sysUserService查询,执行正常
方法doGetAuthorizationInfo获取权限,也是通过sysUserService查询,结果会报错:org.hibernate.HibernateException: No Session found for current thread
同个类中,为什么一个正常,一个会报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>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="myRealm"/> <property name="cacheManager" ref="shiroCacheManager" /> </bean> <bean id="myRealm" class="com.bkbk.module.admin.Realm.MyRealm"> <property name="sysUserService" ref="sysUserService"/> </bean> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="/admin/sys_user!login.action"/> <property name="successUrl" value="/admin/process_definition!findPage.action"/> <property name="unauthorizedUrl" value="/403.do"/> <property name="filters"> <map> <entry key="authenticationFilter" value-ref="authenticationFilter" /> </map> </property> <property name="filterChainDefinitions"> <value> /admin/logout.jsp = logout /admin/work!** = perms["admin:work"] </value> </property> </bean> <bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/> </bean> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"> <property name="proxyTargetClass" value="true" /> </bean> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager"/> </bean>
public class MyRealm extends AuthorizingRealm implements Realm, InitializingBean{ private SysUserService sysUserService; public MyRealm() { super(); } /** * 认证信息 */ @Override protected AuthenticationInfo doGetAuthenticationInfo( AuthenticationToken authcToken ) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken) authcToken; String userName = token.getUsername(); if( userName != null && !"".equals(userName) ){ 、 //这个地方查询,执行正常 SysUser user = sysUserService.login(token.getUsername(), String.valueOf(token.getPassword())); if( user != null ) return new SimpleAuthenticationInfo( user.getUserName(),user.getPassword(), getName()); } return null; } /** * 授权信息 */ @Override protected AuthorizationInfo doGetAuthorizationInfo( PrincipalCollection principals) { String username = SecurityUtils.getSubject().getPrincipals().fromRealm(getName()).toString(); if( username != null ){ SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); //这个地方去查询,结果在dao会报错:org.hibernate.HibernateException: No Session found for current thread info.addRoles(sysUserService.findRoleByUser(user)); return info; } return null; } public SysUserService getSysUserService() { return sysUserService; } public void setSysUserService(SysUserService sysUserService) { this.sysUserService = sysUserService; } @Override public void afterPropertiesSet() throws Exception { Assert.notNull(sysUserService); // TODO Auto-generated method stub } @Override public String getName() { return getClass().getName(); } }
public SysUser login(String userName, String password) { SysUser user = null; List<Object[]> list = sessionFactory.getCurrentSession().createSQLQuery("select id_,password_,user_name_ from `user_` where `user_name_`= '"+userName+"' and `password_`='"+password+"'").list(); for(Object[] o : list) { user = new SysUser(); user.setId(((BigInteger)o[0]).longValue()); user.setPassword((String)o[1]); user.setUserName((String)o[2]); } return user; } public List<String> findResourceByUser(SysUser user) { return sessionFactory.getCurrentSession().createSQLQuery("select distinct(url_) from resource_").list(); }
spring配置事务了吗?有集成spring事务,spring版本是3.2<preclass="brush:xml;toolbar:true;auto-links:false;"><tx:annotation-drivenproxy-target-class="true"transaction-manager="transactionManager"/><beanid="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager"><propertyname="sessionFactory"ref="sessionFactory"/></bean><tx:adviceid="transactionAdvice"transaction-manager="transactionManager"><tx:attributes><tx:methodname="save*"propagation="REQUIRED"/><tx:methodname="delete*"propagation="REQUIRED"/><tx:methodname="update*"propagation="REQUIRED"/><tx:methodname="get*"read-only="true"/><tx:methodname="load*"read-only="true"/><tx:methodname="find*"read-only="true"/><tx:methodname=""read-only="true"/></tx:attributes></tx:advice>
这是事务的配置
你用的hibernate是几?
这个也需要配置:
<preclass="brush:xml;toolbar:true;auto-links:false;"><aop:configproxy-target-class="true"><aop:advisoradvice-ref="txAdvice"pointcut="execution(com.名字.项目名称...(..))"/></aop:config>web.xml里有这个吗
<preclass="brush:xml;toolbar:true;auto-links:false;"><filter><filter-name>openSessionInViewFilter</filter-name><filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class><init-param><param-name>singleSession</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>openSessionInViewFilter</filter-name><url-pattern>/</url-pattern></filter-mapping>老哥解决了吗
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。