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

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

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


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


/aop:config

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

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


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


/aop:config

试试

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

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

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