java_wxp
2013-04-30 12:28 阅读 207
已采纳

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

[code="java"]
public class SpringSecurityMetaDataSource implements FilterInvocationSecurityMetadataSource {
//@Resource(name = "resourceDao")
private ResourceDao resourceDao;

private static Map<String, Collection<ConfigAttribute>> resourceMap = null;

@Autowired
public SpringSecurityMetaDataSource(ResourceDao resourceDao) {
    this.resourceDao = resourceDao;
    loadResourceDefine();
}

}

/**
 * 加载所有资源与权限的关系
 */
@SuppressWarnings("unused")
private void loadResourceDefine() {  
    if(resourceMap == null) {  
        resourceMap = new HashMap<String, Collection<ConfigAttribute>>();  
        List<ResourceManager> resources = this.resourceDao.listAll(ResourceManager.class);
        for (ResourceManager resource : resources) {
            Collection<ConfigAttribute> configAttributes = new ArrayList<ConfigAttribute>();  
                            //以权限名封装为Spring的security Object  
            ConfigAttribute configAttribute = new SecurityConfig(resource.getName());  
            configAttributes.add(configAttribute);  
            resourceMap.put(resource.getUrl(), configAttributes);  
        }  
    }  

    Set<Entry<String, Collection<ConfigAttribute>>> resourceSet = resourceMap.entrySet();
    Iterator<Entry<String, Collection<ConfigAttribute>>> iterator = resourceSet.iterator();

}

[/code]

断点跟进去后发现:
List resources = this.resourceDao.listAll(ResourceManager.class);
是这抛出的错,报Caused by: org.hibernate.HibernateException: No Session found for current thread

我怀疑 通过构造方法注入的resourceDao不受hibernate的事务管理,
因此上诉代码改为了:

[code="java"]
@Resource(name = "resourceDao")
private ResourceDao resourceDao;

private static Map<String, Collection<ConfigAttribute>> resourceMap = null;

@Autowired
public SpringSecurityMetaDataSource() {
    loadResourceDefine();
}

[/code]

这样的话,又会报resourceDao为空指针错,
详见代码附件。
jar包如下:
antlr-2.7.7.jar
aopalliance-1.0.jar
aspectjrt.jar
aspectjweaver.jar
commons-fileupload-1.3.jar
commons-logging-1.1.2.jar
dom4j-1.6.1.jar
druid-0.2.12.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.2.0.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jta-1.1.jar
mysql-connector-java-5.1.21.jar
spring-aop-3.2.2.RELEASE.jar
spring-aspects-3.2.2.RELEASE.jar
spring-beans-3.2.2.RELEASE.jar
spring-context-3.2.2.RELEASE.jar
spring-core-3.2.2.RELEASE.jar
spring-expression-3.2.2.RELEASE.jar
spring-jdbc-3.2.2.RELEASE.jar
spring-orm-3.2.2.RELEASE.jar
spring-security-config-3.2.0.M1.jar
spring-security-core-3.2.0.M1.jar
spring-security-web-3.2.0.M1.jar
spring-tx-3.2.2.RELEASE.jar
spring-web-3.2.2.RELEASE.jar
spring-webmvc-3.2.2.RELEASE.jar

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    jinnianshilongnian jinnianshilongnian 2013-04-30 13:16

    这样的话,又会报resourceDao为空指针错, -------->


    <!-- 鍙涓氬姟閫昏緫灞傚疄鏂戒簨鍔?-->


    /aop:config

    <aop:config expose-proxy="true" proxy-target-class="true" />
    

    -------------改为--------->


    <!-- 鍙涓氬姟閫昏緫灞傚疄鏂戒簨鍔?-->


    /aop:config

    试试

    点赞 评论 复制链接分享
  • iteye_11128 iteye_11128 2013-11-01 12:28

    我也遇到这样的错误,请教你,你解决了没

    点赞 评论 复制链接分享
  • iteye_11128 iteye_11128 2013-11-01 12:29

    我是用SpringSecurityMetaDataSource 继承DAO类来获取session

    点赞 评论 复制链接分享
  • weixin_42671212 weixin_42671212 2014-04-27 16:40

    我查了好久,应该是hibernate4的session没有在loadresource的方法open,我的解决办法是直接注入sessionfactory到这个类中,然后在调用resourceDao之前,opensession

    点赞 评论 复制链接分享

相关推荐